@cdellacqua/signals
Version:
A simple signal pattern implementation that enables reactive programming
38 lines (37 loc) • 1.48 kB
TypeScript
import { ReadonlySignal } from './index';
/**
* Create a signal that emits whenever the passed signal emits. The original
* emitted value gets transformed by the passed function and the result gets
* emitted.
*
* Example:
* ```ts
* const signal$ = makeSignal<number>();
* const derived$ = deriveSignal(signal$, (n) => n + 100);
* derived$.subscribe((v) => console.log(v));
* signal$.emit(3); // will trigger console.log, echoing 103
* ```
* @param signal$ a signal.
* @param transform a transformation function.
* @returns a new signal that will emit the transformed data.
*/
export declare function deriveSignal<T, U>(signal$: ReadonlySignal<T>, transform: (data: T) => U): ReadonlySignal<U>;
/**
* Coalesce multiple signals into one that will emit the latest value emitted
* by any of the source signals.
*
* Example:
* ```ts
* const lastUpdate1$ = makeSignal<number>();
* const lastUpdate2$ = makeSignal<number>();
* const latestUpdate$ = coalesceSignals([lastUpdate1$, lastUpdate2$]);
* latestUpdate$.subscribe((v) => console.log(v));
* lastUpdate1$.emit(1577923200000); // will log 1577923200000
* lastUpdate2$.emit(1653230659450); // will log 1653230659450
* ```
* @param signals$ an array of signals to observe.
* @returns a new signal that emits whenever one of the source signals emits.
*/
export declare function coalesceSignals<T extends unknown[]>(signals$: {
[P in keyof T]: ReadonlySignal<T[P]>;
}): ReadonlySignal<T[number]>;