UNPKG

@ztimson/momentum

Version:

Client library for momentum

92 lines 3.17 kB
import { Cache, CacheOptions, PathEvent, PathEventEmitter } from '@ztimson/utils'; import { Meta, TreeNode, Unsubscribe } from './core'; import { Momentum } from './momentum'; export type ControllerOptions<T> = any & { module: string; key?: keyof T; storage?: null | keyof CacheOptions['persistentStorage']; path?: string; }; /** Boilerplate for connecting to API controllers */ export declare class AssetController<T extends Meta> extends PathEventEmitter { protected momentum: Momentum; protected readonly subscribers: { [key: string]: Unsubscribe; }; cache: Cache<string, T>; opts: ControllerOptions<T>; constructor(momentum: Momentum, opts: ControllerOptions<T>); /** * Process raw data before caching & returning * @hidden */ protected afterRead(value: any): T; /** * Process raw data before sending * @hidden */ protected beforeWrite(value: any): T; /** * Fetch all assets as array * @param {boolean} reload Force reload instead of using cache * @return {Promise<T[]>} All assets as array */ all(reload?: boolean): Promise<T[]>; /** * Create new asset * @param {T} value Asset that will be created * @return {Promise<T>} Saved result */ create(value: T): Promise<T>; /** * Delete asset * @param {string} key Asset primary key * @return {Promise<number>} Returns on success */ delete(key?: string): Promise<number>; /** * Read single asset * @param {string} key Asset primary key * @param {boolean} reload Force reload instead of using cache * @return {Promise<T>} Discovered asset */ read(key: string, reload?: boolean): Promise<T>; /** * Update exiting asset * @param {T} value Asset that will be updated * @return {Promise<T>} Saved result */ update(value: Partial<T>): Promise<T>; /** * Subscribe to live updates with callback * @param {(value: T[]) => any | null} callback Received changes * @param opts Additional options: path - scope to events within path, reload - Re-fetch even if cached * @return {() => void} Function to unsubscribe */ sync(callback?: ((event: PathEvent, value: any) => any) | null, opts?: { path?: string; reload?: boolean; }): Unsubscribe; } export declare class TreeAssetController<T extends Meta & { path: string; }> extends AssetController<T> { /** * Get all schemas organized into a map * @param {boolean} reload Reload instead of using cache * @return {Promise<TreeNode<Schema>[]>} Schemas as a map */ map(reload?: boolean): Promise<TreeNode<T>[]>; /** * Subscribe to live updates with optional callback * @param {(schema: Schema[]) => any} callback Receives latest data * @param opts path - scope events, tree - return as a map or array * @return {() => void} */ sync(callback?: (event: PathEvent, value: any) => any, opts?: { path?: string; map?: boolean; reload?: boolean; }): Unsubscribe; } //# sourceMappingURL=asset-controller.d.ts.map