57 lines
2.1 KiB
TypeScript
57 lines
2.1 KiB
TypeScript
import { Effects } from '../Effects';
|
|
export declare abstract class Watchable<Raw, Mapped = Raw> {
|
|
readonly effects: Effects;
|
|
protected readonly mapFn: (value: Raw) => Mapped;
|
|
protected readonly eqFn: (a: Mapped, b: Mapped) => boolean;
|
|
constructor(effects: Effects, options?: {
|
|
map?: (value: Raw) => Mapped;
|
|
eq?: (a: Mapped, b: Mapped) => boolean;
|
|
});
|
|
/**
|
|
* Fetch the current value, optionally registering a callback for change notification.
|
|
* The callback should be invoked when the underlying data changes.
|
|
*/
|
|
protected abstract fetch(callback?: () => void): Promise<Raw>;
|
|
protected abstract readonly label: string;
|
|
/**
|
|
* Produce a stream of raw values. Default implementation uses fetch() with
|
|
* effects callback in a loop. Override for custom subscription mechanisms
|
|
* (e.g. fs.watch).
|
|
*/
|
|
protected produce(abort: AbortSignal): AsyncGenerator<Raw, void>;
|
|
/**
|
|
* Lifecycle hook called when const() registers a subscription.
|
|
* Return a cleanup function to be called when the subscription ends.
|
|
* Override for side effects like FileHelper's consts tracking.
|
|
*/
|
|
protected onConstRegistered(_value: Mapped): (() => void) | void;
|
|
/**
|
|
* Internal generator that maps raw values and deduplicates using eq.
|
|
*/
|
|
private watchGen;
|
|
/**
|
|
* Returns the value. Reruns the context from which it has been called if the underlying value changes
|
|
*/
|
|
const(): Promise<Mapped>;
|
|
/**
|
|
* Returns the value. Does nothing if the value changes
|
|
*/
|
|
once(): Promise<Mapped>;
|
|
/**
|
|
* Watches the value. Returns an async iterator that yields whenever the value changes
|
|
*/
|
|
watch(abort?: AbortSignal): AsyncGenerator<Mapped, never, unknown>;
|
|
/**
|
|
* Watches the value. Takes a custom callback function to run whenever the value changes
|
|
*/
|
|
onChange(callback: (value: Mapped | undefined, error?: Error) => {
|
|
cancel: boolean;
|
|
} | Promise<{
|
|
cancel: boolean;
|
|
}>): void;
|
|
/**
|
|
* Watches the value. Returns when the predicate is true
|
|
*/
|
|
waitFor(pred: (value: Mapped) => boolean): Promise<Mapped>;
|
|
}
|