@cdellacqua/signals
Ƭ ReadonlySignal<T
>: Object
A signal that can have subscribers and emit values to them.
Name |
---|
T |
Name | Type |
---|---|
nOfSubscriptions |
() => number |
subscribe |
(subscriber : Subscriber <T >) => Unsubscribe |
subscribeOnce |
(subscriber : Subscriber <T >) => Unsubscribe |
Ƭ Signal<T
>: ReadonlySignal
<T
> & { emit
: (v
: T
) => void
}
A signal that can have subscribers and emit values to them.
Name |
---|
T |
Ƭ Subscriber<T
>: (current
: T
) => void
Name |
---|
T |
▸ (current
): void
A generic subscriber that takes a value emitted by a signal as its only parameter.
Name | Type |
---|---|
current |
T |
void
Ƭ Unsubscribe: () => void
▸ (): void
A function that's used to unsubscribe a subscriber from a signal.
void
▸ coalesceSignals<T
>(signals$
): ReadonlySignal
<T
[number
]>
Coalesce multiple signals into one that will emit the latest value emitted by any of the source signals.
Example:
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
Name | Type |
---|---|
T |
extends unknown [] |
Name | Type | Description |
---|---|---|
signals$ |
{ [P in string | number | symbol]: ReadonlySignal<T[P]> } | an array of signals to observe. |
ReadonlySignal
<T
[number
]>
a new signal that emits whenever one of the source signals emits.
▸ deriveSignal<T
, U
>(signal$
, transform
): ReadonlySignal
<U
>
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:
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
Name |
---|
T |
U |
Name | Type | Description |
---|---|---|
signal$ |
ReadonlySignal <T > |
a signal. |
transform |
(data : T ) => U |
a transformation function. |
a new signal that will emit the transformed data.
▸ makeSignal<T
>(): Signal
<T
>
Make a signal of type T.
Example usage:
const signal$ = makeSignal<number>();
signal$.emit(10);
Example usage with no data:
const signal$ = makeSignal<void>();
signal$.emit();
Name |
---|
T |
Signal
<T
>
a signal.