Update index.ts

pull/6167/head
Rémi Marche 5 years ago committed by GitHub
parent 65e65154a7
commit f514b4d2d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -57,32 +57,12 @@ const subscriber_queue = [];
*/ */
export function readable<T>(value: T, start: StartStopNotifier<T>): Readable<T> { export function readable<T>(value: T, start: StartStopNotifier<T>): Readable<T> {
function subscribe(run: Subscriber<T>, invalidate: Invalidator<T> = noop): Unsubscriber {
const subscriber: SubscribeInvalidateTuple<T> = [run, invalidate];
subscribers.push(subscriber);
if (subscribers.length === 1) {
stop = start(set) || noop;
}
run(value);
return () => {
const index = subscribers.indexOf(subscriber);
if (index !== -1) {
subscribers.splice(index, 1);
}
if (subscribers.length === 0) {
stop();
stop = null;
}
};
}
function get(): T { function get(): T {
return value; return value;
} }
return { return {
subscribe subscribe: writable(value, start).subscribe
}; };
} }
@ -119,7 +99,27 @@ export function writable<T>(value: T, start: StartStopNotifier<T> = noop): Writa
set(fn(value)); set(fn(value));
} }
return { set, update, subscribe: readable(value, start).subscribe }; function subscribe(run: Subscriber<T>, invalidate: Invalidator<T> = noop): Unsubscriber {
const subscriber: SubscribeInvalidateTuple<T> = [run, invalidate];
subscribers.push(subscriber);
if (subscribers.length === 1) {
stop = start(set) || noop;
}
run(value);
return () => {
const index = subscribers.indexOf(subscriber);
if (index !== -1) {
subscribers.splice(index, 1);
}
if (subscribers.length === 0) {
stop();
stop = null;
}
};
}
return { set, update, subscribe };
} }
/** One or more `Readable`s. */ /** One or more `Readable`s. */

Loading…
Cancel
Save