SG
Utility to handle side effects with generators. Inspired by redux-saga and co.
An effect is an object that describe a task, but do not execute it. This allows to separate the tasks scheduling from the tasks themself.
install
npm install
Introduction
The sg function takes a generator yielding effect and returns a function which return a promise.
; { //... ; return 'done';} const func = ; ;
effects
The effects are helper functions which return an object describing what to do.
const effect = ;// effect: type: 'call' handler: // function that will execute the effect: called with arg, and returning a promise args: consolelog 'hello world' // args passed to the effect function
call
Calls a function (normal, async or returning a promise) or a generator yielding effect and either returns the value or throws an error.
const add = a + b;const addPromise = { try ; catch error ; }const addAsync = async await Promise; { const c = ; const d = ; return ;}
thunk
Calls a thunk function and either returns the value or throws an error. A thunk function is a function that returns an asynchronous function taking a callback.
const add = ;
co
Calls a co style generator (yielding promise or thunk, not effect).
{ return Promise;}
cps
Calls a continuation passing style function.
const add = ;
put
emit an event
;
take
Waits for event sent by put and return its payload
const payload = ; // { payload: data }
spawn
Launches another sg generator, but do not wait for it to end, returning a task object.
const task = ;
fork
Same as spawn, but errors from the forked generator will bubble up to the parent generator making it fail. Also the parent generator will wait for the forked generator to end before resolving.
const task = ;
cancel
takes a task returned by fork or spawn, and cancels it, ending it and its children.
;
join
takes a task returned by fork or spawn, and joins it, waiting for the task to end or throw an error.
const result = ;
race
yields several effect simultaneously in a literal and returns only the result of the first completed effect, or its error if it failed.
const user cancel = ;
takeEvery
forks given generator each time given event is triggered. It is the same as forking the following generator:
{ while true const action = ; ; }
task
Object returned by fork and spawn. It has a done and cancel method.
- done: returns a promise, which resolves with task result or rejects with task error.
- cancel: Cancels the task if it is still running. This in turn will also cancel all children of this task.
Adding your own custom effects with createEffect
You can create your own effect with createEffect. It takes a type, and an handler.
- Type: A string with the effect name
- Handler:
a function returning a promise and that receives three arguments:
- effect parameters: an array with the list of argument passed to the effect function
- emitter An event emitter used internally for the take and put effects.
- id The internal id of the current saga. You will probably never need this.
Example of custom effect:
;; { return ;} const sqlEffect = ;
sqlEffect('query', 'parameter');
give
type: 'sql' handle: handleSql args: 'query' 'parameter'
Our sqlEffect can be used in a generator like this.
name: 'john' ;
During execution handleSql will get called like so
;
Injecting Custom EventEmitter
Sg use an eventEmitter internally to handle take and put effects. It is possible to pass your own eventEmitter to sg. This allows to take events from this event emitter. Your event emitter must extends node event emitter.