@ainc/onion

Let's do something nice with @ainc/onion!

Usage no npm install needed!

<script type="module">
  import aincOnion from 'https://cdn.skypack.dev/@ainc/onion';
</script>

README

@ainc/onion

Let's do something nice with @ainc/onion!

Install

$ yarn add @ainc/onion

Usage

import { Onion } from '@ainc/onion';

// create onion
const onion = new Onion();

// add `increase` action.
onion.add('increase', ctx => {
    ctx.count ++;
    ctx.next();
});

// add `decrease` action.
onion.add('decrease', ctx => {
    ctx.count --;
    ctx.next();
});

// add common action.
onion.add(ctx => {
    ctx.count >= 3 && ctx.next();
});

// call untyped action, this will output '3'.
onion.call({ count: 3 }, (res) => console.log(res.count));

// call 'increase' action, this will output '4'.
onion.call('increase', { count: 3 }, (res) => console.log(res.count));

// call 'decrease' action, this will no output.
onion.call('decrease', { count: 3 }, (res) => console.log(res.count));

API

  • add(type: string, handler: Handler<T>): () => void;
  • add(handler: Handler<T>): () => void;
  • call(type: string, state?: T, callback?: Callback<T>): void | Promise<void>;
  • call(action?: Action<T>, callback?: Callback<T>): void | Promise<void>;
  • clear(): void;