state-management-utilities
Version:
State management utilities
205 lines (204 loc) • 5.95 kB
TypeScript
import type { StateManager } from "./state-manager";
/**
* The `StateManagerCenter` class is a singleton that manages multiple state managers,
* logs state changes, and provides methods for state hydration and dehydration.
*/
declare class StateManagerCenter {
protected _cacheRefs: Record<string, CacheRef>;
/**
* A record of state managers identified by unique IDs.
*/
protected _stateManagers: Record<string, StateManager<any>>;
/**
* An array of state manager center records.
*/
protected _records: CenterRecordType[];
/**
* A record of the current states identified by unique IDs.
*/
protected _currentStates: Record<string, any>;
/**
* A flag indicating whether logging is enabled.
*/
protected _isEnabledLog: boolean;
/**
* A flag indicating whether cloning is disabled.
*/
protected _disableCloning: boolean;
/**
* An optional callback function for logging.
*/
protected _onLog: ((props: {
uid: string;
state: any;
}) => Promise<void>) | undefined;
/**
* A flag indicating whether hydration is in progress.
*/
protected _isHydration: boolean;
/**
* Gets the hydration status.
*/
get isHydration(): boolean;
/**
* The singleton instance of `StateManagerCenter`.
*/
private static _instance;
/**
* Private constructor to prevent direct instantiation.
*/
private constructor();
/**
* Gets the cloning disable status.
*/
get disableCloning(): boolean;
/**
* Sets the cloning disable status.
*/
set disableCloning(value: boolean);
/**
* Gets the singleton instance of `StateManagerCenter`.
*/
static get instance(): StateManagerCenter;
/**
* Gets the current states.
*/
get currentStates(): Record<string, any>;
/**
* Gets the logging enable status.
*/
get enableLog(): boolean;
/**
* Sets the logging enable status.
*/
set enableLog(value: boolean);
/**
* Returns the state manager center records.
* These records can be sent to the server for monitoring and debugging purposes.
*
* @returns The state manager center records.
*/
get records(): CenterRecordType[];
/**
* Sets the current records to the provided records.
* This method can be used for monitoring and debugging purposes.
*
* @param records - The records to be set.
*/
set records(records: CenterRecordType[]);
/**
* Gets the state manager center records in reverse order.
*/
getReverseRecords(): Promise<CenterRecordType[]>;
/**
* Logs a state change.
*/
_log({ uid, state }: {
uid: string;
state: any;
}): this;
/**
* Registers a state manager with a unique ID.
*/
_register({ uid, stateManager, }: {
uid: string;
stateManager: StateManager<any>;
}): this;
/**
* Applies the given states to the related state managers.
*
* @param {CenterRecordType} record - An object containing the states to be applied.
* @returns {this} The current instance of the class.
*/
apply({ states }: CenterRecordType): this;
/**
* Initializes the hydration process.
*/
protected _initializeHydration(): this;
/**
* Hydrates the state managers with the provided entities.
* @param entities - The entities to be hydrated.
* @returns The hydrated states.
* @throws An error if the entity does not have a `hydrated` property.
*/
get hydrated(): {
generate: (...entries: HydratedEntries) => Promise<{
hydrated: Hydrated;
values: any[];
}>;
config: ({ initial }: {
initial: Hydrated;
}) => {
generate: (...entries: HydratedEntries) => Promise<{
hydrated: Hydrated;
values: any[];
}>;
};
};
protected _generateHydrated({ entries, initial, }: {
initial?: Hydrated;
entries: HydratedEntries;
}): Promise<{
hydrated: Hydrated;
values: any[];
}>;
_registerCacheRef({ uid, ref }: {
uid: string;
ref: any;
}): this;
/**
* Dehydrates the state managers with the provided states.
* @param states - The states to be dehydrated.
*/
dehydrate(hydrated: Hydrated): Promise<void>;
protected _updateCache({ value, hash, cacheRef, timestamp, }: HydratedItem): Promise<void>;
/**
* Registers a callback function for logging.
* If a callback is already registered, an error will be thrown.
* @param callback - The callback function to be registered.
* @returns The `StateManagerCenter` instance.
* @throws An error if a callback is already registered.
*/
onLog(callback: ((props: {
uid: string;
state: any;
}) => Promise<void>) | undefined): StateManagerCenter;
/**
* Clears the state manager center records.
*/
clearRecords(): this;
}
export declare const center: StateManagerCenter;
export type CenterRecordType = {
updatedUID: string;
timestamp: number;
states: Record<string, any>;
number: number;
};
export type HydratedEntries = (Promise<HydratedEntry> | HydratedEntry)[];
export type HydratedEntry = {
update: (record: Hydrated["data"]) => void;
value: any;
};
export type Hydrated = {
id: string;
data: Record<string, HydratedItem>;
timestamp: number;
};
export type HydratedItem = {
value: any;
hash?: string;
cacheRef?: string;
timestamp?: number;
};
export interface CacheRef {
_getCache(cacheKey: string): Promise<{
data: any;
updatedAt: number;
} | undefined>;
_setCache(cacheKey: string, value: {
data: any;
updatedAt: number;
}): void;
}
export {};