sead
Fork of co that describes asynchronous tasks as data.
Why?
Instead of a generator activating side-effects it instead yields data objects
that represent how side-effects ought to be executed. This pushes side-effects
to co
instead of the application itself.
Effectively this makes testing side-effects as easy as checking that each step in a generator returns the proper data structure.
This library was inspired by redux-saga
and re-frame. Whenever I left the world
of redux-saga
and wanted to test my async/await/generator functions it would require
mocking/intercepting HTTP requests which is a terrible developer experience after
coming from describing side-effects as data.
Effects as Data talk by Richard Feldman
How?
sead
will work exactly like co
with the exception that it can handle a new
yielded value type: effect objects. An effect object looks something like this:
task
is an alias for the co
function.
; { const resp = ; // sending an array makes `call` activate the function `json` on `resp` object // this is required because of the way fetch uses context to determine if the Body // promise has been used already. const data = ; return ...data extra: 'stuff' ;} ;
Check out the API section for more effects.
Testing
Taking the previous example, this is how you would test it:
const test = ; ;
Using a little helper library called gen-tester we can make this even easier.
const genTester = ; ;
Take a close look here. When the generator function does not get called by task
all it does is return JSON at every yield
. This is the brilliance of describing
side-effects as data: we can test our generator function synchronously, without
needing any HTTP interceptors or mocking functions! So even though at every yield
this library will make asynchronous calls, for testing, we can step through the
generator one step after another and make sure the yield makes the correct call.
API
task
Manages async flow for a generator. This is an alias to the co
function.
call
const task call = ;const fetch = ; { return 'hi';} ;
all
Uses Promise.all
to execute effects in parallel. Could be an array of effects
or an object of effects.
const task call all = ;const fetch = ; { const resp = ; const data = ; return data;} ;
spawn
Spawns an effect without the generator waiting for that effect to finish.
const task spawn = ;const fetch = ; { return { ; };} { ; console;} ;// COOL// ... five seconds later// ACTIVATE
delay
This will sleep
the generator for the designated amount of time.
const task delay = ; { console ; console;} ;// INIT// ... one second later// END
factory
This is what creates task
. This allows end-developers to build their own
effect middleware. When using middleware it must return a promise, something that co
understands how to handle, and
to allow other middleware to handle the effect as well, you
must return next(effect)
;
const factory = ; const ERROR = 'ERROR';const error = type: ERROR msg ;const middleware = { if effecttype === ERROR return Promise; return ;}; { ;} const customTask = ;; // ERROR: SOMETHING HAPPENED