nrgy
Version:
The library for reactive programming using efficient computing and MVC/MVVM patterns
164 lines (157 loc) • 4.48 kB
TypeScript
import { A as AtomOptions, W as WritableAtom } from './atom-DIp83xZa.js';
import { S as Signal, A as Atom } from './types-CxOJPpKX.js';
/**
* Options passed to the `signal` creation function.
*/
type SignalOptions<TEvent> = {
/**
* Signal's name
*/
name?: string;
/**
* If true, the signal forces usage of "sync" scheduler.
*/
sync?: boolean;
/**
* Callback is called at the same time when the signal is called
*/
onEvent?: (event: TEvent) => void;
/**
* Callback is called when an effect is subscribed.
*/
onSubscribe?: () => void;
/**
* Callback is called when an effect is unsubscribed.
*/
onUnsubscribe?: (isEmpty: boolean) => void;
/**
* Callback is called when the signal is destroyed.
*/
onDestroy?: () => void;
};
/**
* Factory to create `Signal`
*/
interface SignalFn {
<T = void>(options?: SignalOptions<T>): Signal<T>;
}
/**
* Factory to create a writable `Atom` that can be set or updated directly.
*/
interface AtomFn {
<T>(initialValue: T, options?: AtomOptions<T>): WritableAtom<T>;
}
/**
* Type for an array of atoms
*/
type AtomList<TValues extends unknown[]> = [
...{
[K in keyof TValues]: Atom<TValues[K]>;
}
];
/**
* A reactive effect, which can be manually destroyed.
*/
type EffectSubscription<R> = Readonly<{
/**
* Signal that emits the result of the effect.
*/
onResult: Signal<R>;
/**
* Signal that emits the error of the effect.
*/
onError: Signal<unknown>;
/**
* Signal that emits when the effect is destroyed.
*/
onDestroy: Signal<void>;
/**
* Shut down the effect, removing it from any upcoming scheduled executions.
*/
destroy(): void;
}>;
type EffectContext = {
cleanup(callback: () => void): void;
};
type EffectAction<T, R> = (value: T, context: EffectContext) => R | Promise<R>;
/**
* Options for an effect
*/
type EffectOptions = {
sync?: boolean;
};
/**
* An effect function
*/
interface EffectFn {
/**
* Creates a new effect for a signal
*/ <T, R>(source: Signal<T>, action: EffectAction<T, R>, options?: EffectOptions): EffectSubscription<R>;
/**
* Creates a new effect for an atom
*/ <T, R>(source: Atom<T>, action: EffectAction<T, R>, options?: EffectOptions): EffectSubscription<R>;
/**
* Creates a new effect for a list of atoms
*/ <TValues extends unknown[], R>(sources: AtomList<TValues>, action: EffectAction<TValues, R>, options?: EffectOptions): EffectSubscription<R>;
}
/**
* An object which can be unsubscribed from
*/
interface Unsubscribable {
unsubscribe(): void;
}
/**
* An object which can be destroyed
*/
interface Destroyable {
destroy(): void;
}
/**
* A resource which can be unsubscribed from or destroyed
*/
type ScopeTeardown = Unsubscribable | Destroyable | (() => unknown);
/**
* A boundary for effects and business logic.
*
* `Scope` collects all subscriptions which are made by child entities and provides
* `destroy()` method to unsubscribe from them.
*/
interface Scope extends Destroyable {
/**
* Registers a callback or unsubscribable resource which will be called when `destroy()` is called
*/
onDestroy: (teardown: ScopeTeardown) => void;
/**
* Registers an unsubscribable resource which will be called when `destroy()` is called
*/
add: <T extends Unsubscribable | Destroyable>(resource: T) => T;
/**
* Destroys the scope
*/
destroy: () => void;
/**
* Creates a child scope
*/
createScope: () => Scope;
/**
* Creates a new atom and registers it for later disposal
*/
atom: AtomFn;
/**
* Creates a new signal and registers it for later disposal
*/
signal: SignalFn;
/**
* Creates a new effect and registers it for later disposal
*/
effect: EffectFn;
/**
* Creates a new sync effect and registers it for later disposal
*/
syncEffect: EffectFn;
}
/**
* `SharedScope` and `Scope` types allow to distinct which third-party code can invoke `destroy()` method.
*/
type SharedScope = Omit<Scope, 'destroy'>;
export type { AtomFn as A, Destroyable as D, EffectFn as E, SignalFn as S, Unsubscribable as U, Scope as a, AtomList as b, SignalOptions as c, EffectAction as d, EffectContext as e, EffectOptions as f, EffectSubscription as g, ScopeTeardown as h, SharedScope as i };