remove init variable

pull/4668/head
pushkine 6 years ago
parent 9efe5c0364
commit a5d20bfe7c

@ -150,18 +150,15 @@ const single = <T>(store: SingleStore, controller: DeriverController): StartStop
/** derived store StartStopNotifier function when given an array of stores */ /** derived store StartStopNotifier function when given an array of stores */
const multiple = <T>(stores: ArrayStore, controller: DeriverController): StartStopNotifier<T> => set => { const multiple = <T>(stores: ArrayStore, controller: DeriverController): StartStopNotifier<T> => set => {
const values = new Array(stores.length); const values = new Array(stores.length);
let pending = 1 << stores.length;
let inited = false;
let pending = 0;
const sync = () => inited && !pending && controller.update(values, set);
const unsubs = stores.map((store, index) => const unsubs = stores.map((store, index) =>
subscribe( subscribe(
store, store,
value => { value => {
values[index] = value; values[index] = value;
pending &= ~(1 << index); pending &= ~(1 << index);
sync(); if (!pending) controller.update(values, set);
}, },
() => { () => {
pending |= 1 << index; pending |= 1 << index;
@ -169,7 +166,9 @@ const multiple = <T>(stores: ArrayStore, controller: DeriverController): StartSt
) )
); );
(inited = true), sync(); pending &= ~(1 << stores.length);
controller.update(values, set);
return function stop() { return function stop() {
unsubs.forEach(v => v()), controller.cleanup(); unsubs.forEach(v => v()), controller.cleanup();
}; };

Loading…
Cancel
Save