pull/2955/head
Richard Harris 5 years ago
parent ed2a19aa67
commit 0d31e5c046

@ -10,7 +10,7 @@ type Unsubscriber = () => void;
type Updater<T> = (value: T) => T; type Updater<T> = (value: T) => T;
/** Cleanup logic callback. */ /** Cleanup logic callback. */
type Invalidater<T> = (value?: T) => void; type Invalidator<T> = (value?: T) => void;
/** Start and stop notification callbacks. */ /** Start and stop notification callbacks. */
type StartStopNotifier<T> = (set: Subscriber<T>) => Unsubscriber | void; type StartStopNotifier<T> = (set: Subscriber<T>) => Unsubscriber | void;
@ -22,7 +22,7 @@ export interface Readable<T> {
* @param run subscription callback * @param run subscription callback
* @param invalidate cleanup callback * @param invalidate cleanup callback
*/ */
subscribe(run: Subscriber<T>, invalidate?: Invalidater<T>): Unsubscriber; subscribe(run: Subscriber<T>, invalidate?: Invalidator<T>): Unsubscriber;
} }
/** Writable interface for both updating and subscribing. */ /** Writable interface for both updating and subscribing. */
@ -41,7 +41,7 @@ export interface Writable<T> extends Readable<T> {
} }
/** Pair of subscriber and invalidator. */ /** Pair of subscriber and invalidator. */
type SubscribeInvalidateTuple<T> = [Subscriber<T>, Invalidater<T>]; type SubscribeInvalidateTuple<T> = [Subscriber<T>, Invalidator<T>];
/** /**
* Creates a `Readable` store that allows reading by subscription. * Creates a `Readable` store that allows reading by subscription.
@ -78,7 +78,7 @@ export function writable<T>(value: T, start: StartStopNotifier<T> = noop): Writa
set(fn(value)); set(fn(value));
} }
function subscribe(run: Subscriber<T>, invalidate: Invalidater<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];
subscribers.push(subscriber); subscribers.push(subscriber);
if (subscribers.length === 1) { if (subscribers.length === 1) {
@ -127,7 +127,7 @@ export function derived<T, S extends Stores>(
const auto = fn.length < 2; const auto = fn.length < 2;
const invalidators: Array<Invalidater<T>> = []; const invalidators: Array<Invalidator<T>> = [];
const store = readable(initial_value, (set) => { const store = readable(initial_value, (set) => {
let inited = false; let inited = false;
@ -173,7 +173,7 @@ export function derived<T, S extends Stores>(
}); });
return { return {
subscribe(run: Subscriber<T>, invalidate: Invalidater<T> = noop): Unsubscriber { subscribe(run: Subscriber<T>, invalidate: Invalidator<T> = noop): Unsubscriber {
invalidators.push(invalidate); invalidators.push(invalidate);
const unsubscribe = store.subscribe(run, invalidate); const unsubscribe = store.subscribe(run, invalidate);

Loading…
Cancel
Save