Convert src/runtime/store/index.ts to JavaScript

pull/8569/head
S. Elliott Johnson 3 years ago
parent 9007bfc3a2
commit e77b66ed44

@ -1,89 +1,31 @@
import { import { run_all, subscribe, noop, safe_not_equal, is_function, get_store_value } from '../internal';
run_all,
subscribe,
noop,
safe_not_equal,
is_function,
get_store_value
} from '../internal';
/** Callback to inform of a value updates. */
export type Subscriber<T> = (value: T) => void;
/** Unsubscribes from value updates. */
export type Unsubscriber = () => void;
/** Callback to update a value. */
export type Updater<T> = (value: T) => T;
/** Cleanup logic callback. */
type Invalidator<T> = (value?: T) => void;
/**
* Start and stop notification callbacks.
* This function is called when the first subscriber subscribes.
*
* @param {(value: T) => void} set Function that sets the value of the store.
* @param {(value: Updater<T>) => void} set Function that sets the value of the store after passing the current value to the update function.
* @returns {void | (() => void)} Optionally, a cleanup function that is called when the last remaining
* subscriber unsubscribes.
*/
export type StartStopNotifier<T> = (
set: (value: T) => void,
update: (fn: Updater<T>) => void
) => void | (() => void);
/** Readable interface for subscribing. */
export interface Readable<T> {
/**
* Subscribe on value changes.
* @param run subscription callback
* @param invalidate cleanup callback
*/
subscribe(this: void, run: Subscriber<T>, invalidate?: Invalidator<T>): Unsubscriber;
}
/** Writable interface for both updating and subscribing. */
export interface Writable<T> extends Readable<T> {
/**
* Set value and inform subscribers.
* @param value to set
*/
set(this: void, value: T): void;
/**
* Update value using callback and inform subscribers.
* @param updater callback
*/
update(this: void, updater: Updater<T>): void;
}
/** Pair of subscriber and invalidator. */
type SubscribeInvalidateTuple<T> = [Subscriber<T>, Invalidator<T>];
const subscriber_queue = []; const subscriber_queue = [];
/** /**
* Creates a `Readable` store that allows reading by subscription. * Creates a `Readable` store that allows reading by subscription.
* @param value initial value * @param {T} value initial value
* @param {StartStopNotifier} [start] * @param {StartStopNotifier<T>} start undefined
* @returns {import("/Users/elliottjohnson/dev/sveltejs/svelte/index.ts-to-jsdoc").Readable<T>}
*/ */
export function readable<T>(value?: T, start?: StartStopNotifier<T>): Readable<T> { export function readable(value, start) {
return { return {
subscribe: writable(value, start).subscribe subscribe: writable(value, start).subscribe
}; };
} }
/** /**
* Create a `Writable` store that allows both updating and reading by subscription. * Create a `Writable` store that allows both updating and reading by subscription.
* @param {*=}value initial value * @param {T} value initial value
* @param {StartStopNotifier=} start * @param {StartStopNotifier<T>} start undefined
* @returns {import("/Users/elliottjohnson/dev/sveltejs/svelte/index.ts-to-jsdoc").Writable<T>}
*/ */
export function writable<T>(value?: T, start: StartStopNotifier<T> = noop): Writable<T> { export function writable(value, start = noop) {
let stop: Unsubscriber; /** @type {Unsubscriber} */
const subscribers: Set<SubscribeInvalidateTuple<T>> = new Set(); let stop;
/** @type {Set<SubscribeInvalidateTuple<T>>} */
function set(new_value: T): void { const subscribers = new Set();
/** @param {T} new_value
* @returns {void}
*/
function set(new_value) {
if (safe_not_equal(value, new_value)) { if (safe_not_equal(value, new_value)) {
value = new_value; value = new_value;
if (stop) { if (stop) {
@ -102,19 +44,24 @@ export function writable<T>(value?: T, start: StartStopNotifier<T> = noop): Writ
} }
} }
} }
/** @param {Updater<T>} fn
function update(fn: Updater<T>): void { * @returns {void}
*/
function update(fn) {
set(fn(value)); set(fn(value));
} }
/** @param {Subscriber<T>} run
function subscribe(run: Subscriber<T>, invalidate: Invalidator<T> = noop): Unsubscriber { * @param {Invalidator<T>} invalidate
const subscriber: SubscribeInvalidateTuple<T> = [run, invalidate]; * @returns {import("/Users/elliottjohnson/dev/sveltejs/svelte/index.ts-to-jsdoc").Unsubscriber}
*/
function subscribe(run, invalidate = noop) {
/** @type {SubscribeInvalidateTuple<T>} */
const subscriber = [run, invalidate];
subscribers.add(subscriber); subscribers.add(subscriber);
if (subscribers.size === 1) { if (subscribers.size === 1) {
stop = start(set, update) || noop; stop = start(set, update) || noop;
} }
run(value); run(value);
return () => { return () => {
subscribers.delete(subscriber); subscribers.delete(subscriber);
if (subscribers.size === 0 && stop) { if (subscribers.size === 0 && stop) {
@ -123,80 +70,28 @@ export function writable<T>(value?: T, start: StartStopNotifier<T> = noop): Writ
} }
}; };
} }
return { set, update, subscribe }; return { set, update, subscribe };
} }
/** @param {Stores} stores
/** One or more `Readable`s. */ * @param {Function} fn
type Stores = Readable<any> | [Readable<any>, ...Array<Readable<any>>] | Array<Readable<any>>; * @param {T} initial_value
* @returns {import("/Users/elliottjohnson/dev/sveltejs/svelte/index.ts-to-jsdoc").Readable<T>}
/** One or more values from `Readable` stores. */
type StoresValues<T> = T extends Readable<infer U>
? U
: { [K in keyof T]: T[K] extends Readable<infer U> ? U : never };
/**
* Derived value store by synchronizing one or more readable stores and
* applying an aggregation function over its input values.
*
* @param stores - input stores
* @param fn - function callback that aggregates the values
* @param initial_value - when used asynchronously
*/
export function derived<S extends Stores, T>(
stores: S,
fn: (
values: StoresValues<S>,
set: Subscriber<T>,
update: (fn: Updater<T>) => void
) => Unsubscriber | void,
initial_value?: T
): Readable<T>;
/**
* Derived value store by synchronizing one or more readable stores and
* applying an aggregation function over its input values.
*
* @param stores - input stores
* @param fn - function callback that aggregates the values
* @param initial_value - initial value
*/
export function derived<S extends Stores, T>(
stores: S,
fn: (values: StoresValues<S>) => T,
initial_value?: T
): Readable<T>;
/**
* Derived value store by synchronizing one or more readable stores and
* applying an aggregation function over its input values.
*
* @param stores - input stores
* @param fn - function callback that aggregates the values
*/ */
export function derived<S extends Stores, T>( export function derived(stores, fn, initial_value) {
stores: S,
fn: (values: StoresValues<S>) => T
): Readable<T>;
export function derived<T>(stores: Stores, fn: Function, initial_value?: T): Readable<T> {
const single = !Array.isArray(stores); const single = !Array.isArray(stores);
const stores_array: Array<Readable<any>> = single /** @type {Array<Readable<any>>} */
? [stores as Readable<any>] const stores_array = single
: (stores as Array<Readable<any>>); ? [stores]
: stores;
if (!stores_array.every(Boolean)) { if (!stores_array.every(Boolean)) {
throw new Error('derived() expects stores as input, got a falsy value'); throw new Error('derived() expects stores as input, got a falsy value');
} }
const auto = fn.length < 2; const auto = fn.length < 2;
return readable(initial_value, (set, update) => { return readable(initial_value, (set, update) => {
let started = false; let started = false;
const values = []; const values = [];
let pending = 0; let pending = 0;
let cleanup = noop; let cleanup = noop;
const sync = () => { const sync = () => {
if (pending) { if (pending) {
return; return;
@ -204,31 +99,23 @@ export function derived<T>(stores: Stores, fn: Function, initial_value?: T): Rea
cleanup(); cleanup();
const result = fn(single ? values[0] : values, set, update); const result = fn(single ? values[0] : values, set, update);
if (auto) { if (auto) {
set(result as T); set(result);
} else { }
cleanup = is_function(result) ? (result as Unsubscriber) : noop; else {
cleanup = is_function(result) ? result : noop;
} }
}; };
const unsubscribers = stores_array.map((store, i) => subscribe(store, (value) => {
const unsubscribers = stores_array.map((store, i) =>
subscribe(
store,
(value) => {
values[i] = value; values[i] = value;
pending &= ~(1 << i); pending &= ~(1 << i);
if (started) { if (started) {
sync(); sync();
} }
}, }, () => {
() => {
pending |= 1 << i; pending |= 1 << i;
} }));
)
);
started = true; started = true;
sync(); sync();
return function stop() { return function stop() {
run_all(unsubscribers); run_all(unsubscribers);
cleanup(); cleanup();
@ -239,20 +126,61 @@ export function derived<T>(stores: Stores, fn: Function, initial_value?: T): Rea
}; };
}); });
} }
/** /**
* Takes a store and returns a new one derived from the old one that is readable. * Takes a store and returns a new one derived from the old one that is readable.
* *
* @param store - store to make readonly * @param {Readable<T>} store - store to make readonly
* @returns {import("/Users/elliottjohnson/dev/sveltejs/svelte/index.ts-to-jsdoc").Readable<T>}
*/ */
export function readonly<T>(store: Readable<T>): Readable<T> { export function readonly(store) {
return { return {
subscribe: store.subscribe.bind(store) subscribe: store.subscribe.bind(store)
}; };
} }
/** /**
* Get the current value from a store by subscribing and immediately unsubscribing. * Get the current value from a store by subscribing and immediately unsubscribing.
* @param store readable * @param store readable
*/ */
export { get_store_value as get }; export { get_store_value as get };
/**
* @typedef {(value: T) => void} Subscriber
* @template T
*/
/** @typedef {() => void} Unsubscriber */
/**
* @typedef {(value: T) => T} Updater
* @template T
*/
/**
* @typedef {(value?: T) => void} Invalidator
* @template T
*/
/**
* @typedef {(
* set: (value: T) => void,
* update: (fn: Updater<T>) => void
* ) => void | (() => void)} StartStopNotifier
* @template T
*/
/**
* @typedef {[Subscriber<T>, Invalidator<T>]} SubscribeInvalidateTuple
* @template T
*/
/** @typedef {Readable<any> | [Readable<any>, ...Array<Readable<any>>] | Array<Readable<any>>} Stores */
/**
* @typedef {T extends Readable<infer U>
* ? U
* : { [K in keyof T]: T[K] extends Readable<infer U> ? U : never }} StoresValues
* @template T
*/
/**
* Readable interface for subscribing.
* @typedef {Object} Readable
*/
/**
* Writable interface for both updating and subscribing.
* @typedef {Object} Writable
*/
Loading…
Cancel
Save