@tempots/dom 33.1.1 → 34.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dom/dom-context.d.ts +2 -9
- package/dom/dom-utils.d.ts +1 -0
- package/dom/headless-context.d.ts +1 -1
- package/index.cjs +1 -1
- package/index.d.ts +4 -8
- package/index.js +999 -970
- package/package.json +4 -1
- package/renderable/attribute.d.ts +20 -20
- package/renderable/bind.d.ts +1 -1
- package/renderable/conjunction.d.ts +1 -2
- package/renderable/domnode.d.ts +1 -1
- package/renderable/element.d.ts +8 -8
- package/renderable/empty.d.ts +2 -2
- package/renderable/ensure.d.ts +1 -2
- package/renderable/foreach.d.ts +1 -3
- package/renderable/fragment.d.ts +1 -1
- package/renderable/map-signal.d.ts +1 -1
- package/renderable/not-empty.d.ts +1 -2
- package/renderable/on-dispose.d.ts +2 -2
- package/renderable/oneof.d.ts +1 -2
- package/renderable/provider.d.ts +1 -1
- package/renderable/render.d.ts +2 -2
- package/renderable/repeat.d.ts +1 -2
- package/renderable/shadow-root.d.ts +9 -0
- package/renderable/task.d.ts +1 -1
- package/renderable/text.d.ts +1 -2
- package/renderable/utils.d.ts +1 -2
- package/renderable/when.d.ts +1 -1
- package/renderable/with-browser-ctx.d.ts +1 -1
- package/renderable/with-ctx.d.ts +1 -1
- package/renderable/with-headless-ctx.d.ts +1 -1
- package/renderable/with-scope.d.ts +1 -1
- package/types/domain.d.ts +44 -85
- package/std/disposal-scope.d.ts +0 -97
- package/std/element-position.d.ts +0 -64
- package/std/interpolate.d.ts +0 -56
- package/std/scope-stack.d.ts +0 -76
- package/std/signal-utils.d.ts +0 -301
- package/std/signal.d.ts +0 -605
- package/std/value.d.ts +0 -131
package/std/signal.d.ts
DELETED
|
@@ -1,605 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Represents any type of signal.
|
|
3
|
-
* It can be a Signal, Prop, or Computed.
|
|
4
|
-
*
|
|
5
|
-
* @typeParam T - The type of value the signal holds.
|
|
6
|
-
* @public
|
|
7
|
-
*/
|
|
8
|
-
export type AnySignal<T = any> = Signal<T> | Prop<T> | Computed<T>;
|
|
9
|
-
/**
|
|
10
|
-
* Represents a type that maps each property of `T` to a `Signal` of its corresponding type.
|
|
11
|
-
* @typeParam T - The type of the object.
|
|
12
|
-
* @public
|
|
13
|
-
*/
|
|
14
|
-
export type AtGetter<T> = {
|
|
15
|
-
[K in keyof T]-?: Signal<T[K]>;
|
|
16
|
-
};
|
|
17
|
-
export type ListenerOptions = {
|
|
18
|
-
skipInitial?: boolean;
|
|
19
|
-
once?: boolean;
|
|
20
|
-
abortSignal?: AbortSignal;
|
|
21
|
-
/**
|
|
22
|
-
* If true, the listener will not be automatically disposed when the current scope ends.
|
|
23
|
-
* Use this when you need explicit control over the listener lifecycle.
|
|
24
|
-
* @internal
|
|
25
|
-
*/
|
|
26
|
-
noAutoDispose?: boolean;
|
|
27
|
-
};
|
|
28
|
-
/**
|
|
29
|
-
* A reactive signal that holds a value and notifies listeners when the value changes.
|
|
30
|
-
*
|
|
31
|
-
* Signals are the foundation of Tempo's reactive system. They provide a way to create
|
|
32
|
-
* reactive data that automatically updates the UI when changed. Signals can be observed,
|
|
33
|
-
* transformed, and composed to create complex reactive behaviors.
|
|
34
|
-
*
|
|
35
|
-
* @example
|
|
36
|
-
* ```typescript
|
|
37
|
-
* // Create a signal with an initial value
|
|
38
|
-
* const count = new Signal(0, (a, b) => a === b)
|
|
39
|
-
*
|
|
40
|
-
* // Listen to changes
|
|
41
|
-
* const unsubscribe = count.on((newValue, oldValue) => {
|
|
42
|
-
* console.log(`Count changed from ${oldValue} to ${newValue}`)
|
|
43
|
-
* })
|
|
44
|
-
*
|
|
45
|
-
* // Transform the signal
|
|
46
|
-
* const doubled = count.map(n => n * 2)
|
|
47
|
-
* const isEven = count.map(n => n % 2 === 0)
|
|
48
|
-
* ```
|
|
49
|
-
*
|
|
50
|
-
* @example
|
|
51
|
-
* ```typescript
|
|
52
|
-
* // Create a signal from a Promise
|
|
53
|
-
* const userSignal = Signal.ofPromise(
|
|
54
|
-
* fetch('/api/user').then(r => r.json()),
|
|
55
|
-
* null, // initial value
|
|
56
|
-
* error => ({ error: error.message }) // error recovery
|
|
57
|
-
* )
|
|
58
|
-
* ```
|
|
59
|
-
*
|
|
60
|
-
* @typeParam T - The type of the value held by the signal
|
|
61
|
-
* @public
|
|
62
|
-
*/
|
|
63
|
-
export declare class Signal<T> {
|
|
64
|
-
readonly equals: (a: T, b: T) => boolean;
|
|
65
|
-
/**
|
|
66
|
-
* Creates a Signal that holds the result of a Promise, with proper error handling.
|
|
67
|
-
*
|
|
68
|
-
* This static method creates a signal that starts with an initial value and updates
|
|
69
|
-
* when the promise resolves. If the promise rejects, an optional recovery function
|
|
70
|
-
* can provide a fallback value.
|
|
71
|
-
*
|
|
72
|
-
* @example
|
|
73
|
-
* ```typescript
|
|
74
|
-
* // Basic usage with API call
|
|
75
|
-
* const userData = Signal.ofPromise(
|
|
76
|
-
* fetch('/api/user').then(r => r.json()),
|
|
77
|
-
* { loading: true }, // initial state
|
|
78
|
-
* error => ({ error: error.message, loading: false }) // error recovery
|
|
79
|
-
* )
|
|
80
|
-
*
|
|
81
|
-
* // Use in UI
|
|
82
|
-
* Ensure(userData,
|
|
83
|
-
* (user) => html.div('Welcome, ', user.map(u => u.name)),
|
|
84
|
-
* () => html.div('Loading...')
|
|
85
|
-
* )
|
|
86
|
-
* ```
|
|
87
|
-
*
|
|
88
|
-
* @example
|
|
89
|
-
* ```typescript
|
|
90
|
-
* // With custom equality function
|
|
91
|
-
* const config = Signal.ofPromise(
|
|
92
|
-
* loadConfig(),
|
|
93
|
-
* {},
|
|
94
|
-
* () => ({}),
|
|
95
|
-
* (a, b) => JSON.stringify(a) === JSON.stringify(b) // deep equality
|
|
96
|
-
* )
|
|
97
|
-
* ```
|
|
98
|
-
*
|
|
99
|
-
* @typeParam O - The type of the value returned by the Promise
|
|
100
|
-
* @param promise - The Promise to use to feed the Signal
|
|
101
|
-
* @param init - The initial value of the Signal before the Promise resolves
|
|
102
|
-
* @param recover - Optional function to recover from Promise rejection and provide an alternative value
|
|
103
|
-
* @param equals - Function to compare two values for equality (defaults to strict equality)
|
|
104
|
-
* @returns A Signal that represents the result of the Promise
|
|
105
|
-
*/
|
|
106
|
-
static readonly ofPromise: <O>(promise: Promise<O>, init: O, recover?: (error: unknown) => O, equals?: (a: O, b: O) => boolean) => Signal<O>;
|
|
107
|
-
/**
|
|
108
|
-
* Checks if a value is a Signal.
|
|
109
|
-
*
|
|
110
|
-
* @param value - The value to check.
|
|
111
|
-
* @returns `true` if the value is a Signal, `false` otherwise.
|
|
112
|
-
*/
|
|
113
|
-
static readonly is: <O>(value: O | Signal<O>) => value is Signal<O>;
|
|
114
|
-
/**
|
|
115
|
-
* @internal
|
|
116
|
-
*/
|
|
117
|
-
protected readonly $__signal__ = true;
|
|
118
|
-
/**
|
|
119
|
-
* @internal
|
|
120
|
-
*/
|
|
121
|
-
protected _value: T;
|
|
122
|
-
/**
|
|
123
|
-
* @internal
|
|
124
|
-
*/
|
|
125
|
-
protected readonly _derivatives: Array<Computed<unknown>>;
|
|
126
|
-
/**
|
|
127
|
-
* @internal
|
|
128
|
-
*/
|
|
129
|
-
protected readonly _onValueListeners: Array<(value: T, previousValue: T | undefined) => void>;
|
|
130
|
-
/**
|
|
131
|
-
* @internal
|
|
132
|
-
*/
|
|
133
|
-
protected readonly _onDisposeListeners: Array<() => void>;
|
|
134
|
-
/**
|
|
135
|
-
* Represents a signal with a value of type T.
|
|
136
|
-
*
|
|
137
|
-
* @param value - The initial value of the signal.
|
|
138
|
-
* @param equals - A function that determines whether two values of type T are equal.
|
|
139
|
-
* @public
|
|
140
|
-
*/
|
|
141
|
-
constructor(value: T, equals: (a: T, b: T) => boolean);
|
|
142
|
-
/**
|
|
143
|
-
* Gets the current value of the signal.
|
|
144
|
-
* @returns The current value of the signal.
|
|
145
|
-
*/
|
|
146
|
-
readonly get: () => T;
|
|
147
|
-
/**
|
|
148
|
-
* Gets the value of the signal.
|
|
149
|
-
* @returns The current value of the signal.
|
|
150
|
-
*/
|
|
151
|
-
get value(): T;
|
|
152
|
-
/**
|
|
153
|
-
* Checks if the signal has any registered listeners.
|
|
154
|
-
* @returns `true` if the signal has listeners, `false` otherwise.
|
|
155
|
-
*/
|
|
156
|
-
readonly hasListeners: () => boolean;
|
|
157
|
-
/**
|
|
158
|
-
* Registers a listener function to be called whenever the value of the signal changes.
|
|
159
|
-
* The listener function will be immediately called with the current value of the signal.
|
|
160
|
-
* Returns a function that can be called to unregister the listener.
|
|
161
|
-
*
|
|
162
|
-
* When called within a DisposalScope (e.g., inside a renderable), the listener is
|
|
163
|
-
* automatically cleaned up when the scope is disposed. This prevents memory leaks
|
|
164
|
-
* when listening to outer-scope signals from inner scopes.
|
|
165
|
-
*
|
|
166
|
-
* @param listener - The listener function to be called when the value of the signal changes.
|
|
167
|
-
* @param options - Options for the listener.
|
|
168
|
-
*
|
|
169
|
-
* @example
|
|
170
|
-
* ```typescript
|
|
171
|
-
* // Automatic cleanup when scope disposes
|
|
172
|
-
* const MyComponent = () => {
|
|
173
|
-
* const outerSignal = prop(0)
|
|
174
|
-
*
|
|
175
|
-
* return html.div(
|
|
176
|
-
* When(someCondition, () => {
|
|
177
|
-
* // This listener is automatically cleaned up when the When() disposes
|
|
178
|
-
* outerSignal.on(value => console.log(value))
|
|
179
|
-
* return html.span('Inner content')
|
|
180
|
-
* })
|
|
181
|
-
* )
|
|
182
|
-
* }
|
|
183
|
-
* ```
|
|
184
|
-
*/
|
|
185
|
-
readonly on: (listener: (value: T, previousValue: T | undefined) => void, options?: ListenerOptions) => () => void;
|
|
186
|
-
/**
|
|
187
|
-
* Registers a listener function to be called whenever the value of the signal changes.
|
|
188
|
-
* The listener function will not be called with the current value of the signal.
|
|
189
|
-
* Returns a function that can be called to unregister the listener.
|
|
190
|
-
*
|
|
191
|
-
* @param listener - The listener function to be called when the value of the signal changes.
|
|
192
|
-
* @param options - Options for the listener.
|
|
193
|
-
*/
|
|
194
|
-
readonly onChange: (listener: (value: T, previousValue: T) => void, options?: ListenerOptions) => () => void;
|
|
195
|
-
/**
|
|
196
|
-
* @internal
|
|
197
|
-
*/
|
|
198
|
-
protected readonly _setAndNotify: (newV: T) => void;
|
|
199
|
-
/**
|
|
200
|
-
* @internal
|
|
201
|
-
*/
|
|
202
|
-
protected _disposed: boolean;
|
|
203
|
-
/**
|
|
204
|
-
* Checks whether the signal is disposed.
|
|
205
|
-
* @returns True if the signal is disposed, false otherwise.
|
|
206
|
-
*/
|
|
207
|
-
readonly isDisposed: () => boolean;
|
|
208
|
-
/**
|
|
209
|
-
* Adds a listener function to be called when the object is disposed.
|
|
210
|
-
* @param listener - The listener function to be called when the object is disposed.
|
|
211
|
-
* @returns A function that can be called to remove the listener.
|
|
212
|
-
*/
|
|
213
|
-
readonly onDispose: (listener: () => void) => void;
|
|
214
|
-
/**
|
|
215
|
-
* Disposes the signal, releasing any resources associated with it.
|
|
216
|
-
* This clears all listeners, derivatives, and disposal callbacks.
|
|
217
|
-
*/
|
|
218
|
-
readonly dispose: () => void;
|
|
219
|
-
/**
|
|
220
|
-
* Creates a new computed signal by applying a transformation function to this signal's value.
|
|
221
|
-
*
|
|
222
|
-
* The `map` method is one of the most commonly used signal operations. It creates a new
|
|
223
|
-
* computed signal that automatically updates whenever the source signal changes. The
|
|
224
|
-
* transformation function is called with the current value and should return the new value.
|
|
225
|
-
*
|
|
226
|
-
* @example
|
|
227
|
-
* ```typescript
|
|
228
|
-
* const count = prop(5)
|
|
229
|
-
*
|
|
230
|
-
* // Transform to different types
|
|
231
|
-
* const doubled = count.map(n => n * 2)
|
|
232
|
-
* const message = count.map(n => `Count is ${n}`)
|
|
233
|
-
* const isEven = count.map(n => n % 2 === 0)
|
|
234
|
-
*
|
|
235
|
-
* // Use in UI
|
|
236
|
-
* html.div(
|
|
237
|
-
* html.div('Original: ', count.map(String)),
|
|
238
|
-
* html.div('Doubled: ', doubled.map(String)),
|
|
239
|
-
* html.div('Message: ', message),
|
|
240
|
-
* html.div('Is even: ', isEven.map(String))
|
|
241
|
-
* )
|
|
242
|
-
* ```
|
|
243
|
-
*
|
|
244
|
-
* @example
|
|
245
|
-
* ```typescript
|
|
246
|
-
* // Chain multiple transformations
|
|
247
|
-
* const user = prop({ name: 'John', age: 30 })
|
|
248
|
-
* const greeting = user
|
|
249
|
-
* .map(u => u.name)
|
|
250
|
-
* .map(name => name.toUpperCase())
|
|
251
|
-
* .map(name => `Hello, ${name}!`)
|
|
252
|
-
* ```
|
|
253
|
-
*
|
|
254
|
-
* @example
|
|
255
|
-
* ```typescript
|
|
256
|
-
* // With custom equality function for objects
|
|
257
|
-
* const items = prop([{ id: 1, name: 'Item 1' }])
|
|
258
|
-
* const itemNames = items.map(
|
|
259
|
-
* items => items.map(item => item.name),
|
|
260
|
-
* (a, b) => JSON.stringify(a) === JSON.stringify(b) // deep equality
|
|
261
|
-
* )
|
|
262
|
-
* ```
|
|
263
|
-
*
|
|
264
|
-
* **Auto-Disposal:** The returned computed signal is automatically registered with the current
|
|
265
|
-
* disposal scope (if one exists). When used within a renderable or `WithScope()`, the signal
|
|
266
|
-
* will be automatically disposed when the component unmounts. No manual `OnDispose()` needed!
|
|
267
|
-
*
|
|
268
|
-
* ```typescript
|
|
269
|
-
* const MyComponent: Renderable = (ctx) => {
|
|
270
|
-
* const count = prop(0);
|
|
271
|
-
* const doubled = count.map(x => x * 2); // ✅ Auto-disposed
|
|
272
|
-
* return html.div(doubled);
|
|
273
|
-
* };
|
|
274
|
-
* ```
|
|
275
|
-
*
|
|
276
|
-
* @typeParam O - The type of the transformed value
|
|
277
|
-
* @param fn - Function that transforms the signal's value to a new value
|
|
278
|
-
* @param equals - Optional function to determine if two transformed values are equal (defaults to strict equality)
|
|
279
|
-
* @returns A new computed signal with the transformed value (auto-registered with current scope)
|
|
280
|
-
*/
|
|
281
|
-
readonly map: <O>(fn: (value: T) => O, equals?: (a: O, b: O) => boolean) => Computed<O>;
|
|
282
|
-
/**
|
|
283
|
-
* Returns a new Signal that applies the given function to the value of the current Signal,
|
|
284
|
-
* and then flattens the resulting Signal.
|
|
285
|
-
*
|
|
286
|
-
* @typeParam O - The type of the value emitted by the resulting Signal.
|
|
287
|
-
* @param fn - The function to apply to the value of the current Signal.
|
|
288
|
-
* @param equals - A function that determines whether two values of type O are equal.
|
|
289
|
-
* Defaults to a strict equality check (===).
|
|
290
|
-
* @returns A new Signal that emits the values of the resulting Signal.
|
|
291
|
-
*/
|
|
292
|
-
readonly flatMap: <O>(fn: (value: T) => Signal<O>, equals?: (a: O, b: O) => boolean) => Computed<O>;
|
|
293
|
-
/**
|
|
294
|
-
* Invokes a callback function with the current value of the signal, without modifying the signal.
|
|
295
|
-
*
|
|
296
|
-
* @param fn - The callback function to be invoked with the current value of the signal.
|
|
297
|
-
* @returns A new signal that emits the same value as the original signal and invokes the callback function.
|
|
298
|
-
*/
|
|
299
|
-
readonly tap: (fn: (value: T) => void) => Computed<T>;
|
|
300
|
-
/**
|
|
301
|
-
* Returns a new Signal that emits the value at the specified key of the current value.
|
|
302
|
-
*
|
|
303
|
-
* @param key - The key of the value to retrieve.
|
|
304
|
-
* @returns A new Signal that emits the value at the specified key.
|
|
305
|
-
*/
|
|
306
|
-
readonly at: <K extends keyof T>(key: K) => Signal<T[K]>;
|
|
307
|
-
/**
|
|
308
|
-
* @internal
|
|
309
|
-
*/
|
|
310
|
-
private _$;
|
|
311
|
-
/**
|
|
312
|
-
* Represents a collection of signals mapping to each key/field in the wrapped value.
|
|
313
|
-
* @typeParam T - The type of the signals.
|
|
314
|
-
*/
|
|
315
|
-
get $(): AtGetter<T>;
|
|
316
|
-
readonly filter: (fn: (value: T) => boolean, startValue?: T) => Computed<T>;
|
|
317
|
-
/**
|
|
318
|
-
* Returns a new Computed object that applies the provided mapping function to the value of this Signal,
|
|
319
|
-
* and filters out values that are `undefined` or `null`.
|
|
320
|
-
*
|
|
321
|
-
* @typeParam O - The type of the mapped value.
|
|
322
|
-
* @param fn - The mapping function to apply to the value of this Signal.
|
|
323
|
-
* @param startValue - The initial value for the Computed object.
|
|
324
|
-
* @param equals - Optional equality function to determine if two values are equal.
|
|
325
|
-
* @returns - A new Computed object with the mapped and filtered values.
|
|
326
|
-
*/
|
|
327
|
-
readonly filterMap: <O>(fn: (value: T) => O | undefined | null, startValue: O, equals?: (a: O, b: O) => boolean) => Computed<O>;
|
|
328
|
-
/**
|
|
329
|
-
* Maps the values emitted by the signal to a new value asynchronously using the provided function.
|
|
330
|
-
* If the function throws an error, it will be caught and logged.
|
|
331
|
-
* If a recovery function is provided, it will be called with the error and its return value will be used as the mapped value.
|
|
332
|
-
* If no recovery function is provided, the error will be logged as an unhandled promise rejection.
|
|
333
|
-
*
|
|
334
|
-
* @typeParam O - The type of the mapped value.
|
|
335
|
-
* @param fn - The function to map the values emitted by the signal. The second argument to this function allows to cancel the previously running mapping function if it has not completed by the time a new value is emitted.
|
|
336
|
-
* @param alt - The alternate value to use if the signal is disposed or the mapping function throws an error.
|
|
337
|
-
* @param recover - The recovery function to handle errors thrown by the mapping function.
|
|
338
|
-
* @param equals - The equality function to compare the mapped values for equality.
|
|
339
|
-
* @returns A property that holds the mapped value and can be observed for changes.
|
|
340
|
-
*/
|
|
341
|
-
readonly mapAsync: <O>(fn: (value: T, options: {
|
|
342
|
-
abortSignal: AbortSignal;
|
|
343
|
-
}) => Promise<O>, alt: O, recover?: (error: unknown) => O, equals?: (a: O, b: O) => boolean) => Prop<O>;
|
|
344
|
-
/**
|
|
345
|
-
* Maps the values of the signal using the provided function `fn`, and returns a new signal
|
|
346
|
-
* containing the mapped values. If the mapped value is `undefined` or `null`, it is replaced
|
|
347
|
-
* with the provided `alt` value.
|
|
348
|
-
*
|
|
349
|
-
* @typeParam O - The type of the mapped value.
|
|
350
|
-
* @param fn - The function used to map the values of the signal.
|
|
351
|
-
* @param alt - The alternative value to use when the mapped value is `undefined` or `null`.
|
|
352
|
-
* @returns A new signal containing the mapped values.
|
|
353
|
-
*/
|
|
354
|
-
readonly mapMaybe: <O>(fn: (value: T) => O | undefined | null, alt: O) => Computed<O>;
|
|
355
|
-
/**
|
|
356
|
-
* Feeds a property into the signal and sets up disposal behavior.
|
|
357
|
-
* @param prop - The property to feed into the signal.
|
|
358
|
-
* @param autoDisposeProp - Determines whether the property should be automatically disposed when the signal is disposed.
|
|
359
|
-
* @returns The input property.
|
|
360
|
-
*/
|
|
361
|
-
readonly feedProp: (prop: Prop<T>, autoDisposeProp?: boolean) => Prop<T>;
|
|
362
|
-
/**
|
|
363
|
-
* Derives a new property from the current signal.
|
|
364
|
-
* @param options - The options for the derived property.
|
|
365
|
-
* @param options.autoDisposeProp - Determines whether the derived property should be automatically disposed.
|
|
366
|
-
* @param options.equals - A function that determines if two values are equal.
|
|
367
|
-
* @returns The derived property.
|
|
368
|
-
*/
|
|
369
|
-
readonly deriveProp: ({ autoDisposeProp, equals, }?: {
|
|
370
|
-
autoDisposeProp?: boolean;
|
|
371
|
-
equals?: (a: T, b: T) => boolean;
|
|
372
|
-
}) => Prop<T>;
|
|
373
|
-
/**
|
|
374
|
-
* Derives a new signal from the current signal. Useful to create a new signal that emits the same values as the current signal but can be disposed independently.
|
|
375
|
-
* @returns A new signal that emits the same values as the current signal.
|
|
376
|
-
*/
|
|
377
|
-
readonly derive: () => Computed<T>;
|
|
378
|
-
/**
|
|
379
|
-
* Returns a signal that emits the count of values received so far.
|
|
380
|
-
* @returns A signal that emits the count of values received so far.
|
|
381
|
-
*/
|
|
382
|
-
readonly count: () => Computed<number>;
|
|
383
|
-
/**
|
|
384
|
-
* Adds a computed value as a derivative of the signal.
|
|
385
|
-
* When the computed value is disposed, it is automatically removed from the derivatives list.
|
|
386
|
-
* Additionally, when the computed value is disposed, it sets the signal as dirty.
|
|
387
|
-
* @param computed - The computed value to add as a derivative.
|
|
388
|
-
*/
|
|
389
|
-
readonly setDerivative: <O>(computed: Computed<O>) => void;
|
|
390
|
-
}
|
|
391
|
-
/**
|
|
392
|
-
* Represents a computed signal that derives its value from a function.
|
|
393
|
-
* It extends the `Signal` class.
|
|
394
|
-
*
|
|
395
|
-
* @typeParam T - The type of the computed value.
|
|
396
|
-
* @public
|
|
397
|
-
*/
|
|
398
|
-
export declare class Computed<T> extends Signal<T> {
|
|
399
|
-
private readonly _fn;
|
|
400
|
-
/**
|
|
401
|
-
* Checks if a value is an instance of `Computed`.
|
|
402
|
-
*
|
|
403
|
-
* @param value - The value to check.
|
|
404
|
-
* @returns `true` if the value is an instance of `Computed`, `false` otherwise.
|
|
405
|
-
*/
|
|
406
|
-
static is<T = unknown>(value: unknown): value is Computed<T>;
|
|
407
|
-
/**
|
|
408
|
-
* @internal
|
|
409
|
-
*/
|
|
410
|
-
protected readonly $__computed__ = true;
|
|
411
|
-
/**
|
|
412
|
-
* @internal
|
|
413
|
-
*/
|
|
414
|
-
protected _isDirty: boolean;
|
|
415
|
-
/**
|
|
416
|
-
* Creates a new Computed signal.
|
|
417
|
-
*
|
|
418
|
-
* **Auto-Registration:** This constructor automatically registers the signal with the current
|
|
419
|
-
* disposal scope (if one exists). This ensures that computed signals created by methods like
|
|
420
|
-
* `.map()`, `.flatMap()`, `.filter()`, etc. are automatically tracked and disposed.
|
|
421
|
-
*
|
|
422
|
-
* When a computed signal is created within a renderable or `WithScope()`, it will be
|
|
423
|
-
* automatically disposed when the component unmounts or the scope is disposed.
|
|
424
|
-
*
|
|
425
|
-
* To create a computed signal that outlives the current scope, use `untracked()`:
|
|
426
|
-
* ```typescript
|
|
427
|
-
* const globalSignal = untracked(() => mySignal.map(x => x * 2));
|
|
428
|
-
* // Remember to dispose manually: globalSignal.dispose()
|
|
429
|
-
* ```
|
|
430
|
-
*
|
|
431
|
-
* @param _fn - The function that computes the value of the signal.
|
|
432
|
-
* @param equals - The function used to compare two values of type T for equality.
|
|
433
|
-
*
|
|
434
|
-
* @see {@link computed} - Factory function for creating computed signals with explicit dependencies
|
|
435
|
-
* @see {@link Signal.map} - Creates a computed signal by transforming values
|
|
436
|
-
* @see {@link getCurrentScope} - Get the current disposal scope
|
|
437
|
-
* @see {@link untracked} - Create signals outside of scope tracking
|
|
438
|
-
*/
|
|
439
|
-
constructor(_fn: () => T, equals: (a: T, b: T) => boolean);
|
|
440
|
-
/**
|
|
441
|
-
* Marks the signal as dirty, indicating that its value has changed and needs to be recalculated.
|
|
442
|
-
* If the signal is already dirty or disposed, this method does nothing.
|
|
443
|
-
* It also marks all dependent signals as dirty and schedules a notification to update their values.
|
|
444
|
-
*/
|
|
445
|
-
readonly setDirty: () => void;
|
|
446
|
-
/**
|
|
447
|
-
* @internal
|
|
448
|
-
*/
|
|
449
|
-
protected _scheduleCount: number;
|
|
450
|
-
/**
|
|
451
|
-
* Schedules a notification to be executed asynchronously.
|
|
452
|
-
* If the signal is dirty, it will be updated and notified.
|
|
453
|
-
* @internal
|
|
454
|
-
*/
|
|
455
|
-
protected readonly _scheduleNotify: () => void;
|
|
456
|
-
/** {@inheritDoc Signal.get} */
|
|
457
|
-
readonly get: () => T;
|
|
458
|
-
/** {@inheritDoc Signal.value} */
|
|
459
|
-
get value(): T;
|
|
460
|
-
/**
|
|
461
|
-
* Disposes the computed signal and cancels any pending recomputations.
|
|
462
|
-
* This override increments the schedule count to invalidate all pending
|
|
463
|
-
* microtasks before disposing the signal.
|
|
464
|
-
*/
|
|
465
|
-
readonly dispose: () => void;
|
|
466
|
-
}
|
|
467
|
-
/**
|
|
468
|
-
* Represents the data passed to a reducer effect.
|
|
469
|
-
*
|
|
470
|
-
* @typeParam S - The type of the state.
|
|
471
|
-
* @typeParam A - The type of the action.
|
|
472
|
-
* @public
|
|
473
|
-
*/
|
|
474
|
-
export type ReducerEffectData<S, A> = {
|
|
475
|
-
/**
|
|
476
|
-
* The previous state before the action was dispatched.
|
|
477
|
-
*/
|
|
478
|
-
previousState: S;
|
|
479
|
-
/**
|
|
480
|
-
* The current state after the action was dispatched.
|
|
481
|
-
*/
|
|
482
|
-
state: S;
|
|
483
|
-
/**
|
|
484
|
-
* The action that was dispatched.
|
|
485
|
-
*/
|
|
486
|
-
action: A;
|
|
487
|
-
/**
|
|
488
|
-
* A function to dispatch a new action.
|
|
489
|
-
*/
|
|
490
|
-
dispatch: (action: A) => void;
|
|
491
|
-
};
|
|
492
|
-
/**
|
|
493
|
-
* Represents a function that defines a reducer effect.
|
|
494
|
-
* A reducer effect is a function that takes in the previous state, current state, action, and a dispatch function,
|
|
495
|
-
* and performs some side effects based on the state and action.
|
|
496
|
-
*
|
|
497
|
-
* @typeParam S - The type of the state.
|
|
498
|
-
* @typeParam A - The type of the action.
|
|
499
|
-
*
|
|
500
|
-
* @param data - An object containing the previous state, current state, action, and dispatch function.
|
|
501
|
-
* @public
|
|
502
|
-
*/
|
|
503
|
-
export type ReducerEffect<S, A> = (data: ReducerEffectData<S, A>) => void;
|
|
504
|
-
/**
|
|
505
|
-
* Represents a property signal that holds a value of type T.
|
|
506
|
-
* It extends the `Signal` class.
|
|
507
|
-
*
|
|
508
|
-
* @typeParam T - The type of the property value.
|
|
509
|
-
* @public
|
|
510
|
-
*/
|
|
511
|
-
export declare class Prop<T> extends Signal<T> {
|
|
512
|
-
/**
|
|
513
|
-
* Checks if a value is a Prop.
|
|
514
|
-
* @param value - The value to check.
|
|
515
|
-
* @returns `true` if the value is a Prop, `false` otherwise.
|
|
516
|
-
*/
|
|
517
|
-
static is: <T_1>(value: T_1 | Prop<T_1> | Signal<T_1> | Computed<T_1>) => value is Prop<T_1>;
|
|
518
|
-
/**
|
|
519
|
-
* @internal
|
|
520
|
-
*/
|
|
521
|
-
protected readonly $__prop__ = true;
|
|
522
|
-
/**
|
|
523
|
-
* Changes the value of the property and notifies its listeners.
|
|
524
|
-
*
|
|
525
|
-
* @param value - The new value of the property.
|
|
526
|
-
*/
|
|
527
|
-
readonly set: (value: T) => void;
|
|
528
|
-
/**
|
|
529
|
-
* Updates the value of the signal by applying the provided function to the current value.
|
|
530
|
-
* @param fn - The function to apply to the current value.
|
|
531
|
-
*/
|
|
532
|
-
readonly update: (fn: (value: T) => T) => void;
|
|
533
|
-
/**
|
|
534
|
-
* Creates a reducer function that combines the provided reducer function and effects.
|
|
535
|
-
* @param fn - The reducer function that takes the current state and an action, and returns the new state.
|
|
536
|
-
* @param effects - An array of effects to be executed after the state is updated.
|
|
537
|
-
* @returns A dispatch function that can be used to update the state and trigger the effects.
|
|
538
|
-
*/
|
|
539
|
-
readonly reducer: <A>(fn: (acc: T, value: A) => T, ...effects: ReducerEffect<T, A>[]) => (action: A) => void;
|
|
540
|
-
/**
|
|
541
|
-
* Creates an isomorphism for the Signal.
|
|
542
|
-
* An isomorphism is a pair of functions that convert values between two types,
|
|
543
|
-
* along with an equality function to compare values of the second type.
|
|
544
|
-
*
|
|
545
|
-
* @param to - A function that converts values from type T to type O.
|
|
546
|
-
* @param from - A function that converts values from type O to type T.
|
|
547
|
-
* @param equals - An optional function that compares values of type O for equality.
|
|
548
|
-
* Defaults to a strict equality check (===).
|
|
549
|
-
* @returns A Prop object representing the isomorphism.
|
|
550
|
-
*/
|
|
551
|
-
readonly iso: <O>(to: (value: T) => O, from: (value: O) => T, equals?: (a: O, b: O) => boolean) => Prop<O>;
|
|
552
|
-
/**
|
|
553
|
-
* Returns a `Prop` that represents the value at the specified key of the current value.
|
|
554
|
-
*
|
|
555
|
-
* @param key - The key of the value to access.
|
|
556
|
-
* @returns A `Prop` that represents the value at the specified key.
|
|
557
|
-
*/
|
|
558
|
-
readonly atProp: <K extends keyof T>(key: K) => Prop<T[K]>;
|
|
559
|
-
/**
|
|
560
|
-
* Access for the current value of the property.
|
|
561
|
-
*/
|
|
562
|
-
get value(): T;
|
|
563
|
-
set value(value: T);
|
|
564
|
-
}
|
|
565
|
-
/**
|
|
566
|
-
* Creates a computed signal that depends on other signals and updates when any of the dependencies change.
|
|
567
|
-
*
|
|
568
|
-
* @typeParam T - The type of the computed value.
|
|
569
|
-
* @param fn - The function that computes the value.
|
|
570
|
-
* @param dependencies - The array of signals that the computed value depends on.
|
|
571
|
-
* @param equals - The equality function used to compare the previous and current computed values.
|
|
572
|
-
* @returns - The computed signal.
|
|
573
|
-
* @public
|
|
574
|
-
*/
|
|
575
|
-
export declare const computed: <T>(fn: () => T, dependencies: Array<AnySignal>, equals?: (a: T, b: T) => boolean) => Computed<T>;
|
|
576
|
-
/**
|
|
577
|
-
* Executes the provided function `fn` whenever any of the signals in the `signals` array change.
|
|
578
|
-
* Returns a disposable object that can be used to stop the effect.
|
|
579
|
-
*
|
|
580
|
-
* @param fn - The function to execute when the signals change.
|
|
581
|
-
* @param signals - An array of signals to watch for changes.
|
|
582
|
-
* @returns A disposable object that can be used to stop the effect.
|
|
583
|
-
* @public
|
|
584
|
-
*/
|
|
585
|
-
export declare const effect: (fn: () => void, signals: Array<AnySignal>, options?: ListenerOptions) => () => void;
|
|
586
|
-
/**
|
|
587
|
-
* Creates a new Prop object with the specified value and equality function.
|
|
588
|
-
*
|
|
589
|
-
* @typeParam T - The type of the value.
|
|
590
|
-
* @param value - The initial value of the Prop.
|
|
591
|
-
* @param equals - The equality function used to compare values. Defaults to strict equality (===).
|
|
592
|
-
* @returns A new Prop object.
|
|
593
|
-
* @public
|
|
594
|
-
*/
|
|
595
|
-
export declare const prop: <T>(value: T, equals?: (a: T, b: T) => boolean) => Prop<T>;
|
|
596
|
-
/**
|
|
597
|
-
* Creates a signal with the specified initial value and equality function.
|
|
598
|
-
*
|
|
599
|
-
* @typeParam T - The type of the signal value.
|
|
600
|
-
* @param value - The initial value of the signal.
|
|
601
|
-
* @param equals - The equality function used to compare signal values. Defaults to a strict equality check (`===`).
|
|
602
|
-
* @returns A new Signal instance.
|
|
603
|
-
* @public
|
|
604
|
-
*/
|
|
605
|
-
export declare const signal: <T>(value: T, equals?: (a: T, b: T) => boolean) => Signal<T>;
|