Nanoflux Fusion API

05 Jul 2016 . category: docs . Comments
#tutorial

Nanoflux Fusion API Overview

Fusion API

NanoFlux Fusion API

The nanoflux Fusion API extends the normal nanoflux API by very few methods only, i.e. you may use nanoflux as usual, or even mix both styles (which I do not recommend, as it mixes concepts)

getFusionStore()

nanoflux Fusion works with a dedicated single store only. This method returns this store, i.e. a normal nanoflux store. It is already connected to the default dispatcher, so there`s no need to care about the internals.

This store has only three public functions, which are

  • getState(), which returns the immutable application state.
  • use(), which uses a middleware function of type function(newState,currentState, actionName). The function object must return an object, considered as new state.
  • subscribe(), which connects components to listen to changes on this store (the same as in nanoflux)

createFusionator( descriptor, initialState, namespace? )

Creates a Fusionator with optional namespace.

The descriptor is a JSON object containing the Fusionators available functions. The method must return a JSON object or a A+ compliant Promise in case of asynchronous actions. The passed object will become part of the application state then. Each function of a Fusionator has the following signature

function(previousState, argumentList)

where previousState is the immutable application state, and argumentList is an array of arguments passed on the related Actors call.

Additionally, an initial state must be provided, i.e. a JSON object that describes your state at startup/before first action.

The optional namespace argument should be used when breaking Fusionators apart (important for larger applications). With namespace you avoid naming collisions. If a namespace is not given the default namespace is used.

Note: The state object can be of any complexity, as immutability is done recursively. Although, this may have impact on runtime performance, it was proven that performance is sufficient even for several thousand states.

var descriptor = {
	// will be mapped to actor name 'addItem'
	addItem  : function(previousState, args){
		// items array is immutable, so we need to create a copy
		var items = _.deepClone(previousState.items);
		var item = args[0];		
		items.push(item);
		return { items: items }
	}
};

var initialState = { items: [] }

NanoFlux.createFusionator(descriptor, initialState, 'fooNamespace');

'fooNamespace');

getFusionActor( actorId, namespace? )

Returns a function object, i.e. a specific action for a specific Fusionator. Actors are created automatically when a calling createFusionator(). The actorId is the functions name defined in the Fusionator. Using namespaces helps to avoid naming collisions, when using multiple Fusionators. If namespace is not given the default namespace is used.

 
var descriptor = {
	// will be mapped to actor name 'myAction'
	myAction  : function(previousState, args){
	    return { 
	        bar: { foo:  "foo"}, 
	        a: args[0], // {a: 123, b: "text"}
	        b: args[1] // "2ndArg"
	    }; 
	}
}

var initialState = {
	bar : {},
	a: {},
	b: {}
};

var fooNS = 'fooNamespace';
 
NanoFlux.createFusionator(descriptor, initialState ,fooNS);
  
// get Actor
var myAction = NanoFlux.getFusionActor('myAction', fooNS);

// call Actor
myAction({a: 123, b: "text"},"2ndArg");