@tempots/dom 33.1.0 → 34.0.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.
@@ -1,97 +0,0 @@
1
- import { AnySignal, Computed, ListenerOptions, Prop } from './signal';
2
- import { Value } from './value';
3
- import { ValueTypes } from '../types/domain';
4
- /**
5
- * A DisposalScope tracks signals created during its lifetime and disposes them when the scope ends.
6
- * This enables automatic signal disposal without manual OnDispose() calls.
7
- *
8
- * @public
9
- */
10
- export declare class DisposalScope {
11
- private _signals;
12
- private _callbacks;
13
- private _disposed;
14
- /**
15
- * Register a signal with this scope for automatic disposal.
16
- *
17
- * @param signal - The signal to track
18
- * @throws Error if the scope has already been disposed
19
- * @throws Error if the signal has already been disposed
20
- * @public
21
- */
22
- track(signal: AnySignal): void;
23
- /**
24
- * Register a disposal callback to be called when this scope is disposed.
25
- * Callbacks are called before signals are disposed.
26
- * Use this for cleanup that doesn't need the `removeTree` parameter.
27
- *
28
- * @param callback - The callback to call on disposal
29
- * @throws Error if the scope has already been disposed
30
- * @public
31
- */
32
- onDispose(callback: () => void): void;
33
- /**
34
- * Dispose all signals tracked by this scope.
35
- * This method is idempotent - calling it multiple times is safe.
36
- *
37
- * @public
38
- */
39
- dispose(): void;
40
- /**
41
- * Check if this scope has been disposed.
42
- *
43
- * @returns true if the scope has been disposed
44
- * @public
45
- */
46
- get disposed(): boolean;
47
- /**
48
- * Creates a prop signal and tracks it in this scope.
49
- * Use this method in async contexts where automatic tracking doesn't work.
50
- *
51
- * @param value - The initial value
52
- * @param equals - Optional equality function
53
- * @returns A tracked Prop signal
54
- * @public
55
- */
56
- prop<T>(value: T, equals?: (a: T, b: T) => boolean): Prop<T>;
57
- /**
58
- * Creates a computed signal and tracks it in this scope.
59
- * Use this method in async contexts where automatic tracking doesn't work.
60
- *
61
- * @param fn - The computation function
62
- * @param dependencies - Array of signals this computed depends on
63
- * @param equals - Optional equality function
64
- * @returns A tracked Computed signal
65
- * @public
66
- */
67
- computed<T>(fn: () => T, dependencies: Array<AnySignal>, equals?: (a: T, b: T) => boolean): Computed<T>;
68
- /**
69
- * Creates an effect and tracks it in this scope.
70
- * Use this method in async contexts where automatic tracking doesn't work.
71
- *
72
- * @param fn - The effect function
73
- * @param signals - Array of signals to listen to
74
- * @param options - Optional listener options
75
- * @returns A clear function (the effect itself is tracked in the scope)
76
- * @public
77
- */
78
- effect(fn: () => void, signals: Array<AnySignal>, options?: ListenerOptions): () => void;
79
- /**
80
- * Creates a computed signal with curried signature and tracks it in this scope.
81
- * Use this method in async contexts where automatic tracking doesn't work.
82
- *
83
- * @param args - Values or signals to compute from
84
- * @returns A function that takes the computation function and returns a tracked Computed signal
85
- * @public
86
- */
87
- computedOf<T extends Value<unknown>[]>(...args: T): <O>(fn: (...args: ValueTypes<T>) => O, equals?: (a: O, b: O) => boolean) => Computed<O>;
88
- /**
89
- * Creates an effect with curried signature and tracks it in this scope.
90
- * Use this method in async contexts where automatic tracking doesn't work.
91
- *
92
- * @param args - Values or signals to listen to
93
- * @returns A function that takes the effect function and returns a clear function
94
- * @public
95
- */
96
- effectOf<T extends Value<unknown>[]>(...args: T): (fn: (...args: ValueTypes<T>) => void, options?: ListenerOptions) => (() => void);
97
- }
@@ -1,64 +0,0 @@
1
- import { Signal } from './signal';
2
- /**
3
- * Represents the position of an element in a collection.
4
- *
5
- * @public
6
- */
7
- export declare class ElementPosition {
8
- #private;
9
- /**
10
- * The index of the element.
11
- */
12
- readonly index: number;
13
- /**
14
- * The total number of elements in the collection.
15
- */
16
- readonly total: Signal<number>;
17
- /**
18
- * The counter of the element starting from 1.
19
- */
20
- readonly counter: number;
21
- /**
22
- * Checks if the element is the first element in the collection.
23
- * @returns `true` if the element is the first element, `false` otherwise.
24
- */
25
- readonly isFirst: boolean;
26
- /**
27
- * Checks if the counter of the element is even.
28
- * @returns `true` if the counter is even, `false` otherwise.
29
- */
30
- readonly isEven: boolean;
31
- /**
32
- * Checks if the counter of the element is odd.
33
- * @returns `true` if the counter is odd, `false` otherwise.
34
- */
35
- readonly isOdd: boolean;
36
- /**
37
- * Creates a new instance of `ElementPosition`.
38
- * @param index - The index of the element.
39
- * @param total - The total number of elements in the collection.
40
- */
41
- constructor(
42
- /**
43
- * The index of the element.
44
- */
45
- index: number,
46
- /**
47
- * The total number of elements in the collection.
48
- */
49
- total: Signal<number>);
50
- /**
51
- * Checks if the element is the last element in the collection.
52
- * @returns `true` if the element is the last element, `false` otherwise.
53
- */
54
- get isLast(): Signal<boolean>;
55
- /**
56
- * Disposes the internal signal created by `isLast`.
57
- *
58
- * **Note:** With automatic signal disposal, this method is now a no-op when used within
59
- * a disposal scope (e.g., inside a renderable). The signal created by `isLast` is
60
- * automatically tracked and disposed when the scope ends. This method is kept for
61
- * backward compatibility and for cases where ElementPosition is used outside a scope.
62
- */
63
- readonly dispose: () => void;
64
- }
@@ -1,56 +0,0 @@
1
- /**
2
- * Represents a function that interpolates between two values.
3
- *
4
- * @typeParam T - The type of the values being interpolated.
5
- * @param start - The starting value.
6
- * @param end - The ending value.
7
- * @param delta - The interpolation factor between 0 and 1.
8
- * @returns The interpolated value.
9
- * @public
10
- */
11
- export type Interpolate<T> = (start: T, end: T, delta: number) => T;
12
- /**
13
- * Interpolates a number between a start and end value based on a delta.
14
- *
15
- * @param start - The starting value.
16
- * @param end - The ending value.
17
- * @param delta - The delta value between 0 and 1.
18
- * @returns The interpolated number.
19
- * @public
20
- */
21
- export declare const interpolateNumber: Interpolate<number>;
22
- /**
23
- * Interpolates between two strings based on a delta value.
24
- *
25
- * @param start - The starting string.
26
- * @param end - The ending string.
27
- * @param delta - The delta value between 0 and 1.
28
- * @returns The interpolated string.
29
- * @public
30
- */
31
- export declare const interpolateString: Interpolate<string>;
32
- /**
33
- * Interpolates between two dates based on a delta value.
34
- *
35
- * @param start - The starting date.
36
- * @param end - The ending date.
37
- * @param delta - The delta value between 0 and 1.
38
- * @returns The interpolated date.
39
- * @public
40
- */
41
- export declare const interpolateDate: Interpolate<Date>;
42
- /**
43
- * A fake interpolate function that always returns the end value.
44
- *
45
- * @public
46
- */
47
- export declare const endInterpolate: <T>(_start: T, end: T) => T;
48
- /**
49
- * Returns an interpolation function based on the type of the value.
50
- *
51
- * @typeParam T - The type of the value.
52
- * @param value - The value to be interpolated.
53
- * @returns An interpolation function that takes a start value, an end value, and a delta, and returns an interpolated value.
54
- * @public
55
- */
56
- export declare const guessInterpolate: <T>(value: T) => Interpolate<T>;
@@ -1,76 +0,0 @@
1
- import { DisposalScope } from './disposal-scope';
2
- /**
3
- * Global scope stack for tracking active disposal scopes.
4
- * The last element in the array is the current scope.
5
- *
6
- * @internal
7
- */
8
- export declare const scopeStack: DisposalScope[];
9
- /**
10
- * Push a scope onto the stack, making it the current scope.
11
- *
12
- * @param scope - The scope to push
13
- * @internal
14
- */
15
- export declare const pushScope: (scope: DisposalScope) => void;
16
- /**
17
- * Pop the current scope from the stack.
18
- *
19
- * @throws Error if the stack is empty
20
- * @internal
21
- */
22
- export declare const popScope: () => void;
23
- /**
24
- * Get the current active scope.
25
- *
26
- * @returns The current scope, or null if no scope is active
27
- * @public
28
- */
29
- export declare const getCurrentScope: () => DisposalScope | null;
30
- /**
31
- * Get the full scope stack.
32
- * Useful for debugging scope hierarchy.
33
- *
34
- * @advanced Most users don't need this. Use getCurrentScope() instead.
35
- * @returns Read-only array of active scopes
36
- * @public
37
- */
38
- export declare const getScopeStack: () => readonly DisposalScope[];
39
- /**
40
- * Get the parent scope of the current scope.
41
- *
42
- * @advanced Most users don't need this. Accessing parent scopes can lead to
43
- * unexpected behavior. Only use this for debugging or advanced use cases.
44
- * @returns The parent scope or null if no parent exists
45
- * @public
46
- */
47
- export declare const getParentScope: () => DisposalScope | null;
48
- /**
49
- * Execute a function within a scope context.
50
- * The scope is pushed before the function executes and popped after.
51
- * The scope is NOT disposed - the caller is responsible for disposal.
52
- *
53
- * @param scope - The scope to use
54
- * @param fn - The function to execute
55
- * @returns The result of the function
56
- * @public
57
- */
58
- export declare const withScope: <T>(scope: DisposalScope, fn: () => T) => T;
59
- /**
60
- * Execute a function in a new scope and dispose the scope immediately after.
61
- * Useful for one-off scoped operations.
62
- *
63
- * @param fn - The function to execute, receives the scope as parameter
64
- * @returns The result of the function
65
- * @public
66
- */
67
- export declare const scoped: <T>(fn: (scope: DisposalScope) => T) => T;
68
- /**
69
- * Execute a function without any scope tracking.
70
- * Signals created inside will NOT be automatically tracked.
71
- *
72
- * @param fn - The function to execute
73
- * @returns The result of the function
74
- * @public
75
- */
76
- export declare const untracked: <T>(fn: () => T) => T;
@@ -1,301 +0,0 @@
1
- import { ValueType, RemoveSignals, Values } from '../types/domain';
2
- import { AnySignal, Computed, Prop, Signal } from './signal';
3
- import { Value } from './value';
4
- /**
5
- * Represents a memory store that stores key-value pairs.
6
- *
7
- * @public
8
- */
9
- export declare class MemoryStore {
10
- private readonly _store;
11
- /**
12
- * Retrieves the value associated with the specified key from the memory store.
13
- * @param key - The key to retrieve the value for.
14
- * @returns The value associated with the key, or `null` if the key is not found.
15
- */
16
- readonly getItem: (key: string) => string | null;
17
- /**
18
- * Sets the value associated with the specified key in the memory store.
19
- * @param key - The key to set the value for.
20
- * @param value - The value to set.
21
- */
22
- readonly setItem: (key: string, value: string) => void;
23
- }
24
- /**
25
- * Represents the properties required for storing and retrieving a value of type `T`.
26
- *
27
- * @typeParam T - The type of the value to be stored.
28
- * @public
29
- */
30
- export type StoredPropOptions<T> = {
31
- /**
32
- * The key to use for storing and retrieving the value.
33
- * Can be a static string or a reactive signal that changes over time.
34
- */
35
- key: Value<string>;
36
- /**
37
- * The default value to use if the value is not found in the store.
38
- * This can be a value of type `T` or a function that returns a value of type `T`.
39
- * If a function is provided, it will be called to get the default value.
40
- */
41
- defaultValue: T | (() => T);
42
- /**
43
- * The store to use for storing and retrieving the value.
44
- */
45
- store: {
46
- /**
47
- * Retrieves the value associated with the specified key from the store.
48
- * @param key - The key to retrieve the value for.
49
- * @returns The value associated with the key, or `null` if the key is not found.
50
- */
51
- getItem: (key: string) => string | null;
52
- /**
53
- * Sets the value associated with the specified key in the store.
54
- * @param key - The key to set the value for.
55
- * @param value - The value to set.
56
- */
57
- setItem: (key: string, value: string) => void;
58
- };
59
- /**
60
- * A function that serializes a value of type `T` to a string.
61
- * The default implementation uses `JSON.stringify`.
62
- */
63
- serialize?: (v: T) => string;
64
- /**
65
- * A function that deserializes a string to a value of type `T`.
66
- * The default implementation uses `JSON.parse`.
67
- */
68
- deserialize?: (v: string) => T;
69
- /**
70
- * A function that compares two values of type `T` for equality.
71
- * The default implementation uses strict equality (`===`).
72
- */
73
- equals?: (a: T, b: T) => boolean;
74
- /**
75
- * A function that is called when a value is loaded from the store.
76
- * The default implementation returns the value as is.
77
- */
78
- onLoad?: (value: T) => T;
79
- /**
80
- * Whether to sync the value across tabs. Defaults to `true`.
81
- */
82
- syncTabs?: boolean;
83
- /**
84
- * Strategy for handling key changes when using a reactive key.
85
- * - 'load' (default): Load value from new key, the current state is already stored at this point
86
- * - 'migrate': Move current value to new key and continue with current value
87
- * - 'keep': Keep current value without loading from new key
88
- */
89
- onKeyChange?: 'load' | 'migrate' | 'keep';
90
- };
91
- /**
92
- * Creates a stored property that persists its value in a storage mechanism.
93
- *
94
- * @typeParam T - The type of the property value.
95
- * @param options - The options for creating the stored property.
96
- * @returns - The created stored property.
97
- * @public
98
- */
99
- export declare const storedProp: <T>({ key, defaultValue, store, serialize, deserialize, equals, onLoad, syncTabs, onKeyChange, }: StoredPropOptions<T>) => Prop<T>;
100
- /**
101
- * Represents the properties required for storing and retrieving a value of type `T`.
102
- *
103
- * @typeParam T - The type of the value to be stored.
104
- * @public
105
- */
106
- export type StorageOptions<T> = {
107
- /**
108
- * The key to use for storing and retrieving the value.
109
- * Can be a static string or a reactive signal that changes over time.
110
- */
111
- key: Value<string>;
112
- /**
113
- * The default value to use if the value is not found in the store.
114
- * This can be a value of type `T` or a function that returns a value of type `T`.
115
- * If a function is provided, it will be called to get the default value.
116
- */
117
- defaultValue: T | (() => T);
118
- /**
119
- * A function that serializes a value of type `T` to a string.
120
- * The default implementation uses `JSON.stringify`.
121
- */
122
- serialize?: (v: T) => string;
123
- /**
124
- * A function that deserializes a string to a value of type `T`.
125
- * The default implementation uses `JSON.parse`.
126
- */
127
- deserialize?: (v: string) => T;
128
- /**
129
- * A function that compares two values of type `T` for equality.
130
- * The default implementation uses strict equality (`===`).
131
- */
132
- equals?: (a: T, b: T) => boolean;
133
- /**
134
- * A function that is called when a value is loaded from the store.
135
- * The default implementation returns the value as is.
136
- */
137
- onLoad?: (value: T) => T;
138
- /**
139
- * Whether to sync the value across tabs. Defaults to `true`.
140
- */
141
- syncTabs?: boolean;
142
- /**
143
- * Strategy for handling key changes when using a reactive key.
144
- * - 'load' (default): Load value from new key, the current state is already stored at this point
145
- * - 'migrate': Move current value to new key and continue with current value
146
- * - 'keep': Keep current value without loading from new key
147
- */
148
- onKeyChange?: 'load' | 'migrate' | 'keep';
149
- };
150
- /**
151
- * Creates a prop that is backed by the localStorage or a MemoryStore.
152
- *
153
- * @param options - The options for creating the prop.
154
- * @returns The created prop.
155
- * @public
156
- */
157
- export declare const localStorageProp: <T>(options: StorageOptions<T>) => Prop<T>;
158
- /**
159
- * Creates a prop that stores its value in the session storage.
160
- *
161
- * @param options - The options for the storage prop.
162
- * @returns A prop that stores its value in the session storage.
163
- * @public
164
- */
165
- export declare const sessionStorageProp: <T>(options: StorageOptions<T>) => Prop<T>;
166
- /**
167
- * Options for animating signals.
168
- *
169
- * @typeParam T - The type of the signal values.
170
- * @public
171
- */
172
- export type AnimateSignalsOptions<T> = {
173
- /**
174
- * The function that interpolates between two values.
175
- */
176
- interpolate?: (start: T, end: T, delta: number) => T;
177
- /**
178
- * The duration of the animation in milliseconds.
179
- */
180
- duration?: Value<number>;
181
- /**
182
- * The easing function for the animation.
183
- */
184
- easing?: (t: number) => number;
185
- /**
186
- * The function that compares two values for equality.
187
- */
188
- equals?: (a: T, b: T) => boolean;
189
- };
190
- /**
191
- * Animates signals based on the provided options.
192
- *
193
- * @typeParam T - The type of the animated value.
194
- * @param initialValue - The initial value of the animation.
195
- * @param fn - A function that returns the end value of the animation.
196
- * @param dependencies - An array of signals that the animation depends on.
197
- * @param options - Optional options for the animation.
198
- * @returns - The animated value as Prop<T>
199
- * @public
200
- */
201
- export declare const animateSignals: <T>(initialValue: T, fn: () => T, dependencies: Array<AnySignal>, options?: AnimateSignalsOptions<T>) => Prop<T>;
202
- /**
203
- * Represents the configuration options for animating a signal.
204
- *
205
- * @typeParam T - The type of the signal value.
206
- * @public
207
- */
208
- export type AnimateSignal<T> = {
209
- /**
210
- * The initial value for the animation. If not provided, the current value of the input signal will be used.
211
- */
212
- initialValue?: T;
213
- /**
214
- * The interpolation function to use for calculating intermediate values during the animation.
215
- */
216
- interpolate?: (start: T, end: T, delta: number) => T;
217
- /**
218
- * The duration of the animation in milliseconds.
219
- */
220
- duration?: number;
221
- /**
222
- * The easing function to use for controlling the animation progress.
223
- */
224
- easing?: (t: number) => number;
225
- /**
226
- * The equality function to use for comparing signal values.
227
- */
228
- equals?: (a: T, b: T) => boolean;
229
- };
230
- /**
231
- * Animates a signal by creating a new signal that transitions from an initial value to the current value of the input signal.
232
- *
233
- * @typeParam T - The type of the signal value.
234
- * @param signal - The input signal to animate.
235
- * @param options - The animation options.
236
- * @returns - The animated signal.
237
- * @public
238
- */
239
- export declare const animateSignal: <T>(signal: Signal<T>, options?: AnimateSignal<T>) => Prop<T>;
240
- /**
241
- * Computes a value based on a record of signals and literals.
242
- *
243
- * @typeParam T - The type of the record containing signals and literals.
244
- * @typeParam O - The type of the computed value.
245
- * @param record - The record containing signals and literals.
246
- * @param fn - The function to compute the value based on the literals.
247
- * @returns - The computed value as a signal.
248
- * @public
249
- */
250
- export declare const computedRecord: <T extends Record<string, Value<unknown>>, O>(record: T, fn: (value: RemoveSignals<T>) => O) => Computed<O>;
251
- /**
252
- * Merges a record of signals and literals into a single signal.
253
- *
254
- * @typeParam T - The type of the record containing signals and literals.
255
- * @param options - The record containing signals and literals.
256
- * @returns - The merged signal.
257
- * @public
258
- */
259
- export declare const merge: <T extends Record<string, Value<unknown>>>(options: T) => Signal<{ [K in keyof T]: ValueType<T[K]>; }>;
260
- /**
261
- * Delays the value of a signal by a specified amount of time.
262
- *
263
- * @typeParam T - The type of the signal value.
264
- * @param signal - The signal to delay.
265
- * @param ms - The amount of time to delay the signal in milliseconds.
266
- * @returns - The delayed signal.
267
- * @public
268
- */
269
- export declare const delaySignal: <T>(signal: Signal<T>, ms: number | ((value: T) => number)) => Signal<T>;
270
- /**
271
- * Creates a signal that emits the previous value of the input signal.
272
- *
273
- * @typeParam T - The type of the signal value.
274
- * @param signal - The input signal.
275
- * @returns - The signal that emits the previous value of the input signal.
276
- * @public
277
- */
278
- export declare const previousSignal: <T>(signal: Signal<T>) => Signal<T | undefined>;
279
- /**
280
- * Creates a signal that emits a sliding window of values from the input signal.
281
- *
282
- * @typeParam T - The type of the signal value.
283
- * @param options - The options for the sliding window.
284
- * @returns - The signal that emits the sliding window of values.
285
- * @public
286
- */
287
- export declare const slidingWindowSignal: <T>({ size, signal, }: {
288
- size: number | undefined;
289
- signal: Signal<T>;
290
- }) => Computed<T[]>;
291
- /**
292
- * Binds a function or signal of a function to a set of signals and literals.
293
- *
294
- * @typeParam FN - The type of the function to bind.
295
- * @typeParam R - The return type of the function.
296
- * @param fn - The function to bind.
297
- * @returns - A function that takes a set of signals and literals and returns a computed signal.
298
- * @public
299
- */
300
- export declare const bind: <FN extends (...args: any[]) => R, R = ReturnType<FN>>(fn: Value<FN>) => (...args: Values<Parameters<FN>>) => Computed<R>;
301
- export declare function coalesce<L>(...args: readonly [...unknown[], L]): Computed<ValueType<L>>;