Skip to content

Latest commit

 

History

History
194 lines (133 loc) · 3.71 KB

API.md

File metadata and controls

194 lines (133 loc) · 3.71 KB

Table of Contents

inst

Returns a new effect instance.

Parameters

  • name string? an effect name (optional, default '')

Examples

// 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

handler

Returns a handler for the given effect.

It is just a shortcut to handlers(vh, {[eff]: effh}).

Parameters

  • eff Function an effect instance
  • vh AsyncGeneratorFunction a return value handler
  • effh AsyncGeneratorFunction an effect handler

Examples

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

handlers

Returns a handler for the given effects.

Parameters

  • vh AsyncGeneratorFunction a return value handler
  • effhs object effect handlers

Examples

// 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

combineHandlers

Combines some handlers to one handler.

Parameters

  • hs ...AsyncGeneratorFunction effect handlers returned by handler or handlers function

Returns AsyncGeneratorFunction the combined handler

execute

Runs the given generator and returns its result.

Parameters

  • g AsyncIterator a generator

Returns Promise<any> the generator's result