2023-09-05 09:25:47 +02:00
|
|
|
export type Signal<V, A>= (set?: V)=> V & A;
|
|
|
|
type Action<V>= (this: { value: V }, ...a: any[])=> typeof S._ | void;
|
2023-09-07 17:35:23 +02:00
|
|
|
type SymbolOnclear= Symbol;
|
|
|
|
type SymbolSignal= Symbol;
|
2023-09-05 09:25:47 +02:00
|
|
|
type Actions<V>= Record<string, Action<V>>;
|
|
|
|
interface S {
|
|
|
|
_: Symbol
|
2023-09-07 17:35:23 +02:00
|
|
|
/**
|
|
|
|
* Simple example:
|
|
|
|
* ```js
|
|
|
|
* const hello= S("Hello Signal");
|
|
|
|
* ```
|
|
|
|
* …simple todo signal:
|
|
|
|
* ```js
|
|
|
|
* const todos= S([], {
|
|
|
|
* add(v){ this.value.push(S(v)); },
|
|
|
|
* remove(i){ this.value.splice(i, 1); },
|
|
|
|
* [S.symbols.onclear](){ S.clear(...this.value); },
|
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
* …computed signal:
|
|
|
|
* ```js
|
|
|
|
* const name= S("Jan");
|
|
|
|
* const surname= S("Andrle");
|
|
|
|
* const fullname= S(()=> name()+" "+surname());
|
|
|
|
* ```
|
|
|
|
* @param value Initial signal value. Or function computing value from other signals.
|
|
|
|
* @param actions Use to define actions on the signal. Such as add item to the array.
|
|
|
|
* There is also a reserved function `S.symbol.onclear` which is called when the signal is cleared
|
|
|
|
* by `S.clear`.
|
|
|
|
* */
|
2023-09-05 09:25:47 +02:00
|
|
|
<V, A extends Actions<V>>(value: V, actions?: A): Signal<V, A>;
|
|
|
|
action<S extends Signal<any, Actions<any>>, A extends (S extends Signal<any, infer A> ? A : never), N extends keyof A>(
|
|
|
|
signal: S,
|
|
|
|
name: N,
|
|
|
|
...params: A[N] extends (...args: infer P)=> any ? P : never
|
|
|
|
): void;
|
2023-10-19 15:01:54 +02:00
|
|
|
clear(...signals: Signal<any, any>[]): void;
|
2023-09-07 17:35:23 +02:00
|
|
|
on<T>(signal: Signal<T, any>, onchange: (a: T)=> void, options?: AddEventListenerOptions): void;
|
|
|
|
symbols: {
|
|
|
|
signal: SymbolSignal;
|
|
|
|
onclear: SymbolOnclear;
|
|
|
|
}
|
2023-09-12 15:25:13 +02:00
|
|
|
el<S extends any, T extends HTMLElement>(signal: Signal<S, any>, el: (v: S)=> T): T;
|
2023-09-05 09:25:47 +02:00
|
|
|
}
|
|
|
|
export const S: S;
|
2023-09-07 18:21:51 +02:00
|
|
|
declare global {
|
2023-09-09 21:15:43 +02:00
|
|
|
type ddeSignal<T, A= {}>= Signal<T, A>;
|
2023-09-07 18:21:51 +02:00
|
|
|
type ddeActions<V>= Actions<V>
|
|
|
|
}
|