Returns a new effect instance.
name
string? an effect name (optional, default''
)
// Creates a new effect instance.
const eff = inst();
// Creates a new effect instance with a name.
const fooEff = inst('foo');
// An effect instance is a function returning an object.
// It calls *effect invocation*.
eff(1); // => {eff: inst%0, values: [1]}
Returns function the effect instance
Returns a handler for the given effect.
It is just a shortcut to handlers(vh, {[eff]: effh})
.
eff
Function an effect instancevh
AsyncGeneratorFunction a return value handlereffh
AsyncGeneratorFunction an effect handler
const write = inst();
const handleWrite = handler(
// An effect instance:
// It is target to handle.
write,
// A return value handler:
// It is called when `main` is finished.
async function* (v) {
return v;
},
// An effect handler:
// It is called on each `yield write(...args)` with the continuation from
// here and the given `args`.
async function* (k, ...args) {
console.log(...args);
return yield* k();
},
);
const main = async function* () {
yield write('hello world');
};
execute(handleWrite(main));
// Outputs:
// hello world
Returns AsyncGeeneratorFunction the handlar for the given effect
Returns a handler for the given effects.
vh
AsyncGeneratorFunction a return value handlereffhs
object effect handlers
// Implements `State` monad like effect:
const get = inst();
const put = inst();
const handleState = async function* (init, main) {
const f = yield* handler(
async function* (v) {
return async function* (_) {
return v;
};
},
{
async *[get](k) {
return async function* (s) {
return yield* k(s)(s);
};
},
async *[put](k, s) {
return async function* (_) {
return yield* k()(s);
},
},
},
)(main);
return yield* f(init);
};
const main = async function* () {
yield put(42);
return yield get();
};
execute(handleState(0, main)).then(console.log);
// Outputs:
// 42
Returns AsyncGeneratorFunction the handlar for the given effects
Combines some handlers to one handler.
hs
...AsyncGeneratorFunction effect handlers returned byhandler
orhandlers
function
Returns AsyncGeneratorFunction the combined handler
Runs the given generator and returns its result.
g
AsyncIterator a generator
Returns Promise<any> the generator's result