@solidjs/signals 0.9.10 → 0.10.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,4 +1,3 @@
1
- import type { SignalOptions } from "./core/index.js";
2
1
  export type Accessor<T> = () => T;
3
2
  export type Setter<in out T> = {
4
3
  <U extends T>(...args: undefined extends T ? [] : [value: Exclude<U, Function> | ((prev: T) => U)]): undefined extends T ? undefined : U;
@@ -13,92 +12,104 @@ export type EffectBundle<Prev, Next extends Prev = Prev> = {
13
12
  effect: EffectFunction<Prev, Next>;
14
13
  error: (err: unknown, cleanup: () => void) => void;
15
14
  };
15
+ /** Options for effect primitives (`createEffect`, `createRenderEffect`, `createTrackedEffect`, `createReaction`). */
16
16
  export interface EffectOptions {
17
+ /** Debug name (dev mode only) */
17
18
  name?: string;
19
+ /** When true, defers the initial effect execution until the next change */
18
20
  defer?: boolean;
19
21
  }
22
+ /** Options for plain signals created with `createSignal(value)` or `createOptimistic(value)`. */
23
+ export interface SignalOptions<T> {
24
+ /** Debug name (dev mode only) */
25
+ name?: string;
26
+ /** Custom equality function, or `false` to always notify subscribers */
27
+ equals?: false | ((prev: T, next: T) => boolean);
28
+ /** Suppress dev-mode warnings when writing inside an owned scope */
29
+ pureWrite?: boolean;
30
+ /** Callback invoked when the signal loses all subscribers */
31
+ unobserved?: () => void;
32
+ }
33
+ /**
34
+ * Options for read-only memos created with `createMemo`.
35
+ * Also used in combination with `SignalOptions` for writable memos
36
+ * (`createSignal(fn)` / `createOptimistic(fn)`).
37
+ */
20
38
  export interface MemoOptions<T> {
39
+ /** Stable identifier for the owner hierarchy */
40
+ id?: string;
41
+ /** Debug name (dev mode only) */
21
42
  name?: string;
43
+ /** Custom equality function, or `false` to always notify subscribers */
22
44
  equals?: false | ((prev: T, next: T) => boolean);
45
+ /** Callback invoked when the computed loses all subscribers */
46
+ unobserved?: () => void;
47
+ /** When true, defers the initial computation until the value is first read */
48
+ lazy?: boolean;
23
49
  }
24
50
  export type NoInfer<T extends any> = [T][T extends any ? 0 : never];
25
51
  /**
26
- * Creates a simple reactive state with a getter and setter
52
+ * Creates a simple reactive state with a getter and setter.
53
+ *
54
+ * When called with a plain value, creates a signal with `SignalOptions` (name, equals, pureWrite, unobserved).
55
+ * When called with a function, creates a writable memo with `SignalOptions & MemoOptions` (adds id, lazy).
56
+ *
27
57
  * ```typescript
28
- * const [state: Accessor<T>, setState: Setter<T>] = createSignal<T>(
29
- * value: T,
30
- * options?: { name?: string, equals?: false | ((prev: T, next: T) => boolean) }
31
- * )
58
+ * // Plain signal
59
+ * const [state, setState] = createSignal<T>(value, options?: SignalOptions<T>);
60
+ * // Writable memo (function overload)
61
+ * const [state, setState] = createSignal<T>(fn, initialValue?, options?: SignalOptions<T> & MemoOptions<T>);
32
62
  * ```
33
- * @param value initial value of the state; if empty, the state's type will automatically extended with undefined; otherwise you need to extend the type manually if you want setting to undefined not be an error
63
+ * @param value initial value of the state; if empty, the state's type will automatically extended with undefined
34
64
  * @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
35
65
  *
36
- * @returns ```typescript
37
- * [state: Accessor<T>, setState: Setter<T>]
38
- * ```
39
- * * the Accessor is a function that returns the current value and registers each call to the reactive root
40
- * * the Setter is a function that allows directly setting or mutating the value:
41
- * ```typescript
42
- * const [count, setCount] = createSignal(0);
43
- * setCount(count => count + 1);
44
- * ```
66
+ * @returns `[state: Accessor<T>, setState: Setter<T>]`
45
67
  *
46
68
  * @description https://docs.solidjs.com/reference/basic-reactivity/create-signal
47
69
  */
48
70
  export declare function createSignal<T>(): Signal<T | undefined>;
49
71
  export declare function createSignal<T>(value: Exclude<T, Function>, options?: SignalOptions<T>): Signal<T>;
50
- export declare function createSignal<T>(fn: ComputeFunction<T>, initialValue?: T, options?: SignalOptions<T>): Signal<T>;
72
+ export declare function createSignal<T>(fn: ComputeFunction<T>, initialValue?: T, options?: SignalOptions<T> & MemoOptions<T>): Signal<T>;
51
73
  /**
52
- * Creates a readonly derived reactive memoized signal
74
+ * Creates a readonly derived reactive memoized signal.
75
+ *
53
76
  * ```typescript
54
- * export function createMemo<T>(
55
- * compute: (v: T) => T,
56
- * value?: T,
57
- * options?: { name?: string, equals?: false | ((prev: T, next: T) => boolean) }
58
- * ): () => T;
77
+ * const value = createMemo<T>(compute, initialValue?, options?: MemoOptions<T>);
59
78
  * ```
60
79
  * @param compute a function that receives its previous or the initial value, if set, and returns a new value used to react on a computation
61
80
  * @param value an optional initial value for the computation; if set, fn will never receive undefined as first argument
62
- * @param options allows to set a name in dev mode for debugging purposes and use a custom comparison function in equals
81
+ * @param options `MemoOptions` -- id, name, equals, unobserved, lazy
63
82
  *
64
83
  * @description https://docs.solidjs.com/reference/basic-reactivity/create-memo
65
84
  */
66
85
  export declare function createMemo<Next extends Prev, Prev = Next>(compute: ComputeFunction<undefined | NoInfer<Prev>, Next>): Accessor<Next>;
67
86
  export declare function createMemo<Next extends Prev, Init = Next, Prev = Next>(compute: ComputeFunction<Init | Prev, Next>, value: Init, options?: MemoOptions<Next>): Accessor<Next>;
68
87
  /**
69
- * Creates a reactive effect that runs after the render phase
88
+ * Creates a reactive effect that runs after the render phase.
89
+ *
70
90
  * ```typescript
71
- * export function createEffect<T>(
72
- * compute: (prev: T) => T,
73
- * effect: (v: T, prev: T) => (() => void) | void,
74
- * value?: T,
75
- * options?: { name?: string }
76
- * ): void;
91
+ * createEffect<T>(compute, effectFn | { effect, error }, initialValue?, options?: EffectOptions);
77
92
  * ```
78
93
  * @param compute a function that receives its previous or the initial value, if set, and returns a new value used to react on a computation
79
- * @param effect a function that receives the new value and is used to perform side effects, return a cleanup function to run on disposal
80
- * @param error an optional function that receives an error if thrown during the computation
94
+ * @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
81
95
  * @param value an optional initial value for the computation; if set, fn will never receive undefined as first argument
82
- * @param options allows to set a name in dev mode for debugging purposes
96
+ * @param options `EffectOptions` -- name, defer
83
97
  *
84
98
  * @description https://docs.solidjs.com/reference/basic-reactivity/create-effect
85
99
  */
86
100
  export declare function createEffect<Next>(compute: ComputeFunction<undefined | NoInfer<Next>, Next>, effectFn: EffectFunction<NoInfer<Next>, Next> | EffectBundle<NoInfer<Next>, Next>): void;
87
101
  export declare function createEffect<Next, Init = Next>(compute: ComputeFunction<Init | Next, Next>, effect: EffectFunction<Next, Next> | EffectBundle<Next, Next>, value: Init, options?: EffectOptions): void;
88
102
  /**
89
- * Creates a reactive computation that runs during the render phase as DOM elements are created and updated but not necessarily connected
103
+ * Creates a reactive computation that runs during the render phase as DOM elements
104
+ * are created and updated but not necessarily connected.
105
+ *
90
106
  * ```typescript
91
- * export function createRenderEffect<T>(
92
- * compute: (prev: T) => T,
93
- * effect: (v: T, prev: T) => (() => void) | void,
94
- * value?: T,
95
- * options?: { name?: string }
96
- * ): void;
107
+ * createRenderEffect<T>(compute, effectFn, initialValue?, options?: EffectOptions);
97
108
  * ```
98
109
  * @param compute a function that receives its previous or the initial value, if set, and returns a new value used to react on a computation
99
- * @param effect a function that receives the new value and is used to perform side effects
110
+ * @param effectFn a function that receives the new value and is used to perform side effects
100
111
  * @param value an optional initial value for the computation; if set, fn will never receive undefined as first argument
101
- * @param options allows to set a name in dev mode for debugging purposes
112
+ * @param options `EffectOptions` -- name, defer
102
113
  *
103
114
  * @description https://docs.solidjs.com/reference/secondary-primitives/create-render-effect
104
115
  */
@@ -113,27 +124,23 @@ export declare function createRenderEffect<Next, Init = Next>(compute: ComputeFu
113
124
  * state). Use only when dynamic subscription patterns require same-scope tracking.
114
125
  *
115
126
  * ```typescript
116
- * export function createTrackedEffect(
117
- * compute: () => (() => void) | void,
118
- * options?: { name?: string }
119
- * ): void;
127
+ * createTrackedEffect(compute, options?: EffectOptions);
120
128
  * ```
121
129
  * @param compute a function that contains reactive reads to track and returns an optional cleanup function to run on disposal or before next execution
122
- * @param options allows to set a name in dev mode for debugging purposes
130
+ * @param options `EffectOptions` -- name, defer
123
131
  *
124
132
  * @description https://docs.solidjs.com/reference/secondary-primitives/create-tracked-effect
125
133
  */
126
134
  export declare function createTrackedEffect(compute: () => void | (() => void), options?: EffectOptions): void;
127
135
  /**
128
- * Creates a reactive computation that runs after the render phase with flexible tracking
136
+ * Creates a reactive computation that runs after the render phase with flexible tracking.
137
+ *
129
138
  * ```typescript
130
- * export function createReaction(
131
- * onInvalidate: () => void,
132
- * options?: { name?: string }
133
- * ): (fn: () => void) => void;
139
+ * const track = createReaction(effectFn, options?: EffectOptions);
140
+ * track(() => { // reactive reads });
134
141
  * ```
135
- * @param invalidated a function that is called when tracked function is invalidated.
136
- * @param options allows to set a name in dev mode for debugging purposes
142
+ * @param effectFn a function (or `EffectBundle`) that is called when tracked function is invalidated
143
+ * @param options `EffectOptions` -- name, defer
137
144
  *
138
145
  * @description https://docs.solidjs.com/reference/secondary-primitives/create-reaction
139
146
  */
@@ -146,36 +153,26 @@ export declare function resolve<T>(fn: () => T): Promise<T>;
146
153
  /**
147
154
  * Creates an optimistic signal that can be used to optimistically update a value
148
155
  * and then revert it back to the previous value at end of transition.
156
+ *
157
+ * When called with a plain value, creates an optimistic signal with `SignalOptions` (name, equals, pureWrite, unobserved).
158
+ * When called with a function, creates a writable optimistic memo with `SignalOptions & MemoOptions` (adds id, lazy).
159
+ *
149
160
  * ```typescript
150
- * export function createOptimistic<T>(): Signal<T | undefined>;
151
- * export function createOptimistic<T>(
152
- * value: Exclude<T, Function>,
153
- * options?: SignalOptions<T>
154
- * ): Signal<T>;
155
- * export function createOptimistic<T>(
156
- * fn: ComputeFunction<T>,
157
- * initialValue?: T,
158
- * options?: SignalOptions<T>
159
- * ): Signal<T>;
161
+ * // Plain optimistic signal
162
+ * const [state, setState] = createOptimistic<T>(value, options?: SignalOptions<T>);
163
+ * // Writable optimistic memo (function overload)
164
+ * const [state, setState] = createOptimistic<T>(fn, initialValue?, options?: SignalOptions<T> & MemoOptions<T>);
160
165
  * ```
161
- * @param value initial value of the signal; if empty, the signal's type will automatically extended with undefined; otherwise you need to extend the type manually if you want setting to undefined not be an error
166
+ * @param value initial value of the signal; if empty, the signal's type will automatically extended with undefined
162
167
  * @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
163
168
  *
164
- * @returns ```typescript
165
- * [state: Accessor<T>, setState: Setter<T>]
166
- * ```
167
- * * the Accessor is a function that returns the current value and registers each call to the reactive root
168
- * * the Setter is a function that allows directly setting or mutating the value:
169
- * ```typescript
170
- * const [count, setCount] = createOptimistic(0);
171
- * setCount(count => count + 1);
172
- * ```
169
+ * @returns `[state: Accessor<T>, setState: Setter<T>]`
173
170
  *
174
171
  * @description https://docs.solidjs.com/reference/basic-reactivity/create-optimistic-signal
175
172
  */
176
173
  export declare function createOptimistic<T>(): Signal<T | undefined>;
177
174
  export declare function createOptimistic<T>(value: Exclude<T, Function>, options?: SignalOptions<T>): Signal<T>;
178
- export declare function createOptimistic<T>(fn: ComputeFunction<T>, initialValue?: T, options?: SignalOptions<T>): Signal<T>;
175
+ export declare function createOptimistic<T>(fn: ComputeFunction<T>, initialValue?: T, options?: SignalOptions<T> & MemoOptions<T>): Signal<T>;
179
176
  /**
180
177
  * Runs a callback after the current flush cycle completes.
181
178
  *
@@ -1,4 +1,4 @@
1
- export type { Store, StoreSetter, StoreNode, NotWrappable, SolidStore } from "./store.js";
1
+ export type { Store, StoreSetter, StoreNode, StoreOptions, ProjectionOptions, NotWrappable, SolidStore } from "./store.js";
2
2
  export type { Merge, Omit } from "./utils.js";
3
3
  export { isWrappable, createStore, deep, $TRACK, $PROXY, $TARGET } from "./store.js";
4
4
  export { createProjection } from "./projection.js";
@@ -1,22 +1,19 @@
1
1
  import { $REFRESH } from "../core/index.js";
2
- import { type NoFn, type Store, type StoreOptions, type StoreSetter } from "./store.js";
2
+ import { type NoFn, type ProjectionOptions, type Store, type StoreSetter } from "./store.js";
3
3
  /**
4
4
  * Creates an optimistic store that can be used to optimistically update a value
5
5
  * and then revert it back to the previous value at end of transition.
6
- * ```typescript
7
- * export function createOptimisticStore<T>(
8
- * fn: (store: T) => void,
9
- * initial: T,
10
- * options?: { key?: string | ((item: NonNullable<any>) => any); all?: boolean }
11
- * ): [get: Store<T>, set: StoreSetter<T>];
12
- * ```
6
+ *
7
+ * When called with a plain value, creates an optimistic store.
8
+ * When called with a function, creates a derived optimistic store with `ProjectionOptions` (name, key, all).
9
+ *
13
10
  * @param fn a function that receives the current store and can be used to mutate it directly inside a transition
14
- * @param initial The initial value of the signal.
15
- * @param options Optional signal options.
11
+ * @param initial The initial value of the store.
12
+ * @param options Optional projection options for reconciliation.
16
13
  *
17
- * @returns A tuple containing an accessor for the current value and a setter function to apply changes.
14
+ * @returns A tuple containing a store accessor and a setter function to apply changes.
18
15
  */
19
16
  export declare function createOptimisticStore<T extends object = {}>(store: NoFn<T> | Store<NoFn<T>>): [get: Store<T>, set: StoreSetter<T>];
20
- export declare function createOptimisticStore<T extends object = {}>(fn: (store: T) => void | T | Promise<void | T> | AsyncIterable<void | T>, store?: NoFn<T> | Store<NoFn<T>>, options?: StoreOptions): [get: Store<T> & {
17
+ export declare function createOptimisticStore<T extends object = {}>(fn: (store: T) => void | T | Promise<void | T> | AsyncIterable<void | T>, store?: NoFn<T> | Store<NoFn<T>>, options?: ProjectionOptions): [get: Store<T> & {
21
18
  [$REFRESH]: any;
22
19
  }, set: StoreSetter<T>];
@@ -1,17 +1,25 @@
1
1
  import { $REFRESH, type Computed } from "../core/index.js";
2
- import { type Store, type StoreOptions } from "./store.js";
3
- export declare function createProjectionInternal<T extends object = {}>(fn: (draft: T) => void | T | Promise<void | T> | AsyncIterable<void | T>, initialValue?: T, options?: StoreOptions): {
2
+ import { type ProjectionOptions, type Store } from "./store.js";
3
+ export declare function createProjectionInternal<T extends object = {}>(fn: (draft: T) => void | T | Promise<void | T> | AsyncIterable<void | T>, initialValue?: T, options?: ProjectionOptions): {
4
4
  store: Store<T> & {
5
5
  [$REFRESH]: any;
6
6
  };
7
7
  node: Computed<void | T>;
8
8
  };
9
9
  /**
10
- * Creates a mutable derived value
10
+ * Creates a mutable derived store (projection). The derive function receives a mutable
11
+ * draft and can mutate it directly or return a new value for reconciliation.
12
+ *
13
+ * ```typescript
14
+ * const store = createProjection<T>(fn, initialValue?, options?: ProjectionOptions);
15
+ * ```
16
+ * @param fn a function that receives the current draft and mutates it or returns new data
17
+ * @param initialValue the initial store value (defaults to `{}`)
18
+ * @param options `ProjectionOptions` -- name, key, all
11
19
  *
12
20
  * @see {@link https://github.com/solidjs/x-reactivity#createprojection}
13
21
  */
14
- export declare function createProjection<T extends object = {}>(fn: (draft: T) => void | T | Promise<void | T> | AsyncIterable<void | T>, initialValue?: T, options?: StoreOptions): Store<T> & {
22
+ export declare function createProjection<T extends object = {}>(fn: (draft: T) => void | T | Promise<void | T> | AsyncIterable<void | T>, initialValue?: T, options?: ProjectionOptions): Store<T> & {
15
23
  [$REFRESH]: any;
16
24
  };
17
25
  export declare const writeTraps: ProxyHandler<any>;
@@ -1,8 +1,16 @@
1
1
  import { $REFRESH, type Computed, type Signal } from "../core/index.js";
2
2
  export type Store<T> = Readonly<T>;
3
3
  export type StoreSetter<T> = (fn: (state: T) => T | void) => void;
4
+ /** Base options for store primitives. */
4
5
  export type StoreOptions = {
6
+ /** Debug name (dev mode only) */
7
+ name?: string;
8
+ };
9
+ /** Options for derived/projected stores created with `createStore(fn)`, `createProjection`, or `createOptimisticStore(fn)`. */
10
+ export type ProjectionOptions = StoreOptions & {
11
+ /** Key property name or function for reconciliation identity */
5
12
  key?: string | ((item: NonNullable<any>) => any);
13
+ /** When true, reconciles all properties (not just tracked ones) */
6
14
  all?: boolean;
7
15
  };
8
16
  export type NoFn<T> = T extends Function ? never : T;
@@ -36,8 +44,25 @@ export declare function getKeys(source: Record<PropertyKey, any>, override: Reco
36
44
  export declare function getPropertyDescriptor(source: Record<PropertyKey, any>, override: Record<PropertyKey, any> | undefined, property: PropertyKey): PropertyDescriptor | undefined;
37
45
  export declare const storeTraps: ProxyHandler<StoreNode>;
38
46
  export declare function storeSetter<T extends object>(store: Store<T>, fn: (draft: T) => T | void): void;
47
+ /**
48
+ * Creates a deeply reactive store with proxy-based tracking.
49
+ *
50
+ * When called with a plain value, wraps it in a reactive proxy.
51
+ * When called with a function, creates a derived projection store with `ProjectionOptions` (name, key, all).
52
+ *
53
+ * ```typescript
54
+ * // Plain store
55
+ * const [store, setStore] = createStore<T>(initialValue);
56
+ * // Derived store (projection)
57
+ * const [store, setStore] = createStore<T>(fn, initialValue?, options?: ProjectionOptions);
58
+ * ```
59
+ * @param store initial value to wrap in a reactive proxy, or a derive function
60
+ * @param options `ProjectionOptions` -- name, key, all (only for derived stores)
61
+ *
62
+ * @returns `[store: Store<T>, setStore: StoreSetter<T>]`
63
+ */
39
64
  export declare function createStore<T extends object = {}>(store: NoFn<T> | Store<NoFn<T>>): [get: Store<T>, set: StoreSetter<T>];
40
- export declare function createStore<T extends object = {}>(fn: (store: T) => void | T | Promise<void | T> | AsyncIterable<void | T>, store?: NoFn<T> | Store<NoFn<T>>, options?: StoreOptions): [get: Store<T> & {
65
+ export declare function createStore<T extends object = {}>(fn: (store: T) => void | T | Promise<void | T> | AsyncIterable<void | T>, store?: NoFn<T> | Store<NoFn<T>>, options?: ProjectionOptions): [get: Store<T> & {
41
66
  [$REFRESH]: any;
42
67
  }, set: StoreSetter<T>];
43
68
  export declare function deep<T extends object>(store: Store<T>): Store<T>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@solidjs/signals",
3
- "version": "0.9.10",
4
- "description": "",
3
+ "version": "0.10.0",
4
+ "description": "SolidJS' standalone reactive implementation",
5
5
  "author": "Ryan Carniato",
6
6
  "license": "MIT",
7
7
  "repository": {