Projects:Actions and hooks with Global State/Specs
Contents |
Technical Specification
Actions
Currently all actions only receive two parameters, state and payload. Those should receive an additional options parameter which should contain the global state. If present, the return of an action should be the modified global state instead of the modified model state part.
Option is an object that will contain the global state, like so:
options = { globalState: {global state} // do not mutate (copy necessary parts first)} }
Model hooks
Currently all model hook receive one(state) or 2 parameters(state and payload). To achieve the objective of the project, a new parameter shall be added(options, which is expected to be an object).
This parameter may or may not be provided, when provided, it can include a globalState property. If provided, instead of returning the modified model state, the hook function will return the global state with the modifications applied to it.
Previously
OB.App.StateAPI.Ticket.addModelHook({ hook: (state, payload) => { // do logic return newState; // This is a state that only includes the Model, not global } });
After adding the new parameter
OB.App.StateAPI.Ticket.addModelHook({ hook: (state, payload, options) => { let newState = {...options.globalState}; // do logic over copy of global state and return the new state return newState; // a new modified globalState is being returned, the API will take care to pass it correctly to the rest of the hooks/actions } });
Action
Same logic as for model hooks is also applicable to actions/prehooks and posthooks.
OB.App.StateAPI.Ticket.registerAction( 'newAction', (state, payload, options) => { let newState = {...options.globalState}; // do logic over copy of global state and return the new state return newState; // a new modified globalState is being returned, the API will take care to pass it correctly to the rest of the hooks/actions } );
Action Preparation
Action preparations are also provided with the global state, although these can't modify it, they are able to read it and prepare the payload accordingly.
OB.App.StateAPI.Ticket.myAction.addActionPreparation( async (state, payload, options) => { let newPayload = {...payload}; // generate new payload, now it is also possible to use options.globalState to read info from the globalState return newPayload; // a new modified globalState is being returned, the API will take care to pass it correctly to the rest of the hooks/actions } );