UNPKG

nano-wallet-js-test-1

Version:

SDK for developers to create and interact with Nanocurrency wallet easily

103 lines (102 loc) 3.1 kB
/** * State change callbacks */ export type Listener<T> = (state: T, resetting?: boolean) => Promise<void>; /** * @type BaseConfig * * Base controller configuration * @property disabled - Determines if this controller is enabled */ export interface BaseConfig { } /** * @type BaseState * * Base state representation * @property name - Unique name for this controller */ export interface BaseState { } /** * Controller class that provides configuration, state management, and subscriptions */ export default class BaseController<C extends BaseConfig, S extends BaseState> { /** * Default options used to configure this controller */ defaultConfig: C; /** * Default state set on this controller */ defaultState: S; private readonly initialConfig; private readonly initialState; private internalConfig; private internalState; private internalListeners; /** * Creates a BaseController instance. Both initial state and initial * configuration options are merged with defaults upon initialization. * * @param config - Initial options used to configure this controller. * @param state - Initial state to set on this controller. */ constructor(config?: Partial<C>, state?: Partial<S>); /** * Enables the controller. This sets each config option as a member * variable on this instance and triggers any defined setters. This * also sets initial state and triggers any listeners. * * @returns This controller instance. */ protected initialize(): this; /** * Retrieves current controller configuration options. * * @returns The current configuration. */ get config(): C; /** * Retrieves current controller state. * * @returns The current state. */ get state(): S; /** * Updates controller configuration. * * @param config - New configuration options. * @param overwrite - Overwrite config instead of merging. * @param fullUpdate - Boolean that defines if the update is partial or not. */ configure(config: Partial<C>, overwrite?: boolean, fullUpdate?: boolean): void; /** * Notifies all subscribed listeners of current modified state. */ notify(state: S, resetting?: boolean): Promise<void>; /** * Adds new listener to be notified of state changes. * * @param listener - The callback triggered when state changes. */ subscribe(listener: Listener<S>): void; /** * Removes existing listener from receiving state changes. * * @param listener - The callback to remove. * @returns `true` if a listener is found and unsubscribed. */ unsubscribe(listener: Listener<S>): boolean; /** * Updates controller state. * * @param state - The new state. * @param overwrite - Overwrite state instead of merging. */ update(state: Partial<S>, overwrite?: boolean, resetting?: boolean): Promise<void>; /** * Resets controller state to default. */ reset(): void; }