More correct algorithm

pull/6332/head
subtle-byte 5 years ago
parent 203254b399
commit 53607d57cc

@ -13,7 +13,7 @@ export type Updater<T> = (value: T) => T;
type Invalidator<T> = (value?: T) => void; type Invalidator<T> = (value?: T) => void;
/** Start and stop notification callbacks. */ /** Start and stop notification callbacks. */
export type StartStopNotifier<T> = (set: Subscriber<T>, invalidate: Invalidator<T>) => Unsubscriber | void; export type StartStopNotifier<T> = (set: Subscriber<T>, invalidate: Invalidator<T>, revalidate: Revalidator) => Unsubscriber | void;
/** Readable interface for subscribing. */ /** Readable interface for subscribing. */
export interface Readable<T> { export interface Readable<T> {
@ -106,7 +106,7 @@ export function writable<T>(value?: T, start: StartStopNotifier<T> = noop): Writ
const subscription: Subscription<T> = [run, invalidate, revalidate]; const subscription: Subscription<T> = [run, invalidate, revalidate];
subscriptions.push(subscription); subscriptions.push(subscription);
if (subscriptions.length === 1) { if (subscriptions.length === 1) {
stop = start(set, invalidate) || noop; stop = start(set, invalidate, revalidate) || noop;
} }
run(value); run(value);
@ -180,7 +180,7 @@ export function derived<T>(stores: Stores, fn: Function, initial_value?: T): Rea
const auto = fn.length < 2; const auto = fn.length < 2;
return readable(initial_value, (set, invalidate) => { return readable(initial_value, (set, invalidate, revalidate) => {
let inited = false; let inited = false;
const values = []; const values = [];
@ -210,13 +210,18 @@ export function derived<T>(stores: Stores, fn: Function, initial_value?: T): Rea
} }
}, },
() => { () => {
const invalidated = pending & (1 << i); const invalidated = pending;
pending |= (1 << i); pending |= (1 << i);
if (!invalidated) { if (!invalidated) {
invalidate(); invalidate();
} }
}, },
() => pending &= ~(1 << i) () => {
pending &= ~(1 << i)
if (!pending) {
revalidate();
}
},
)); ));
inited = true; inited = true;

Loading…
Cancel
Save