uluru
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

Quick Example

interface State {
    count: number;
}
interface Actions {
    "increment": void;
    "add": number;
    "delay-add": { delay: number, n: number };
}
let state: State = {
    count: 0,
};

const dispatcher = new Uluru.Dispatcher<Actions, State>(reduce => {
    const reduced = reduce(state);
    console.log(reduced);
    state = reduced;
});

dispatcher.subscribe("increment")(apply => () => {
    apply(prev => ({ ...prev, count: prev.count + 1 }));
});
dispatcher.subscribe("add")(apply => n => {
    apply(prev => ({ ...prev, count: prev.count + n }));
});
dispatcher.subscribe("delay-add")(apply => payload => {
    setTimeout(() => {
        apply(prev => ({ ...prev, count: prev.count + payload.n }));        
    }, payload.delay);
});

dispatcher.dispatch("delay-add")({ delay: 2000, n: 100 });
dispatcher.dispatch("increment")();
// { count: 1 }
dispatcher.dispatch("delay-add")({ delay: 1000, n: 20 });
dispatcher.dispatch("add")(5);
// { count: 6 }
// { count: 26 }
// { count: 126 }

Package Sidebar

Install

npm i uluru

Weekly Downloads

9

Version

1.1.1

License

Unlicense

Last publish

Collaborators

  • siritori