@xeito/store
Version:
Reactive Stores for Xeito | Framework for building web applications
56 lines (51 loc) • 1.92 kB
TypeScript
interface Subscription {
unsubscribe(): void;
}
declare class ReadStore<T> {
protected _value: T;
protected _listeners: Set<(value: T) => void>;
protected _callback: (set: (value: any) => void) => () => void;
protected _killCallback: () => void | undefined;
constructor(value?: T, callback?: (set: (value: any) => void) => () => void);
get value(): T;
/**
* Sets the value of the store and calls all the listeners
* @param value The new value
*/
protected set(value: T): void;
/**
* Subscribe to the store
* @param listener A function that takes the current value and returns the new value
* @returns A subscription object with an unsubscribe method
*/
subscribe(listener: (value: T) => void): Subscription;
}
declare class WriteStore<T> extends ReadStore<T> {
constructor(value?: T, callback?: (set: (value: any) => void) => () => void);
/**
* Override the set method to make it public
* @param value The new value
*/
set(value: T): void;
/**
* Update method
* This method takes a callback function that takes the current value and returns the new value
* @param callback A function that takes the current value and returns the new value
*/
update(callback?: (value: T) => T): void;
}
declare class DerivedStore<T> {
private _value;
private _listeners;
private _callback;
private _killCallback;
private _stores;
private _subscriptions;
private _values;
constructor(stores: ReadStore<any> | ReadStore<any>[], callback: (value: any | any[], set: (value?: T) => void) => any | (() => void), initialValue?: T);
get value(): T;
subscribe(listener: (values: any | any[]) => void): Subscription;
private set;
private runCallback;
}
export { DerivedStore, ReadStore, Subscription, WriteStore };