pull/4668/head
pushkin 6 years ago committed by GitHub
parent 0d263cc1ea
commit def8713b57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -66,46 +66,37 @@ export function writable<T>(value: T, start: StartStopNotifier<T> = noop): Writa
const subscribers: Array<SubscribeInvalidateTuple<T>> = []; const subscribers: Array<SubscribeInvalidateTuple<T>> = [];
function set(new_value: T): void { function set(new_value: T): void {
if (safe_not_equal(value, new_value)) { if (!safe_not_equal(value, new_value)) return;
value = new_value; value = new_value;
if (stop) { // store is ready if (!stop) return;
const run_queue = !subscriber_queue.length; const run_queue = !subscriber_queue.length;
for (let i = 0; i < subscribers.length; i += 1) { for (let i = 0, s; i < subscribers.length; i++) {
const s = subscribers[i]; (s = subscribers[i])[1]();
s[1]();
subscriber_queue.push(s, value); subscriber_queue.push(s, value);
} }
if (run_queue) { if (!run_queue) return;
for (let i = 0; i < subscriber_queue.length; i += 2) { for (let i = 0; i < subscriber_queue.length; i++) {
subscriber_queue[i][0](subscriber_queue[i + 1]); subscriber_queue[i][0](subscriber_queue[++i]);
} }
subscriber_queue.length = 0; subscriber_queue.length = 0;
} }
}
}
}
function update(fn: Updater<T>): void { function update(fn: Updater<T>): void {
set(fn(value)); set(fn(value));
} }
function subscribe(run: Subscriber<T>, invalidate: Invalidator<T> = noop): Unsubscriber { function subscribe(
run: Subscriber<T>,
invalidate: Invalidator<T> = noop
): Unsubscriber {
const subscriber: SubscribeInvalidateTuple<T> = [run, invalidate]; const subscriber: SubscribeInvalidateTuple<T> = [run, invalidate];
if (!subscribers.length) stop = start(set) || noop;
subscribers.push(subscriber); subscribers.push(subscriber);
if (subscribers.length === 1) {
stop = start(set) || noop;
}
run(value); run(value);
return () => { return () => {
const index = subscribers.indexOf(subscriber); const index = subscribers.indexOf(subscriber);
if (index !== -1) { if (~index) subscribers.splice(index, 1);
subscribers.splice(index, 1); if (!subscribers.length) stop(), (stop = null);
}
if (subscribers.length === 0) {
stop();
stop = null;
}
}; };
} }
@ -161,9 +152,7 @@ export function derived<T>(stores: Stores, fn: Function, initial_value?: T): Rea
let cleanup = noop; let cleanup = noop;
const sync = () => { const sync = () => {
if (pending) { if (pending) return;
return;
}
cleanup(); cleanup();
const result = fn(single ? values[0] : values, set); const result = fn(single ? values[0] : values, set);
if (auto) { if (auto) {
@ -173,18 +162,18 @@ export function derived<T>(stores: Stores, fn: Function, initial_value?: T): Rea
} }
}; };
const unsubscribers = stores_array.map((store, i) => subscribe( const unsubscribers = stores_array.map((store, i) =>
subscribe(
store, store,
(value) => { (value) => {
values[i] = value; values[i] = value;
pending &= ~(1 << i); pending &= ~(1 << i);
if (inited) { if (inited) sync();
sync();
}
}, },
() => { () => {
pending |= (1 << i); pending |= 1 << i;
}), }
)
); );
inited = true; inited = true;

Loading…
Cancel
Save