@quantajs/core
Version:
A compact, scalable, and developer-friendly state management library designed for any JavaScript environment. It includes a reactivity system that enables efficient and flexible data handling, making complex state management easy.
333 lines (301 loc) • 9.47 kB
TypeScript
export declare type ActionDefinition<S extends object, G extends object, A> = {
[key in keyof A]: (this: StoreInstance<S, G, A>, ...args: any[]) => any;
} & ThisType<S & {
[K in keyof G]: G[K];
} & A>;
/**
* Common migration patterns
*/
export declare const CommonMigrations: {
/**
* Add a new property with default value
*/
addProperty<T extends Record<string, any>, K extends string, V>(property: K, defaultValue: V): MigrationFunction<T & Record<K, V>>;
/**
* Remove a property
*/
removeProperty<T extends Record<string, any>, K extends keyof T>(property: K): MigrationFunction<Omit<T, K>>;
/**
* Rename a property
*/
renameProperty<T extends Record<string, any>, K extends keyof T, N extends string>(oldName: K, newName: N): MigrationFunction<Omit<T, K> & Record<N, T[K]>>;
/**
* Transform a property value
*/
transformProperty<T extends Record<string, any>, K extends keyof T>(property: K, transform: (value: T[K]) => any): MigrationFunction<T>;
};
export declare const computed: <G>(getter: () => G) => {
readonly value: G;
};
export declare const createLogger: (config?: Partial<LoggerConfig>) => Logger;
/**
* Create a migration manager from a configuration object
*/
export declare function createMigrationManager<T = any>(config: Pick<PersistenceConfig<T>, 'migrations' | 'version'>): MigrationManager<T>;
export declare function createPersistenceManager<T extends Record<string, any>>(getState: () => T, setState: (newState: Partial<T>) => void, notifySubscribers: () => void, config: PersistenceConfig<T>, storeName?: string): PersistenceManager;
export declare const createStore: <S extends object, G extends object, A extends Record<string, (...args: any[]) => any>>(name: string, options: StoreOptions<S, G, A>) => StoreInstance<S, G, A>;
export declare type GetterDefinition<S, G> = {
[key in keyof G]: (state: S) => G[key];
};
export declare class IndexedDBAdapter implements PersistenceAdapter {
key: string;
private dbName;
private storeName;
private version;
constructor(key: string, dbName?: string, storeName?: string, version?: number);
read(): Promise<any>;
write(data: any): Promise<void>;
remove(): Promise<void>;
private openDB;
}
export declare class LocalStorageAdapter implements PersistenceAdapter {
key: string;
constructor(key: string);
read(): any;
write(data: any): void;
remove(): void;
subscribe(callback: (data: any) => void): () => void;
}
export declare class Logger {
private config;
private isNode;
private hasConsole;
constructor(config?: Partial<LoggerConfig>);
/**
* Detect if running in Node.js environment
*/
private detectNodeEnvironment;
/**
* Detect if console is available
*/
private detectConsole;
/**
* Get timestamp string
*/
private getTimestamp;
/**
* Get color codes for different log levels (Node.js only)
*/
private getColorCode;
/**
* Reset color code (Node.js only)
*/
private getResetCode;
/**
* Get level name
*/
private getLevelName;
/**
* Format message with prefix, timestamp, and colors
*/
private formatMessage;
/**
* Safe console method execution
*/
private safeConsoleCall;
/**
* Check if logging is enabled for the given level
*/
private shouldLog;
/**
* Debug level logging
*/
debug(message: string, ...args: any[]): void;
/**
* Info level logging (alias for log)
*/
info(message: string, ...args: any[]): void;
/**
* Standard logging
*/
log(message: string, ...args: any[]): void;
/**
* Warning level logging
*/
warn(message: string, ...args: any[]): void;
/**
* Error level logging
*/
error(message: string, ...args: any[]): void;
/**
* Update logger configuration
*/
configure(config: Partial<LoggerConfig>): void;
/**
* Set log level
*/
setLevel(level: LogLevel): void;
/**
* Get current log level
*/
getLevel(): LogLevel;
/**
* Create a child logger with a prefix
*/
child(prefix: string): Logger;
}
export declare const logger: Logger;
declare interface LoggerConfig {
level: LogLevel;
prefix?: string;
timestamp?: boolean;
colors?: boolean;
formatters?: {
[key: string]: (message: string, ...args: any[]) => string;
};
}
/**
* logger utility that works across all JavaScript environments
*/
export declare enum LogLevel {
DEBUG = 0,
INFO = 1,
WARN = 2,
ERROR = 3,
SILENT = 4
}
/**
* Migration definition
*/
declare interface MigrationDefinition<T = any> {
version: number;
migrate: MigrationFunction<T>;
description?: string;
}
/**
* Migration function type
*/
declare type MigrationFunction<T = any> = (data: any) => T;
/**
* Migration manager for handling schema changes
*/
export declare class MigrationManager<T = any> {
private migrations;
constructor(migrations?: MigrationDefinition<T>[]);
/**
* Add a migration function for a specific version
*/
addMigration(version: number, migrate: MigrationFunction<T>): void;
/**
* Remove a migration for a specific version
*/
removeMigration(version: number): void;
/**
* Get all available migration versions
*/
getVersions(): number[];
/**
* Check if a migration exists for a specific version
*/
hasMigration(version: number): boolean;
/**
* Get the highest migration version
*/
getHighestVersion(): number;
/**
* Apply migrations from current version to target version
*/
migrate(data: any, fromVersion: number, toVersion: number): any;
/**
* Validate that all migrations can be applied successfully
*/
validateMigrations(): {
valid: boolean;
errors: string[];
};
}
export declare interface PersistedData<T = any> {
data: T;
version: number;
timestamp: number;
storeName?: string;
checksum?: string;
}
export declare interface PersistenceAdapter {
key: string;
read(): Promise<any> | any;
write(data: any): Promise<void> | void;
remove(): Promise<void> | void;
subscribe?(callback: (data: any) => void): () => void;
}
export declare interface PersistenceConfig<T = any> {
adapter: PersistenceAdapter;
serialize?: (data: T) => string;
deserialize?: (data: string) => T;
migrations?: Record<number, (data: any) => any>;
version?: number;
debounceMs?: number;
include?: Array<keyof T>;
exclude?: Array<keyof T>;
transform?: {
in?: (data: any) => any;
out?: (data: any) => any;
};
onError?: (error: Error, operation: 'read' | 'write' | 'remove' | 'watch-setup') => void;
validator?: (data: any) => boolean;
}
export declare interface PersistenceManager {
save(): Promise<void>;
load(): Promise<void>;
clear(): Promise<void>;
getAdapter(): PersistenceAdapter;
isRehydrated(): boolean;
destroy(): void;
}
export declare const reactive: <S>(target: S) => S;
export declare class SessionStorageAdapter implements PersistenceAdapter {
key: string;
constructor(key: string);
read(): any;
write(data: any): void;
remove(): void;
subscribe(callback: (data: any) => void): () => void;
}
export declare type StateDefinition<S> = () => S;
export declare interface Store<S, G, A> {
state: S;
getters: G;
actions: A;
subscribe: (callback: StoreSubscriber<S>) => () => void;
$reset: () => void;
$persist?: PersistenceManager;
}
export declare type StoreInstance<S extends object, G extends Record<string, any>, A> = S & {
[K in keyof G]: G[K];
} & A & {
state: S;
getters: G;
actions: A;
subscribe: (callback: StoreSubscriber) => () => void;
$reset: () => void;
$persist?: PersistenceManager;
$destroy?: () => void;
};
export declare type StoreOptions<S extends object, G extends object, A> = {
state: StateDefinition<S>;
getters?: GetterDefinition<S, G>;
actions?: ActionDefinition<S, G, A>;
persist?: PersistenceConfig<S>;
} & ThisType<S & {
[K in keyof G]: G[K];
} & A>;
export declare type StoreSubscriber<S = any> = (snapshot?: S) => void;
export declare function useStore<S, G, A>(name: string): Store<S, G, A>;
export declare const watch: <T>(source: () => T, callback: (value: T, oldValue?: T) => void, options?: WatchOptions) => () => void;
/**
* Options for configuring a watcher.
*
*
* @property {boolean} [deep]
* Whether to perform deep watching (i.e., recursively track nested object changes).
* When `true`, changes inside nested structures are detected using polling and JSON diffing.
*
* @property {boolean} [immediate]
* Whether to invoke the watcher callback immediately upon setup,
* rather than waiting for the first change.
*/
declare interface WatchOptions {
deep?: boolean;
immediate?: boolean;
}
export { }