Introduction
alt-mutators
are a simple way to define mutating operations for an API, tied to your alt store. Their interface bear a striking similarity to alt's data async sources, in fact the code is based based on just that.
ALPHA SOFTWARE
Please note that this is an early version of this concept. No stability guaranteed.
Install
npm install --save-dev alt-mutators
Usage
Add the registerMutator
function by decorating your store with the @mutator
function. Then pass it the mutator definition in your constructor:
; const mutatorDefinition = createTodo: // Argument is always an object. State is added by the plugin. { // This method must return a promise. return apiMethod; } writing: TodoActionswritingTodo success: TodoActionswroteTodo error: TodoActionsfailedWritingTodo ; @mutator { this; } { } { } { }} ;
This adds a public method to your alt store called createTodo
. Usage from a view might look something like this:
; Component { TodoStore }
Using the result
The Promise returned by the mutator will resolve with
- The object send to the mutator's write function
- The result of the promise that the mutator returned
So for example if this is the call to a mutator
TodoStore;
Then the mutator will receive
{ return ;}
Then the promise returned from calling the mutator method on the store will resolve to
state: todos: // same object the mutator received, todo: title: "Write better docs" // same object the mutator received, result: id: 1 title: "Write better docs" // whatever the promise returned by the mutator resolves to
With that in mind you can comfortably listen to the promise after calling your store and (preferably) query your store:
TodoStore ;
Feedback
Please leave feedback in an issue or on twitter to @mullsork.