@ordojs/core
Version:
Core compiler and runtime for OrdoJS framework
169 lines • 4.74 kB
TypeScript
/**
* @fileoverview OrdoJS Reactivity System - Signal-based reactive state management
* @author OrdoJS Framework Team
*/
/**
* Cleanup function type
*/
export type EffectCleanup = () => void;
/**
* Unsubscribe function type
*/
export type Unsubscribe = () => void;
/**
* Signal interface for reactive values
*/
export interface Signal<T> {
/** Current value */
readonly value: T;
/** Subscribe to value changes */
subscribe(callback: (value: T) => void): Unsubscribe;
/** Update value with new value */
set(value: T): void;
/** Update value with updater function */
update(updater: (current: T) => T): void;
/** Get current value (same as .value) */
get(): T;
/** Check if signal has subscribers */
hasSubscribers(): boolean;
}
/**
* Computed signal interface for derived values
*/
export interface ComputedSignal<T> extends Omit<Signal<T>, 'set' | 'update'> {
/** Recompute the value */
recompute(): void;
/** Get dependencies */
getDependencies(): Signal<any>[];
}
/**
* Effect options
*/
export interface EffectOptions {
/** Run effect immediately */
immediate?: boolean;
/** Effect name for debugging */
name?: string;
/** Cleanup function */
onCleanup?: () => void;
}
/**
* Batch options
*/
export interface BatchOptions {
/** Batch name for debugging */
name?: string;
/** Priority level */
priority?: 'low' | 'normal' | 'high';
}
/**
* Global reactivity context
*/
interface ReactivityContext {
/** Currently running effect */
currentEffect: EffectInstance | null;
/** Effect stack for nested effects */
effectStack: EffectInstance[];
/** Batch update queue */
batchQueue: Set<Signal<any>>;
/** Is currently batching */
isBatching: boolean;
/** Scheduled batch flush */
batchFlushScheduled: boolean;
}
/**
* Effect instance
*/
interface EffectInstance {
/** Effect function */
fn: () => void;
/** Dependencies */
dependencies: Set<Signal<any>>;
/** Cleanup function */
cleanup?: () => void;
/** Effect options */
options: EffectOptions;
/** Is active */
active: boolean;
}
/**
* Create a reactive signal
*/
export declare function signal<T>(initialValue: T): Signal<T>;
/**
* Create a computed signal
*/
export declare function computed<T>(computeFn: () => T): ComputedSignal<T>;
/**
* Create an effect that runs when dependencies change
*/
export declare function effect(fn: () => void, options?: EffectOptions): EffectCleanup;
/**
* Batch multiple updates together
*/
export declare function batch<T>(fn: () => T, options?: BatchOptions): T;
/**
* Create a derived signal that depends on other signals
*/
export declare function derived<T>(fn: () => T): ComputedSignal<T>;
/**
* Create a writable derived signal
*/
export declare function writableDerived<T>(getter: () => T, setter: (value: T) => void): Signal<T>;
/**
* Create a signal that persists to localStorage
*/
export declare function persistentSignal<T>(key: string, initialValue: T, storage?: Storage): Signal<T>;
/**
* Create a debounced signal
*/
export declare function debouncedSignal<T>(source: Signal<T>, delay: number): Signal<T>;
/**
* Create a throttled signal
*/
export declare function throttledSignal<T>(source: Signal<T>, delay: number): Signal<T>;
/**
* Combine multiple signals into one
*/
export declare function combineSignals<T extends readonly Signal<any>[]>(signals: T): ComputedSignal<{
[K in keyof T]: T[K] extends Signal<infer U> ? U : never;
}>;
/**
* Create a signal from a promise
*/
export declare function fromPromise<T>(promise: Promise<T>, initialValue?: T): Signal<{
loading: boolean;
data?: T;
error?: Error;
}>;
/**
* Create a signal from an event target
*/
export declare function fromEvent<T = Event>(target: EventTarget, eventName: string, options?: AddEventListenerOptions): Signal<T | null>;
/**
* Reactivity utilities
*/
export declare const reactivity: {
signal: typeof signal;
computed: typeof computed;
effect: typeof effect;
batch: typeof batch;
derived: typeof derived;
writableDerived: typeof writableDerived;
persistentSignal: typeof persistentSignal;
debouncedSignal: typeof debouncedSignal;
throttledSignal: typeof throttledSignal;
combineSignals: typeof combineSignals;
fromPromise: typeof fromPromise;
fromEvent: typeof fromEvent;
};
/**
* Get current reactivity context (for debugging)
*/
export declare function getReactivityContext(): Readonly<ReactivityContext>;
/**
* Reset reactivity context (for testing)
*/
export declare function resetReactivityContext(): void;
export {};
//# sourceMappingURL=reactivity.d.ts.map