uluru
v1.1.1
Published
Immutable single state manager
Downloads
9
Maintainers
Readme
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 }