-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
-
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
-
224
-
225
-
226
# API
## Interface types for Signals
```ts
export interface Disposable {
// Stop reactivity on the object
dispose(): void;
// ...internal members
}
export interface Computation {
// ...internal members
}
export interface ReactiveValue<T> {
// Get the containing value and associate a surrounding or specified Computation
// to the ReactiveValue.
get(computation?: Computation): T;
// Alias for `get()`
get value(): T;
// Get the containing value without associating to any Computations.
once(): T;
// ...internal members
}
```
## `Signal` class
```ts
export class Signal<T> implements Disposable, ReactiveValue<T> {
constructor(initialValue: T);
get(computation?: Computation): T;
// Set a new value and triggers updates.
set(newValue: T): void;
// Update a containing value based on the previous value.
update(fn: (prevValue: T) => T): void;
// Get or Set a containing value.
value: T;
once(): T;
dispose(): void;
}
// Alias for `new Signal(initialValue)`
export function signal<T>(initialValue: T): Signal<T>;
```
## `Derived` class
```ts
export class Derived<T> implements Disposable, Computation, ReactiveValue<T> {
constructor(compute: DerivedValueComputeFunction<T>);
get(computation?: Computation): T;
readonly value: T;
once(): T;
dispose(): void;
}
// Alias for `new Derived(compute)`
export function derived<T>(compute: DerivedValueComputeFunction<T>): Derived<T>;
```
<details>
<summary>Supplimental types</summary>
```ts
type DerivedValueComputeFunction<T> = (computation: Computation) => T;
```
</details>
## `Effect` class
```ts
export class Effect implements Disposable, Computation {
constructor(fn: EffectFunction);
dispose(): void;
}
// Alias for `new Effect(fn)`
export function effect(fn: EffectFunction): Effect;
```
<details>
<summary>Supplimental types</summary>
```ts
type EffectCleanupCallback = () => void;
type EffectFunction = (
computation: Computation,
) => void | EffectCleanupCallback;
```
</details>
## Signals helpers
```ts
// This helper is especially useful for writing reusable UIs (or components).
export type ReactiveOrStatic<T> = T | ReactiveValue<T>;
// Returns true if `x` is ReactiveValue, false otherwise.
export function isReactive<T>(x: ReactiveOrStatic<T>): x is ReactiveValue<T>;
// Creates a ReactiveValue that tracks Promise state changes.
export function asyncDerived<T, E = unknown>(
fn: (ctx: Computation, signal: AbortSignal) => Promise<T>,
): ReactiveValue<PromiseSnapshot<T, E>>;
```
<details>
<summary>Supplimental types</summary>
```ts
// The promise is not settled.
export interface Pending {
isSettled: false;
isRejected: false;
isResolved: false;
}
// The promise is resolved.
export interface Resolved<T> {
isSettled: true;
isRejected: false;
isResolved: true;
data: T;
}
// The promise is rejected.
export interface Rejected<E> {
isSettled: true;
isRejected: true;
isResolved: false;
error: E;
}
// State of a Promise at a particular point.
export type PromiseSnapshot<T, E> = Pending | Resolved<T> | Rejected<E>;
```
</details>
## DOM node creation
```ts
export type NodeChild = Node | string | null | undefined;
export type ElementSetup<T extends Element> = (el: T) => void;
// Creates an HTMLElement
export function el(
tagName: string,
setups?: ElementSetup<HTMLElement>[],
children?: ReactiveOrStatic<NodeChild>[],
): HTMLElement;
// Creates a SVGElement
export function svg(
tagName: string,
setups?: ElementSetup<SVGElement>[],
children?: ReactiveOrStatic<NodeChild>[],
): SVGElement;
// Creates a MathMLElement
export function mathml(
tagName: string,
setups?: ElementSetup<MathMLElement>[],
children?: ReactiveOrStatic<NodeChild>[],
): MathMLElement;
// Creates DocumentFragment
export function fragment(
children: ReactiveOrStatic<NodeChild>[],
): DocumentFragment;
```
## Element setups
```ts
// Set or Remove attribute from an element.
export function attr(
name: string,
value: ReactiveOrStatic<string | boolean>,
): ElementSetup<Element>;
// Set value to a property of an element.
export function prop<T extends Element, K extends keyof T = keyof T>(
name: T,
value: ReactiveOrStatic<T[K]>,
): ElementSetup<T>;
// Add an event listener to an element.
export function on(
eventName: string,
listener: ReactiveOrStatic<(event: Event) => void>,
options?: AddEventListenerOptions,
): ElementSetup<Element>;
// Set a list of class to an element.
export function classList(
...classes: ReactiveOrStatic<string | null>[]
): ElementSetup<Element>;
// Set value to a stylesheet property of an element.
export function style(
name: string,
value: ReactiveOrStatic<string | null>,
): ElementSetup<Element>;
```