Action Registry
The first step in adding Trrack
to your application is to create a action registry using the Registry
class.
const registry = Registry.create();
Now actions can be registered in the registry. Actions are the only way Trrack
knows that something has happened and it has to update the provenance graph.
Action Types
There are two types of actions that can be registered.
State Action
State actions get the current state
of the application as an argument and can modify it based on the payload
, which is the second argument.
This is similar to redux reducers (opens in a new tab).
// Action which updates the `count` key in the state by desired value.
const incrementAction = registry.register(
'increment-action', // Action name
// Action handler which receives the current state and a payload.
(currentState: State, payload: number) => {
currentState.count += payload;
}
);
When state actions are executed, the changes to the state are stored in a new provenance node.
Trrack Action
The other type of action that is supported is called a Trrack action
. These actions do not modify the application state, but execute any arbitrary side effect.
When trrack action
is executed, Trrack
keeps a record of execution and the payload of the action.
You need to provide a undo
counterpart to each action to allow Trrack
to undo the action.
An trrack action
can be set as undo for itself.
const arr = [1, 2, 3, 4];
registry.register('sort', (sortBy: 'asc' | 'dsc') => {
if (sortBy === 'asc') arr.sort();
if (sortBy === 'dsc') arr.sort().reverse();
return {
type: 'sort',
payload: sortBy === 'asc' ? 'dsc' : 'asc',
meta: {
hasSideEffects: true,
},
};
});
Trrack provides no guarantees if the trrack action
is properly undone by the
undo action
. We rely on developers for correct implementation. Trrack can
only guarantee that the undo action will be called properly during the
traversal.
You should be using state actions
exclusively in your application. State
actions allow for faster traversal through the provenance graph.