@broxus/js-core
Version:
MobX-based JavaScript Core library
67 lines (66 loc) • 2.54 kB
TypeScript
import { type CreateObservableOptions } from 'mobx';
import { type ObjectLiteral } from './types';
type MakeObservableOptions = Omit<CreateObservableOptions, 'proxy'>;
export interface AbstractStoreOptions<T, U> extends MakeObservableOptions {
initialData?: Partial<T>;
initialState?: Partial<U>;
}
export declare abstract class AbstractStore<T extends ObjectLiteral = ObjectLiteral, U extends ObjectLiteral = ObjectLiteral> {
/**
* Store data (e.g. user data, account data, form data etc.)
* @protected
*/
protected _data: T;
/**
* Store state (e.g. interface states, notations, errors etc.)
* @protected
*/
protected _state: U;
protected constructor(options?: AbstractStoreOptions<T, U>);
/**
* Set partial data with `key:value` hash or pass a function as
* an argument that takes the argument of the current data object.
* @template K - Keys of data type T
* @param dataOrFn - Partial data object or function returning partial data
*/
setData<K extends keyof T>(dataOrFn: Pick<T, K> | ((prevData: Readonly<T>) => Pick<T, K>)): this;
/**
* Set data by the given key and value.
* @template K - Key of data type T
* @param key - The data key to set
* @param value - The value to set
*/
setData<K extends keyof T>(key: K, value: T[K]): this;
/**
* Set partial state with `key:value` hash or pass a function as
* an argument that takes the argument of the current state object.
* @template K - Keys of state type U
* @param stateOrFn - Partial state object or function returning partial state
*/
setState<K extends keyof U>(stateOrFn: Pick<U, K> | ((prevState: Readonly<U>) => Pick<U, K>)): this;
/**
* Set state by the given key and value.
* @template K - Key of state type U
* @param key - The state key to set
* @param value - The value to set
*/
setState<K extends keyof U>(key: K, value?: U[K]): this;
/**
* Get data as a plain object.
*
* You may pass `true` to get both data and state as a tuple.
*
* @example
* ```typescript
* const store = new MyStore();
* const data = store.toJSON(); // returns data only
* const [data, state] = store.toJSON(true); // returns both data and state
* ```
*
* @param both - If `true`, returns both data and state as a tuple
* @returns Data object or tuple of data and state
*/
toJSON(both: true): [T, U];
toJSON(): T;
}
export {};