RocketJump Composition
RocketJumps can be merged!
As stated in the initial part of the API description, the rj constructor can take several arguments, each of which can be the result of a previous call (provided that the effect property was not set), a provided plugin, or a configuration object. Calling rj with several arguments is interpreted as a composition will.
How does composition work?#
Consider the following example
The composition order goes from top to bottom and from left to right. Hence, in our example, we have the following composition order: config R > config O > config C > config K.
Different composition strategies#
Due to the wide variety of available configuration properties, it is not possible to define a global composition strategy. Instead, we can describe some composition strategies which describe how different properties are composed
Chain properties#
Chain properties are actions and selectors
Chain properties are so called because they are functions that are invoked in chain (i.e. the output of the previous one is the input of the second one) in composition order at composition time. The argument of the first call is the default value. At the end, the RocketJump Object will contain only the output of the last call.
In our example, let's pretend that all the four config objects (R, O, C, K) define the actions property. Composition works like this:
- the default action bag is generated
- the actions transform of
configRis called with the default action bag as parameter, and its output merged with the default action bag to producetempActionBag1 - the actions transform of
configOis called withtempActionBag1as parameter, and its output is merged withtempActionBag1to createtempActionBag2 - the actions transform of
configCis called withtempActionBag2as parameter, and its output is merged withtempActionBag2to createtempActionBag3
- the actions transform of
- the actions transform of
configKis called withtempActionBag3as parameter, and its output is merged withtempActionBag3to createtempActionBag4
- the actions transform of
tempActionBag4is the final action bag
Recursive properties#
Recursive properties are reducer, (composeReducer), effectCaller
Recursive properties are so called because they involve runtime function composition: the final value is a function which is the mathematical composition of the functions defined in the merged configurations in composition order.
composeReducer here is put in parenthesis because it is not involved directly in composition, being squashed onto the reducer property before composition starts (i.e. with respect to composition, there is no composeReducer property, but only reducer, which contains also all the composed reducers)
Let's pretend that all the four config objects (R, O, C, K) define the reducer property. Composition works like this:
- the default reducer is generated
- a new reducer is created by transforming the default reducer as stated in
configRto createtempReducer1 - a new reducer is created by transforming
tempReducer1as stated inconfigOto createtempReducer2 - a new reducer is created by transforming
tempReducer2as stated inconfigCto createtempReducer3 - a new reducer is created by transforming
tempReducer3as stated inconfigKto createtempReducer4 tempReducer4is the final reducer
Let's pretend that all the four config objects (R, O, C, K) define the effectCaller property. Composition works like this:
- the default effectCaller is generated
- when the user triggers an effect run, the following things happen:
- the
effectCallerspecified inconfigRis called, with itseffectargument set to a fake effect function - when the fake effect function is called with
...args, theeffectCallerset inconfigOis called with...argsand with a second fake effect function - when this second fake effect function is called with
...args, theeffectCallerset inconfigCis called with...argsand with a third fake effect function - when this third fake effect function is called with
...args, theeffectCallerset inconfigKis called with...argsand with a the default effect caller as itseffectparameter
- the
Merged properties#
Merged property is only computed
Merged properties are merged using plain object assignment in composition order
Overwrite properties#
Overwrite properties are takeEffect, name
Overwrite properties are not merged, the last configuration in composition order defining it wins