@datorama/akita
Version:
State Management Tailored-Made for JS Applications
128 lines (127 loc) • 2.94 kB
TypeScript
import { StoreCache, UpdateStateCallback } from './types';
import { BehaviorSubject, Observable } from 'rxjs';
import { deepFreeze } from './deepFreeze';
import { StoreConfigOptions, UpdatableStoreConfigOptions } from './storeConfig';
/**
*
* Store for managing any type of data
*
* @example
*
* export interface SessionState {
* token: string;
* userDetails: UserDetails
* }
*
* export function createInitialState(): SessionState {
* return {
* token: '',
* userDetails: null
* };
* }
*
* @StoreConfig({ name: 'session' })
* export class SessionStore extends Store<SessionState> {
* constructor() {
* super(createInitialState());
* }
* }
*/
export declare class Store<S = any> {
protected options: Partial<StoreConfigOptions>;
private store;
private storeValue;
private inTransaction;
private _initialState;
protected cache: StoreCache;
constructor(initialState: Partial<S>, options?: Partial<StoreConfigOptions>);
/**
* Set the loading state
*
* @example
*
* store.setLoading(true)
*
*/
setLoading(loading?: boolean): void;
/**
*
* Set whether the data is cached
*
* @example
*
* store.setHasCache(true)
* store.setHasCache(false)
* store.setHasCache(true, { restartTTL: true })
*
*/
setHasCache(hasCache: boolean, options?: {
restartTTL: boolean;
}): void;
/**
* Set the error state
*
* @example
*
* store.setError({text: 'unable to load data' })
*
*/
setError<T>(error: T): void;
_select<R>(project: (store: S) => R): Observable<R>;
_value(): S;
_cache(): BehaviorSubject<boolean>;
readonly config: StoreConfigOptions;
readonly storeName: string;
readonly deepFreeze: typeof deepFreeze;
readonly cacheConfig: {
ttl: number;
};
readonly resettable: boolean;
_setState(newStateFn: (state: Readonly<S>) => S, _dispatchAction?: boolean): void;
/**
*
* Reset the current store back to the initial value
*
* @example
*
* store.reset()
*
*/
reset(): void;
/**
*
* Update the store's value
*
* @example
*
* this.store.update(state => {
* return {...}
* })
*/
update(stateCallback: UpdateStateCallback<S>): any;
/**
*
* @example
*
* this.store.update({ token: token })
*/
update(state: Partial<S>): any;
updateStoreConfig(newOptions: UpdatableStoreConfigOptions): void;
akitaPreUpdate(_: Readonly<S>, nextState: Readonly<S>): S;
ngOnDestroy(): void;
/**
*
* Destroy the store
*
* @example
*
* store.destroy()
*
*/
destroy(): void;
private onInit;
private dispatch;
private watchTransaction;
private isResettable;
private handleTransaction;
}