@solidjs/signals 0.13.13 → 2.0.0-beta.10
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/README.md +8 -7
- package/dist/dev.js +502 -244
- package/dist/node.cjs +1501 -1308
- package/dist/prod.js +1134 -929
- package/dist/types/boundaries.d.ts +124 -3
- package/dist/types/core/action.d.ts +34 -0
- package/dist/types/core/constants.d.ts +24 -0
- package/dist/types/core/context.d.ts +10 -2
- package/dist/types/core/core.d.ts +108 -9
- package/dist/types/core/dev.d.ts +1 -1
- package/dist/types/core/effect.d.ts +3 -2
- package/dist/types/core/error.d.ts +24 -0
- package/dist/types/core/external.d.ts +19 -0
- package/dist/types/core/graph.d.ts +1 -0
- package/dist/types/core/owner.d.ts +93 -3
- package/dist/types/core/scheduler.d.ts +27 -2
- package/dist/types/core/types.d.ts +3 -6
- package/dist/types/index.d.ts +2 -2
- package/dist/types/map.d.ts +32 -3
- package/dist/types/signals.d.ts +338 -39
- package/dist/types/store/optimistic.d.ts +38 -12
- package/dist/types/store/projection.d.ts +55 -15
- package/dist/types/store/reconcile.d.ts +22 -0
- package/dist/types/store/store.d.ts +72 -15
- package/dist/types/store/storePath.d.ts +28 -0
- package/dist/types/store/utils.d.ts +78 -7
- package/dist/types-cjs/boundaries.d.cts +173 -0
- package/dist/types-cjs/core/action.d.cts +35 -0
- package/dist/types-cjs/core/async.d.cts +6 -0
- package/dist/types-cjs/core/constants.d.cts +49 -0
- package/dist/types-cjs/core/context.d.cts +36 -0
- package/dist/types-cjs/core/core.d.cts +153 -0
- package/dist/types-cjs/core/dev.d.cts +49 -0
- package/dist/types-cjs/core/effect.d.cts +31 -0
- package/dist/types-cjs/core/error.d.cts +38 -0
- package/dist/types-cjs/core/external.d.cts +45 -0
- package/dist/types-cjs/core/graph.d.cts +4 -0
- package/dist/types-cjs/core/heap.d.cts +14 -0
- package/dist/types-cjs/core/index.d.cts +12 -0
- package/dist/types-cjs/core/lanes.d.cts +54 -0
- package/dist/types-cjs/core/owner.d.cts +116 -0
- package/dist/types-cjs/core/scheduler.d.cts +106 -0
- package/dist/types-cjs/core/types.d.cts +83 -0
- package/dist/types-cjs/index.d.cts +9 -0
- package/dist/types-cjs/map.d.cts +53 -0
- package/dist/types-cjs/package.json +3 -0
- package/dist/types-cjs/signals.d.cts +491 -0
- package/dist/types-cjs/store/index.d.cts +9 -0
- package/dist/types-cjs/store/optimistic.d.cts +45 -0
- package/dist/types-cjs/store/projection.d.cts +66 -0
- package/dist/types-cjs/store/reconcile.d.cts +23 -0
- package/dist/types-cjs/store/store.d.cts +125 -0
- package/dist/types-cjs/store/storePath.d.cts +58 -0
- package/dist/types-cjs/store/utils.d.cts +114 -0
- package/package.json +32 -24
|
@@ -16,7 +16,7 @@ export interface NodeOptions<T> {
|
|
|
16
16
|
name?: string;
|
|
17
17
|
transparent?: boolean;
|
|
18
18
|
equals?: ((prev: T, next: T) => boolean) | false;
|
|
19
|
-
|
|
19
|
+
ownedWrite?: boolean;
|
|
20
20
|
/** Exclude this signal from snapshot capture (internal — not part of public API) */
|
|
21
21
|
_noSnapshot?: boolean;
|
|
22
22
|
unobserved?: () => void;
|
|
@@ -29,8 +29,7 @@ export interface RawSignal<T> {
|
|
|
29
29
|
_snapshotValue?: any;
|
|
30
30
|
_name?: string;
|
|
31
31
|
_equals: false | ((a: T, b: T) => boolean);
|
|
32
|
-
|
|
33
|
-
_noSnapshot?: boolean;
|
|
32
|
+
_config: number;
|
|
34
33
|
_unobserved?: () => void;
|
|
35
34
|
_time: number;
|
|
36
35
|
_transition: Transition | null;
|
|
@@ -48,8 +47,7 @@ export interface FirewallSignal<T> extends RawSignal<T> {
|
|
|
48
47
|
export type Signal<T> = RawSignal<T> | FirewallSignal<T>;
|
|
49
48
|
export interface Owner {
|
|
50
49
|
id?: string;
|
|
51
|
-
|
|
52
|
-
_childrenForbidden?: boolean;
|
|
50
|
+
_config: number;
|
|
53
51
|
_snapshotScope?: boolean;
|
|
54
52
|
_disposal: Disposable | Disposable[] | null;
|
|
55
53
|
_parent: Owner | null;
|
|
@@ -65,7 +63,6 @@ export interface Computed<T> extends RawSignal<T>, Owner {
|
|
|
65
63
|
_deps: Link | null;
|
|
66
64
|
_depsTail: Link | null;
|
|
67
65
|
_flags: number;
|
|
68
|
-
_inSnapshotScope?: boolean;
|
|
69
66
|
_blocked?: boolean;
|
|
70
67
|
_pendingSource?: Computed<any>;
|
|
71
68
|
_pendingSources?: Set<Computed<any>>;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export { $REFRESH, ContextNotFoundError, NoOwnerError, NotReadyError, action, createContext, createOwner, createRoot, runWithOwner, flush, getNextChildId, peekNextChildId, getContext, setContext, getOwner, isDisposed, getObserver, isEqual, untrack, isPending, latest, isRefreshing, refresh, SUPPORTS_PROXY, setSnapshotCapture, markSnapshotScope, releaseSnapshotScope, clearSnapshots, enforceLoadingBoundary, enableExternalSource } from "./core/index.js";
|
|
2
2
|
import { type Dev } from "./core/index.js";
|
|
3
3
|
export declare const DEV: Dev | undefined;
|
|
4
|
-
export type { Owner, Context, ContextRecord, IQueue, ExternalSourceFactory, ExternalSource, ExternalSourceConfig, Dev, DevHooks, DiagnosticCapture, DiagnosticCode, DiagnosticEvent, DiagnosticKind, Diagnostics, DiagnosticSeverity } from "./core/index.js";
|
|
4
|
+
export type { Owner, Context, ContextRecord, IQueue, ExternalSourceFactory, ExternalSource, ExternalSourceConfig, Refreshable, Dev, DevHooks, DiagnosticCapture, DiagnosticCode, DiagnosticEvent, DiagnosticKind, Diagnostics, DiagnosticSeverity } from "./core/index.js";
|
|
5
5
|
export { createSignal, createMemo, createEffect, createRenderEffect, createTrackedEffect, createReaction, createOptimistic, resolve, onSettled, onCleanup } from "./signals.js";
|
|
6
6
|
export type { Accessor, Setter, Signal, ComputeFunction, EffectFunction, EffectBundle, EffectOptions, SignalOptions, MemoOptions, NoInfer } from "./signals.js";
|
|
7
7
|
export { mapArray, repeat, type Maybe } from "./map.js";
|
|
8
8
|
export * from "./store/index.js";
|
|
9
|
-
export { createLoadingBoundary, createErrorBoundary, createRevealOrder, flatten } from "./boundaries.js";
|
|
9
|
+
export { createLoadingBoundary, createErrorBoundary, createRevealOrder, flatten, type RevealOrder } from "./boundaries.js";
|
package/dist/types/map.d.ts
CHANGED
|
@@ -1,9 +1,28 @@
|
|
|
1
1
|
import { type Accessor } from "./signals.js";
|
|
2
2
|
export type Maybe<T> = T | void | null | undefined | false;
|
|
3
3
|
/**
|
|
4
|
-
* Reactively
|
|
4
|
+
* Reactively maps an array, reusing the previously-mapped value for unchanged
|
|
5
|
+
* items. The callback receives `(value, index)` as accessors so individual
|
|
6
|
+
* items and indexes can be subscribed to without re-running the mapper.
|
|
5
7
|
*
|
|
6
|
-
*
|
|
8
|
+
* This is the underlying helper that powers `<For>`. App code should use
|
|
9
|
+
* `<For>` directly; reach for `mapArray` when implementing custom list
|
|
10
|
+
* components.
|
|
11
|
+
*
|
|
12
|
+
* - `options.keyed` — `true` (default for primitives) compares by identity;
|
|
13
|
+
* `false` falls back to index-only mapping; pass a function `(item) => key`
|
|
14
|
+
* for stable identity by extracted key.
|
|
15
|
+
* - `options.fallback` — accessor returning a value to show when the input is
|
|
16
|
+
* empty.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const view = mapArray(
|
|
21
|
+
* items,
|
|
22
|
+
* (item, index) => `${index()}: ${item().label}`,
|
|
23
|
+
* { fallback: () => "no items" }
|
|
24
|
+
* );
|
|
25
|
+
* ```
|
|
7
26
|
*
|
|
8
27
|
* @description https://docs.solidjs.com/reference/reactive-utilities/map-array
|
|
9
28
|
*/
|
|
@@ -13,7 +32,17 @@ export declare function mapArray<Item, MappedItem>(list: Accessor<Maybe<readonly
|
|
|
13
32
|
name?: string;
|
|
14
33
|
}): Accessor<MappedItem[]>;
|
|
15
34
|
/**
|
|
16
|
-
* Reactively
|
|
35
|
+
* Reactively renders a callback `count` times, reusing previously-rendered
|
|
36
|
+
* entries when only the count changes. Underlying helper for `<Repeat>`.
|
|
37
|
+
*
|
|
38
|
+
* - `options.from` — start index (default `0`); useful for offset/windowed
|
|
39
|
+
* rendering.
|
|
40
|
+
* - `options.fallback` — accessor returning a value to show when count is `0`.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const view = repeat(count, i => `Item ${i}`, { fallback: () => "empty" });
|
|
45
|
+
* ```
|
|
17
46
|
*
|
|
18
47
|
* @description https://docs.solidjs.com/reference/reactive-utilities/repeat
|
|
19
48
|
*/
|
package/dist/types/signals.d.ts
CHANGED
|
@@ -1,13 +1,68 @@
|
|
|
1
1
|
import type { Disposable } from "./core/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Low-level reactive-cleanup primitive. Registers a callback that runs when
|
|
4
|
+
* the surrounding owner is disposed.
|
|
5
|
+
*
|
|
6
|
+
* **In 2.0 user code this is rare.** The two cases where you might reach for
|
|
7
|
+
* it have better-shaped tools:
|
|
8
|
+
*
|
|
9
|
+
* - **Component lifecycle (mount/unmount, listeners, intervals):** use
|
|
10
|
+
* {@link onSettled} and **return** a cleanup function. Setup and teardown
|
|
11
|
+
* stay paired in one block. This replaces the 1.x `onMount` + `onCleanup`
|
|
12
|
+
* pairing.
|
|
13
|
+
* - **Cleanup tied to an effect run:** `onCleanup` does not belong in
|
|
14
|
+
* `createEffect`'s apply phase. If a compute phase genuinely needs per-run
|
|
15
|
+
* teardown, that's usually a sign the work should be a memo/projection
|
|
16
|
+
* instead, or moved to `onSettled` if it's lifecycle-shaped.
|
|
17
|
+
*
|
|
18
|
+
* Where `onCleanup` is the right tool is **library / custom-primitive
|
|
19
|
+
* internals** — coordinating disposal inside a `createRoot` body, or wiring
|
|
20
|
+
* cleanup to a captured owner via `runWithOwner` from a custom factory.
|
|
21
|
+
* Application code rarely needs to write any of those shapes directly.
|
|
22
|
+
*
|
|
23
|
+
* Must be called inside an owner. Calling outside an owner is a no-op (with a
|
|
24
|
+
* dev-mode warning).
|
|
25
|
+
*
|
|
26
|
+
* Cannot be used inside `createTrackedEffect` or `onSettled` — return a
|
|
27
|
+
* cleanup function from the callback body instead.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* // Library shape: thread a resource's disposal into a *captured* owner
|
|
32
|
+
* // from a factory that has no settle-phase setup of its own. `onSettled`
|
|
33
|
+
* // would queue a callback we don't need; `onCleanup` is the leaner
|
|
34
|
+
* // primitive when the only job is "register disposal on this owner".
|
|
35
|
+
* function bindToOwner<T extends { dispose(): void }>(owner: Owner, resource: T): T {
|
|
36
|
+
* runWithOwner(owner, () => onCleanup(() => resource.dispose()));
|
|
37
|
+
* return resource;
|
|
38
|
+
* }
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
2
41
|
export declare function onCleanup(fn: Disposable): Disposable;
|
|
42
|
+
/**
|
|
43
|
+
* A zero-arg getter for a reactive value. Calling it inside a tracking scope
|
|
44
|
+
* (memo, effect compute, JSX expression) subscribes the scope to changes.
|
|
45
|
+
*
|
|
46
|
+
* Reading outside any tracking scope simply returns the current value without
|
|
47
|
+
* creating a subscription.
|
|
48
|
+
*/
|
|
3
49
|
export type Accessor<T> = () => T;
|
|
4
50
|
export declare function accessor<T>(node: any): Accessor<T>;
|
|
51
|
+
/**
|
|
52
|
+
* A signal setter. Accepts either a new value or an updater `(prev) => next`.
|
|
53
|
+
*
|
|
54
|
+
* If the type permits `undefined`, `setState()` (no args) clears to `undefined`.
|
|
55
|
+
*
|
|
56
|
+
* To store a function as the value itself (rather than as an updater), wrap it
|
|
57
|
+
* with an updater: `setHandler(() => myHandler)`.
|
|
58
|
+
*/
|
|
5
59
|
export type Setter<in out T> = {
|
|
6
60
|
<U extends T>(...args: undefined extends T ? [] : [value: Exclude<U, Function> | ((prev: T) => U)]): undefined extends T ? undefined : U;
|
|
7
61
|
<U extends T>(value: (prev: T) => U): U;
|
|
8
62
|
<U extends T>(value: Exclude<U, Function>): U;
|
|
9
63
|
<U extends T>(value: Exclude<U, Function> | ((prev: T) => U)): U;
|
|
10
64
|
};
|
|
65
|
+
/** A `[get, set]` pair returned from `createSignal` / `createOptimistic`. */
|
|
11
66
|
export type Signal<T> = [get: Accessor<T>, set: Setter<T>];
|
|
12
67
|
export type ComputeFunction<Prev, Next extends Prev = Prev> = (v: Prev) => PromiseLike<Next> | AsyncIterable<Next> | Next;
|
|
13
68
|
export type EffectFunction<Prev, Next extends Prev = Prev> = (v: Next, p?: Prev) => (() => void) | void;
|
|
@@ -15,21 +70,39 @@ export type EffectBundle<Prev, Next extends Prev = Prev> = {
|
|
|
15
70
|
effect: EffectFunction<Prev, Next>;
|
|
16
71
|
error: (err: unknown, cleanup: () => void) => void;
|
|
17
72
|
};
|
|
18
|
-
/** Options
|
|
19
|
-
|
|
73
|
+
/** Options shared by every effect primitive. */
|
|
74
|
+
interface BaseEffectOptions {
|
|
20
75
|
/** Debug name (dev mode only) */
|
|
21
76
|
name?: string;
|
|
77
|
+
}
|
|
78
|
+
/** Options for effect primitives that support deferring/scheduling their initial run (`createEffect`, `createRenderEffect`, `createReaction`). */
|
|
79
|
+
export interface EffectOptions extends BaseEffectOptions {
|
|
22
80
|
/** When true, defers the initial effect execution until the next change */
|
|
23
81
|
defer?: boolean;
|
|
82
|
+
/**
|
|
83
|
+
* When true, enqueues the initial effect callback through the effect queue instead of running
|
|
84
|
+
* it synchronously at creation. Lets the initial run participate in transitions -- if any
|
|
85
|
+
* source throws `NotReadyError` during the compute phase, the callback is held until the
|
|
86
|
+
* transition settles.
|
|
87
|
+
*
|
|
88
|
+
* Primarily for render effects that need transition-aware initial mounts (e.g. the root
|
|
89
|
+
* `insert()` in `render()`).
|
|
90
|
+
*/
|
|
91
|
+
schedule?: boolean;
|
|
24
92
|
}
|
|
25
93
|
/** Options for plain signals created with `createSignal(value)` or `createOptimistic(value)`. */
|
|
26
94
|
export interface SignalOptions<T> {
|
|
27
95
|
/** Debug name (dev mode only) */
|
|
28
96
|
name?: string;
|
|
29
|
-
/**
|
|
97
|
+
/**
|
|
98
|
+
* Custom equality function, or `false` to always notify subscribers.
|
|
99
|
+
* Defaults to reference equality (`isEqual`). Pass a comparator (e.g.
|
|
100
|
+
* `(a, b) => a.id === b.id`) for value-based equality, or `false` to
|
|
101
|
+
* notify on every write regardless of equality.
|
|
102
|
+
*/
|
|
30
103
|
equals?: false | ((prev: T, next: T) => boolean);
|
|
31
104
|
/** Suppress dev-mode warnings when writing inside an owned scope */
|
|
32
|
-
|
|
105
|
+
ownedWrite?: boolean;
|
|
33
106
|
/** Callback invoked when the signal loses all subscribers */
|
|
34
107
|
unobserved?: () => void;
|
|
35
108
|
}
|
|
@@ -45,18 +118,30 @@ export interface MemoOptions<T> {
|
|
|
45
118
|
name?: string;
|
|
46
119
|
/** When true, the owner is invisible to the ID scheme -- inherits parent ID and doesn't consume a childCount slot */
|
|
47
120
|
transparent?: boolean;
|
|
48
|
-
/**
|
|
121
|
+
/**
|
|
122
|
+
* Custom equality function, or `false` to always notify subscribers.
|
|
123
|
+
* Defaults to reference equality (`isEqual`). Pass a comparator (e.g.
|
|
124
|
+
* `(a, b) => a.id === b.id`) for value-based equality, or `false` to
|
|
125
|
+
* notify on every recompute regardless of equality.
|
|
126
|
+
*/
|
|
49
127
|
equals?: false | ((prev: T, next: T) => boolean);
|
|
50
128
|
/** Callback invoked when the computed loses all subscribers */
|
|
51
129
|
unobserved?: () => void;
|
|
52
|
-
/**
|
|
130
|
+
/**
|
|
131
|
+
* When true, defers the initial computation until the value is first read,
|
|
132
|
+
* **and** opts the memo into autodisposal — once it has no remaining
|
|
133
|
+
* subscribers it is torn down and recomputed from scratch on the next read.
|
|
134
|
+
* Use it for compute-on-demand values that should not retain state across
|
|
135
|
+
* idle periods. Non-lazy owned memos live for their owner's lifetime and
|
|
136
|
+
* never autodispose.
|
|
137
|
+
*/
|
|
53
138
|
lazy?: boolean;
|
|
54
139
|
}
|
|
55
140
|
export type NoInfer<T extends any> = [T][T extends any ? 0 : never];
|
|
56
141
|
/**
|
|
57
142
|
* Creates a simple reactive state with a getter and setter.
|
|
58
143
|
*
|
|
59
|
-
* When called with a plain value, creates a signal with `SignalOptions` (name, equals,
|
|
144
|
+
* When called with a plain value, creates a signal with `SignalOptions` (name, equals, ownedWrite, unobserved).
|
|
60
145
|
* When called with a function, creates a writable memo with `SignalOptions & MemoOptions` (adds id, lazy).
|
|
61
146
|
*
|
|
62
147
|
* ```typescript
|
|
@@ -70,56 +155,152 @@ export type NoInfer<T extends any> = [T][T extends any ? 0 : never];
|
|
|
70
155
|
*
|
|
71
156
|
* @returns `[state: Accessor<T>, setState: Setter<T>]`
|
|
72
157
|
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```ts
|
|
160
|
+
* const [count, setCount] = createSignal(0);
|
|
161
|
+
*
|
|
162
|
+
* count(); // 0
|
|
163
|
+
* setCount(1); // explicit value
|
|
164
|
+
* setCount(c => c + 1); // updater
|
|
165
|
+
* ```
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* ```ts
|
|
169
|
+
* // Writable memo: starts as `fn()`, can be locally overwritten by setter.
|
|
170
|
+
* const [user, setUser] = createSignal(() => fetchUser(userId()));
|
|
171
|
+
*
|
|
172
|
+
* setUser({ ...user(), name: "Alice" }); // optimistic local edit
|
|
173
|
+
* ```
|
|
174
|
+
*
|
|
73
175
|
* @description https://docs.solidjs.com/reference/basic-reactivity/create-signal
|
|
74
176
|
*/
|
|
75
177
|
export declare function createSignal<T>(): Signal<T | undefined>;
|
|
76
178
|
export declare function createSignal<T>(value: Exclude<T, Function>, options?: SignalOptions<T>): Signal<T>;
|
|
77
|
-
export declare function createSignal<T>(fn: ComputeFunction<T>,
|
|
179
|
+
export declare function createSignal<T>(fn: ComputeFunction<T>, options?: SignalOptions<T> & MemoOptions<T>): Signal<T>;
|
|
78
180
|
/**
|
|
79
181
|
* Creates a readonly derived reactive memoized signal.
|
|
80
182
|
*
|
|
81
183
|
* ```typescript
|
|
82
|
-
* const value = createMemo<T>(compute,
|
|
184
|
+
* const value = createMemo<T>(compute, options?: MemoOptions<T>);
|
|
83
185
|
* ```
|
|
84
|
-
* @param compute a function that receives its previous
|
|
85
|
-
* @param value an optional initial value for the computation; if set, fn will never receive undefined as first argument
|
|
186
|
+
* @param compute a function that receives its previous value and returns a new value used to react on a computation
|
|
86
187
|
* @param options `MemoOptions` -- id, name, equals, unobserved, lazy
|
|
87
188
|
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```ts
|
|
191
|
+
* const [first, setFirst] = createSignal("Ada");
|
|
192
|
+
* const [last, setLast] = createSignal("Lovelace");
|
|
193
|
+
*
|
|
194
|
+
* const fullName = createMemo(() => `${first()} ${last()}`);
|
|
195
|
+
*
|
|
196
|
+
* fullName(); // "Ada Lovelace"
|
|
197
|
+
* ```
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```ts
|
|
201
|
+
* // Async memo — reads surface as pending inside <Loading>
|
|
202
|
+
* const user = createMemo(async () => {
|
|
203
|
+
* const res = await fetch(`/users/${id()}`);
|
|
204
|
+
* return res.json();
|
|
205
|
+
* });
|
|
206
|
+
* ```
|
|
207
|
+
*
|
|
88
208
|
* @description https://docs.solidjs.com/reference/basic-reactivity/create-memo
|
|
89
209
|
*/
|
|
90
|
-
export declare function createMemo<
|
|
91
|
-
export declare function createMemo<Next extends Prev, Init = Next, Prev = Next>(compute: ComputeFunction<Init | Prev, Next>, value: Init, options?: MemoOptions<Next>): Accessor<Next>;
|
|
210
|
+
export declare function createMemo<T>(compute: ComputeFunction<undefined | NoInfer<T>, T>, options?: MemoOptions<T>): Accessor<T>;
|
|
92
211
|
/**
|
|
93
|
-
* Creates a reactive effect
|
|
212
|
+
* Creates a reactive effect with **separate compute and effect phases**.
|
|
213
|
+
*
|
|
214
|
+
* - `compute(prev)` runs reactively — *put all reactive reads here*. The
|
|
215
|
+
* returned value is passed to `effect` and is also the new "previous" value
|
|
216
|
+
* for the next run.
|
|
217
|
+
* - `effect(next, prev?)` runs imperatively (untracked) after the queue
|
|
218
|
+
* flushes. *Put DOM writes / fetch / logging / subscriptions here.* It may
|
|
219
|
+
* return a cleanup function which runs before the next effect or on
|
|
220
|
+
* disposal.
|
|
221
|
+
*
|
|
222
|
+
* Reactive reads inside `effect` will *not* re-trigger this effect — that's
|
|
223
|
+
* intentional. If you need a single-phase tracked effect, use
|
|
224
|
+
* `createTrackedEffect` (with the tradeoffs noted there).
|
|
225
|
+
*
|
|
226
|
+
* Pass an `EffectBundle` (`{ effect, error }`) instead of a plain function to
|
|
227
|
+
* intercept errors thrown from the compute or effect phases.
|
|
94
228
|
*
|
|
95
229
|
* ```typescript
|
|
96
|
-
* createEffect<T>(compute, effectFn | { effect, error },
|
|
230
|
+
* createEffect<T>(compute, effectFn | { effect, error }, options?: EffectOptions);
|
|
97
231
|
* ```
|
|
98
|
-
* @param compute a function that receives its previous
|
|
232
|
+
* @param compute a function that receives its previous value and returns a new value used to react on a computation
|
|
99
233
|
* @param effectFn a function that receives the new value and is used to perform side effects (return a cleanup function), or an `EffectBundle` with `effect` and `error` handlers
|
|
100
|
-
* @param
|
|
101
|
-
*
|
|
234
|
+
* @param options `EffectOptions` -- name, defer, schedule
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* ```ts
|
|
238
|
+
* const [count, setCount] = createSignal(0);
|
|
239
|
+
*
|
|
240
|
+
* createEffect(
|
|
241
|
+
* () => count(), // compute: tracks `count`
|
|
242
|
+
* value => console.log(value) // effect: side effect
|
|
243
|
+
* );
|
|
244
|
+
*
|
|
245
|
+
* setCount(1); // logs 1 after the next flush
|
|
246
|
+
* ```
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
* ```ts
|
|
250
|
+
* createEffect(
|
|
251
|
+
* () => userId(),
|
|
252
|
+
* id => {
|
|
253
|
+
* const ctrl = new AbortController();
|
|
254
|
+
* fetch(`/users/${id}`, { signal: ctrl.signal });
|
|
255
|
+
* return () => ctrl.abort(); // cleanup before next run / disposal
|
|
256
|
+
* }
|
|
257
|
+
* );
|
|
258
|
+
* ```
|
|
102
259
|
*
|
|
103
260
|
* @description https://docs.solidjs.com/reference/basic-reactivity/create-effect
|
|
104
261
|
*/
|
|
105
|
-
export declare function createEffect<
|
|
106
|
-
|
|
262
|
+
export declare function createEffect<T>(compute: ComputeFunction<undefined | NoInfer<T>, T>, effectFn: EffectFunction<NoInfer<T>, T> | EffectBundle<NoInfer<T>, T>, options?: EffectOptions): void;
|
|
263
|
+
/**
|
|
264
|
+
* @deprecated `createEffect(compute)` (single argument) is no longer supported.
|
|
265
|
+
* Pass a separate effect function as the second argument:
|
|
266
|
+
* `createEffect(compute, effect)`. See [MISSING_EFFECT_FN].
|
|
267
|
+
*
|
|
268
|
+
* - For a side effect that reacts to changes, split the work:
|
|
269
|
+
* `createEffect(() => signal(), value => doWork(value))`.
|
|
270
|
+
* - For a derived value, use `createMemo(() => signal())`.
|
|
271
|
+
* - For a one-shot side effect at construction time, just call the function.
|
|
272
|
+
*/
|
|
273
|
+
export declare function createEffect<T>(compute: ComputeFunction<undefined | NoInfer<T>, T>): never;
|
|
107
274
|
/**
|
|
108
275
|
* Creates a reactive computation that runs during the render phase as DOM elements
|
|
109
276
|
* are created and updated but not necessarily connected.
|
|
110
277
|
*
|
|
278
|
+
* Same compute / effect split as `createEffect`, but scheduled inside the render
|
|
279
|
+
* queue rather than after it. Reach for this only when authoring renderer
|
|
280
|
+
* plumbing (custom DOM bindings, JSX-generated `insert()` / `spread()` calls).
|
|
281
|
+
* App code should use `createEffect`.
|
|
282
|
+
*
|
|
111
283
|
* ```typescript
|
|
112
|
-
* createRenderEffect<T>(compute, effectFn,
|
|
284
|
+
* createRenderEffect<T>(compute, effectFn, options?: EffectOptions);
|
|
113
285
|
* ```
|
|
114
|
-
* @param compute a function that receives its previous
|
|
286
|
+
* @param compute a function that receives its previous value and returns a new value used to react on a computation
|
|
115
287
|
* @param effectFn a function that receives the new value and is used to perform side effects
|
|
116
|
-
* @param
|
|
117
|
-
*
|
|
288
|
+
* @param options `EffectOptions` -- name, defer, schedule
|
|
289
|
+
*
|
|
290
|
+
* @example
|
|
291
|
+
* ```ts
|
|
292
|
+
* // Custom directive: bind an element's textContent to a reactive source.
|
|
293
|
+
* function bindText(el: HTMLElement, source: () => string) {
|
|
294
|
+
* createRenderEffect(
|
|
295
|
+
* () => source(),
|
|
296
|
+
* value => { el.textContent = value; }
|
|
297
|
+
* );
|
|
298
|
+
* }
|
|
299
|
+
* ```
|
|
118
300
|
*
|
|
119
301
|
* @description https://docs.solidjs.com/reference/secondary-primitives/create-render-effect
|
|
120
302
|
*/
|
|
121
|
-
export declare function createRenderEffect<
|
|
122
|
-
export declare function createRenderEffect<Next, Init = Next>(compute: ComputeFunction<Init | Next, Next>, effectFn: EffectFunction<Next, Next>, value: Init, options?: EffectOptions): void;
|
|
303
|
+
export declare function createRenderEffect<T>(compute: ComputeFunction<undefined | NoInfer<T>, T>, effectFn: EffectFunction<NoInfer<T>, T>, options?: EffectOptions): void;
|
|
123
304
|
/**
|
|
124
305
|
* Creates a tracked reactive effect where dependency tracking and side effects happen
|
|
125
306
|
* in the same scope.
|
|
@@ -129,14 +310,27 @@ export declare function createRenderEffect<Next, Init = Next>(compute: ComputeFu
|
|
|
129
310
|
* state). Use only when dynamic subscription patterns require same-scope tracking.
|
|
130
311
|
*
|
|
131
312
|
* ```typescript
|
|
132
|
-
* createTrackedEffect(compute, options?:
|
|
313
|
+
* createTrackedEffect(compute, options?: { name?: string });
|
|
133
314
|
* ```
|
|
134
315
|
* @param compute a function that contains reactive reads to track and returns an optional cleanup function to run on disposal or before next execution
|
|
135
|
-
* @param options
|
|
316
|
+
* @param options -- name
|
|
317
|
+
*
|
|
318
|
+
* @example
|
|
319
|
+
* ```ts
|
|
320
|
+
* createTrackedEffect(() => {
|
|
321
|
+
* const target = focusedNode();
|
|
322
|
+
* if (!target) return;
|
|
323
|
+
*
|
|
324
|
+
* const handler = () => log(target.value());
|
|
325
|
+
* target.on("change", handler);
|
|
326
|
+
*
|
|
327
|
+
* return () => target.off("change", handler);
|
|
328
|
+
* });
|
|
329
|
+
* ```
|
|
136
330
|
*
|
|
137
331
|
* @description https://docs.solidjs.com/reference/secondary-primitives/create-tracked-effect
|
|
138
332
|
*/
|
|
139
|
-
export declare function createTrackedEffect(compute: () => void | (() => void), options?:
|
|
333
|
+
export declare function createTrackedEffect(compute: () => void | (() => void), options?: BaseEffectOptions): void;
|
|
140
334
|
/**
|
|
141
335
|
* Creates a reactive computation that runs after the render phase with flexible tracking.
|
|
142
336
|
*
|
|
@@ -147,11 +341,40 @@ export declare function createTrackedEffect(compute: () => void | (() => void),
|
|
|
147
341
|
* @param effectFn a function (or `EffectBundle`) that is called when tracked function is invalidated
|
|
148
342
|
* @param options `EffectOptions` -- name, defer
|
|
149
343
|
*
|
|
344
|
+
* @example
|
|
345
|
+
* ```ts
|
|
346
|
+
* const [count, setCount] = createSignal(0);
|
|
347
|
+
*
|
|
348
|
+
* const track = createReaction(() => {
|
|
349
|
+
* console.log("count changed once, re-arm to listen again");
|
|
350
|
+
* track(() => count()); // re-arm
|
|
351
|
+
* });
|
|
352
|
+
*
|
|
353
|
+
* track(() => count()); // initial arm
|
|
354
|
+
*
|
|
355
|
+
* setCount(1); // logs once, reaction re-armed for next change
|
|
356
|
+
* ```
|
|
357
|
+
*
|
|
150
358
|
* @description https://docs.solidjs.com/reference/secondary-primitives/create-reaction
|
|
151
359
|
*/
|
|
152
360
|
export declare function createReaction(effectFn: EffectFunction<undefined> | EffectBundle<undefined>, options?: EffectOptions): (tracking: () => void) => void;
|
|
153
361
|
/**
|
|
154
|
-
*
|
|
362
|
+
* Awaits a reactive expression and returns its first fully-settled value as a
|
|
363
|
+
* `Promise`. Pending async reads (`createMemo` returning a promise, etc.) are
|
|
364
|
+
* waited on; once the expression returns synchronously without `NotReadyError`
|
|
365
|
+
* the promise resolves with that value.
|
|
366
|
+
*
|
|
367
|
+
* Must be called *outside* a tracking scope — it doesn't subscribe, it just
|
|
368
|
+
* resolves the current value once.
|
|
369
|
+
*
|
|
370
|
+
* @example
|
|
371
|
+
* ```ts
|
|
372
|
+
* const user = createMemo(() => fetch(`/users/${id()}`).then(r => r.json()));
|
|
373
|
+
*
|
|
374
|
+
* // outside any reactive scope
|
|
375
|
+
* const initial = await resolve(() => user());
|
|
376
|
+
* ```
|
|
377
|
+
*
|
|
155
378
|
* @param fn a reactive expression to resolve
|
|
156
379
|
*/
|
|
157
380
|
export declare function resolve<T>(fn: () => T): Promise<T>;
|
|
@@ -159,34 +382,110 @@ export declare function resolve<T>(fn: () => T): Promise<T>;
|
|
|
159
382
|
* Creates an optimistic signal that can be used to optimistically update a value
|
|
160
383
|
* and then revert it back to the previous value at end of transition.
|
|
161
384
|
*
|
|
162
|
-
* When called with a plain value, creates an optimistic signal with `SignalOptions` (name, equals,
|
|
385
|
+
* When called with a plain value, creates an optimistic signal with `SignalOptions` (name, equals, ownedWrite, unobserved).
|
|
163
386
|
* When called with a function, creates a writable optimistic memo with `SignalOptions & MemoOptions` (adds id, lazy).
|
|
164
387
|
*
|
|
165
388
|
* ```typescript
|
|
166
389
|
* // Plain optimistic signal
|
|
167
390
|
* const [state, setState] = createOptimistic<T>(value, options?: SignalOptions<T>);
|
|
168
391
|
* // Writable optimistic memo (function overload)
|
|
169
|
-
* const [state, setState] = createOptimistic<T>(fn,
|
|
392
|
+
* const [state, setState] = createOptimistic<T>(fn, options?: SignalOptions<T> & MemoOptions<T>);
|
|
170
393
|
* ```
|
|
171
394
|
* @param value initial value of the signal; if empty, the signal's type will automatically extended with undefined
|
|
172
395
|
* @param options optional object with a name for debugging purposes and equals, a comparator function for the previous and next value to allow fine-grained control over the reactivity
|
|
173
396
|
*
|
|
174
397
|
* @returns `[state: Accessor<T>, setState: Setter<T>]`
|
|
175
398
|
*
|
|
399
|
+
* @example
|
|
400
|
+
* ```ts
|
|
401
|
+
* const [todos, setTodos] = createOptimistic(initialTodos);
|
|
402
|
+
*
|
|
403
|
+
* const addTodo = action(function* (text: string) {
|
|
404
|
+
* const tempId = crypto.randomUUID();
|
|
405
|
+
* setTodos(t => [...t, { id: tempId, text, pending: true }]); // optimistic
|
|
406
|
+
* const saved = yield api.createTodo(text);
|
|
407
|
+
* setTodos(t => t.map(x => (x.id === tempId ? saved : x))); // reconcile
|
|
408
|
+
* });
|
|
409
|
+
* ```
|
|
410
|
+
*
|
|
176
411
|
* @description https://docs.solidjs.com/reference/basic-reactivity/create-optimistic-signal
|
|
177
412
|
*/
|
|
178
413
|
export declare function createOptimistic<T>(): Signal<T | undefined>;
|
|
179
414
|
export declare function createOptimistic<T>(value: Exclude<T, Function>, options?: SignalOptions<T>): Signal<T>;
|
|
180
|
-
export declare function createOptimistic<T>(fn: ComputeFunction<T>,
|
|
415
|
+
export declare function createOptimistic<T>(fn: ComputeFunction<T>, options?: SignalOptions<T> & MemoOptions<T>): Signal<T>;
|
|
181
416
|
/**
|
|
182
|
-
*
|
|
417
|
+
* Schedules `callback` to run **once** after the reactive graph has fully
|
|
418
|
+
* settled — i.e. once every pending async read inside the current owner has
|
|
419
|
+
* resolved and the queue has flushed. Each call registers a single fire; it
|
|
420
|
+
* does not create an ongoing subscription.
|
|
421
|
+
*
|
|
422
|
+
* The canonical lifecycle primitive in 2.0. Three main usages:
|
|
423
|
+
*
|
|
424
|
+
* - **Component-level setup-and-teardown** *(the most common shape)*: run
|
|
425
|
+
* setup after the component's first stable render and **return a cleanup
|
|
426
|
+
* function** to dispose it on owner disposal. This is the replacement for
|
|
427
|
+
* the 1.x `onMount` + `onCleanup` pairing — setup and teardown live in one
|
|
428
|
+
* block, and `onCleanup` is no longer the right tool for component
|
|
429
|
+
* bodies. (`onMount` no longer exists in 2.0.)
|
|
430
|
+
* - **Post-settle "ready" hook:** run once after a component's first stable
|
|
431
|
+
* render — analytics ping, focus, scroll-into-view, etc. No cleanup needed.
|
|
432
|
+
* - **Inside an event handler:** schedule work to run after the action /
|
|
433
|
+
* transition triggered by the event has completed.
|
|
434
|
+
*
|
|
435
|
+
* Reactive reads inside the callback are *not* tracked — to react to
|
|
436
|
+
* subsequent settles, register a new `onSettled` each time.
|
|
183
437
|
*
|
|
184
|
-
*
|
|
185
|
-
*
|
|
186
|
-
* async values will throw NotReadyError, causing the callback to re-run when they settle.
|
|
438
|
+
* `onCleanup` is **not** allowed inside the callback — return a cleanup
|
|
439
|
+
* function instead. The returned cleanup runs on owner disposal.
|
|
187
440
|
*
|
|
188
|
-
*
|
|
441
|
+
* @example
|
|
442
|
+
* ```tsx
|
|
443
|
+
* // Component-level setup + teardown — replaces onMount + onCleanup.
|
|
444
|
+
* // Subscribe to an external source on mount, unsubscribe on dispose.
|
|
445
|
+
* function useViewportWidth() {
|
|
446
|
+
* const [width, setWidth] = createSignal(window.innerWidth);
|
|
447
|
+
* onSettled(() => {
|
|
448
|
+
* const onResize = () => setWidth(window.innerWidth);
|
|
449
|
+
* window.addEventListener("resize", onResize);
|
|
450
|
+
* return () => window.removeEventListener("resize", onResize);
|
|
451
|
+
* });
|
|
452
|
+
* return width;
|
|
453
|
+
* }
|
|
454
|
+
* ```
|
|
455
|
+
*
|
|
456
|
+
* @example
|
|
457
|
+
* ```tsx
|
|
458
|
+
* // Post-settle "ready" hook — no cleanup needed.
|
|
459
|
+
* function Dashboard() {
|
|
460
|
+
* const data = createMemo(async () => fetchData());
|
|
461
|
+
*
|
|
462
|
+
* onSettled(() => {
|
|
463
|
+
* analytics.track("dashboard.ready");
|
|
464
|
+
* });
|
|
465
|
+
*
|
|
466
|
+
* return <Loading fallback={<Spinner />}><pre>{data()}</pre></Loading>;
|
|
467
|
+
* }
|
|
468
|
+
* ```
|
|
469
|
+
*
|
|
470
|
+
* @example
|
|
471
|
+
* ```tsx
|
|
472
|
+
* // Event-handler — runs after the action settles.
|
|
473
|
+
* function SaveButton() {
|
|
474
|
+
* const save = action(function* () {
|
|
475
|
+
* yield api.save();
|
|
476
|
+
* });
|
|
477
|
+
*
|
|
478
|
+
* const handleClick = () => {
|
|
479
|
+
* save();
|
|
480
|
+
* onSettled(() => toast("Saved!"));
|
|
481
|
+
* };
|
|
482
|
+
*
|
|
483
|
+
* return <button onClick={handleClick}>Save</button>;
|
|
484
|
+
* }
|
|
485
|
+
* ```
|
|
189
486
|
*
|
|
190
|
-
* @param callback Function to run
|
|
487
|
+
* @param callback Function to run; may return a cleanup function that fires
|
|
488
|
+
* on owner disposal
|
|
191
489
|
*/
|
|
192
490
|
export declare function onSettled(callback: () => void | (() => void)): void;
|
|
491
|
+
export {};
|