reactjs-signal 1.0.5 → 1.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/README.md CHANGED
@@ -118,109 +118,6 @@ scope.run(() => {
118
118
 
119
119
  - `EffectScope`: The created effect scope.
120
120
 
121
- ### `unstable_createAsyncComputed`
122
-
123
- Creates an async computed signal in Alien Signals. The getter is an async generator that yields dependencies and finally resolves to a computed value.
124
-
125
- #### Example
126
-
127
- ```typescript
128
- const asyncComp = createAsyncComputed<number>(async function* () {
129
- yield someDependency;
130
- return 42;
131
- });
132
- ```
133
-
134
- #### Parameters
135
-
136
- - `getter` (`() => AsyncGenerator<Dependency, T>`): An async generator returning dependencies and ultimately a value.
137
-
138
- #### Returns
139
-
140
- - `AsyncComputed<T>`: The created async computed signal.
141
-
142
- ### `unstable_createAsyncEffect`
143
-
144
- Creates an async effect in Alien Signals. The function is an async generator that yields dependencies as they are discovered.
145
-
146
- #### Example
147
-
148
- ```typescript
149
- createAsyncEffect(async function* () {
150
- yield someDependency;
151
- console.log('Async effect done!');
152
- });
153
- ```
154
-
155
- #### Parameters
156
-
157
- - `fn` (`() => AsyncGenerator<Dependency, T>`): An async generator returning dependencies.
158
-
159
- #### Returns
160
-
161
- - `Promise<T>`: The created async effect object.
162
-
163
- ### `unstable_createComputedArray`
164
-
165
- Creates a computed array signal in Alien Signals, deriving a reactive array from an original signal array.
166
-
167
- #### Example
168
-
169
- ```typescript
170
- const numbersSignal = createSignal([1, 2, 3]);
171
- const compArray = createComputedArray(numbersSignal, (itemSignal, i) => () => {
172
- return itemSignal.get() * 2;
173
- });
174
- ```
175
-
176
- #### Parameters
177
-
178
- - `arr` (`ISignal<I[]>`): Signal containing an array.
179
- - `getGetter` (`(itemSignal: ISignal<I>, index: number) => () => O`): A function returning a getter for each item signal.
180
-
181
- #### Returns
182
-
183
- - `Readonly<O[]>`: A proxied array signal.
184
-
185
- ### `unstable_createComputedSet`
186
-
187
- Creates a computed Set signal in Alien Signals that tracks changes to a source Set signal.
188
-
189
- #### Example
190
-
191
- ```typescript
192
- const setSignal = createSignal(new Set([1, 2]));
193
- const compSet = createComputedSet(setSignal);
194
- ```
195
-
196
- #### Parameters
197
-
198
- - `source` (`IWritableSignal<Set<T>>`): A signal containing a Set.
199
-
200
- #### Returns
201
-
202
- - `ISignal<Set<T>>`: A computed signal referencing that Set.
203
-
204
- ### `unstable_createEqualityComputed`
205
-
206
- Creates an equality-based computed signal, only updating when the new value is not deeply equal to the old value.
207
-
208
- #### Example
209
-
210
- ```typescript
211
- const eqComp = createEqualityComputed(() => {
212
- return { foo: 'bar' };
213
- });
214
- ```
215
-
216
- #### Parameters
217
-
218
- - `getter` (`() => T`): A function returning the value to compare.
219
-
220
- #### Returns
221
-
222
- - `ISignal<T>`: An equality computed signal.
223
-
224
121
  ### `useSignal`
225
122
 
226
123
  React hook returning `[value, setValue]` for a given Alien Signal. Uses `useSyncExternalStore` for concurrency-safe re-renders.
@@ -361,50 +258,6 @@ useHydrateSignal(countSignal, 10);
361
258
 
362
259
  - `EffectScope`: The created effect scope.
363
260
 
364
- ### `unstable_useAsyncComputedValue`
365
-
366
- React hook to read from an async computed signal. The hook fetches the current value, subscribing to changes via `useSyncExternalStore`, and triggers a `get()` call to retrieve updated data. Maintains an internal state for the resolved value of the promise.
367
-
368
- #### Example
369
-
370
- ```typescript
371
- const asyncSignal = createAsyncComputed<number>(async function*() {
372
- const val = someSignal.get();
373
- yield Promise.resolve(someSignal); // track async dep
374
- return val * 2;
375
- });
376
-
377
- function AsyncDisplay() {
378
- const value = useAsyncComputedValue(asyncSignal);
379
- return <div>Value: {String(value)}</div>;
380
- }
381
- ```
382
-
383
- #### Parameters
384
-
385
- - `alienAsyncComp` (`AsyncComputed<T>`): The async computed signal to read.
386
-
387
- #### Returns
388
-
389
- - `T | undefined`: The resolved value (or undefined if not yet resolved).
390
-
391
- ### `unstable_useAsyncEffect`
392
-
393
- React hook to run an asynchronous effect whenever the component mounts, cleaning up when it unmounts.
394
-
395
- #### Example
396
-
397
- ```typescript
398
- useAsyncEffect(async function* () {
399
- yield someDependency;
400
- console.log('Async side effect complete!');
401
- });
402
- ```
403
-
404
- #### Parameters
405
-
406
- - `fn` (`() => AsyncGenerator<Dependency, T>`): An async generator representing the effect logic.
407
-
408
261
 
409
262
  ## Refer
410
263
 
package/dist/index.d.ts CHANGED
@@ -1,45 +1,47 @@
1
- import { IWritableSignal, ISignal, Effect, EffectScope, Dependency, Computed } from 'alien-signals';
2
-
3
- /**
4
- *
5
- * React Alien Signals is a **TypeScript** library that provides hooks built on top of [Alien Signals](https://github.com/stackblitz/alien-signals).
6
- * It offers a seamless integration with React, ensuring concurrency-safe re-renders without tearing.
7
- *
8
- * @module reactjs-signal
9
- */
10
-
11
- declare class AsyncComputed<T = any> extends Computed {
12
- get(): Promise<T>;
13
- update(): Promise<boolean>;
14
- }
1
+ type IWritableSignal<T> = {
2
+ (): T;
3
+ (value: T): void;
4
+ };
15
5
  /**
16
6
  * Creates a writable Alien Signal.
17
7
  *
18
8
  * @example
19
9
  * ```typescript
20
10
  * const countSignal = createSignal(0);
21
- * countSignal.set(10); // sets the value to 10
11
+ * countSignal(10); // sets the value to 10
22
12
  * ```
23
13
  *
24
14
  * @template T - The type of the signal value.
25
15
  * @param {T} initialValue - The initial value of the signal.
26
16
  * @returns {IWritableSignal<T>} The created Alien Signal.
27
17
  */
28
- declare function createSignal<T>(initialValue: T): IWritableSignal<T>;
18
+ declare function createSignal<T>(initialValue: T): {
19
+ (): T;
20
+ (value: T): void;
21
+ };
22
+ /**
23
+ * Creates a writable Alien Signal that persists its value in localStorage.
24
+ *
25
+ * @template T - The type of the signal value.
26
+ * @param {string} key - The localStorage key to use for persistence.
27
+ * @param {T} initialValue - The initial value of the signal.
28
+ * @returns {IWritableSignal<T>} The created Alien Signal.
29
+ */
30
+ declare function createSignalStorage<T>(key: string, initialValue: T): IWritableSignal<T>;
29
31
  /**
30
32
  * Creates a computed Alien Signal based on a getter function.
31
33
  *
32
34
  * @example
33
35
  * ```typescript
34
36
  * const countSignal = createSignal(1);
35
- * const doubleSignal = createComputed(() => countSignal.get() * 2);
37
+ * const doubleSignal = createComputed(() => countSignal() * 2);
36
38
  * ```
37
39
  *
38
40
  * @template T - The type of the computed value.
39
41
  * @param {() => T} fn - A getter function returning a computed value.
40
42
  * @returns {ISignal<T>} The created computed signal.
41
43
  */
42
- declare function createComputed<T>(fn: () => T): ISignal<T>;
44
+ declare function createComputed<T>(fn: () => T): () => T;
43
45
  /**
44
46
  * Creates a side effect in Alien Signals.
45
47
  *
@@ -47,7 +49,7 @@ declare function createComputed<T>(fn: () => T): ISignal<T>;
47
49
  * ```typescript
48
50
  * const countSignal = createSignal(1);
49
51
  * createEffect(() => {
50
- * console.log('Count is', countSignal.get());
52
+ * console.log('Count is', countSignal());
51
53
  * });
52
54
  * ```
53
55
  *
@@ -55,110 +57,7 @@ declare function createComputed<T>(fn: () => T): ISignal<T>;
55
57
  * @param {() => T} fn - A function that will run whenever its tracked signals update.
56
58
  * @returns {Effect<T>} The created effect object.
57
59
  */
58
- declare function createEffect<T>(fn: () => T): Effect<T>;
59
- /**
60
- * Creates an Alien Signals effect scope. This scope can manage multiple effects,
61
- * allowing you to stop or start them together.
62
- *
63
- * @example
64
- * ```typescript
65
- * const scope = createSignalScope();
66
- * scope.run(() => {
67
- * // create effects in here...
68
- * });
69
- * ```
70
- *
71
- * @returns {EffectScope} The created effect scope.
72
- */
73
- declare function createSignalScope(): EffectScope;
74
- /**
75
- * Creates an async computed signal in Alien Signals. The getter is an async generator
76
- * that yields dependencies and finally resolves to a computed value.
77
- *
78
- * @example
79
- * ```typescript
80
- * const asyncComp = createAsyncComputed<number>(async function* () {
81
- * yield someDependency;
82
- * return 42;
83
- * });
84
- * ```
85
- *
86
- * @template T - The type of the computed value.
87
- * @param {() => AsyncGenerator<Dependency, T>} getter - An async generator returning dependencies and ultimately a value.
88
- * @returns {AsyncComputed<T>} The created async computed signal.
89
- * @experimental
90
- */
91
- declare function unstable_createAsyncComputed<T>(getter: () => AsyncGenerator<Dependency, T>): AsyncComputed<T>;
92
- /**
93
- * Creates an async effect in Alien Signals. The function is an async generator
94
- * that yields dependencies as they are discovered.
95
- *
96
- * @example
97
- * ```typescript
98
- * createAsyncEffect(async function* () {
99
- * yield someDependency;
100
- * console.log('Async effect done!');
101
- * });
102
- * ```
103
- *
104
- * @template T - The type of the effect value.
105
- * @param {() => AsyncGenerator<Dependency, T>} fn - An async generator returning dependencies.
106
- * @returns {Promise<T>} The created async effect object.
107
- */
108
- declare function unstable_createAsyncEffect<T>(fn: () => AsyncGenerator<Dependency, T>): Promise<T>;
109
- /**
110
- * Creates a computed array signal in Alien Signals, deriving a reactive
111
- * array from an original signal array.
112
- *
113
- * @example
114
- * ```typescript
115
- * const numbersSignal = createSignal([1, 2, 3]);
116
- * const compArray = createComputedArray(numbersSignal, (itemSignal, i) => () => {
117
- * return itemSignal.get() * 2;
118
- * });
119
- * ```
120
- *
121
- * @template I - The type of the items in the input array.
122
- * @template O - The type of the items in the output array.
123
- * @param {ISignal<I[]>} arr - Signal containing an array.
124
- * @param {(itemSignal: ISignal<I>, index: number) => () => O} getGetter - A function returning a getter for each item signal.
125
- * @returns {Readonly<O[]>} A proxied array signal.
126
- * @experimental
127
- */
128
- declare function unstable_createComputedArray<I, O>(arr: ISignal<I[]>, getGetter: (itemSignal: ISignal<I>, index: number) => () => O): Readonly<O[]>;
129
- /**
130
- * Creates a computed Set signal in Alien Signals that tracks changes
131
- * to a source Set signal.
132
- *
133
- * @example
134
- * ```typescript
135
- * const setSignal = createSignal(new Set([1, 2]));
136
- * const compSet = createComputedSet(setSignal);
137
- * ```
138
- *
139
- * @template T - The type of the items in the Set.
140
- * @param {IWritableSignal<Set<T>>} source - A signal containing a Set.
141
- * @returns {ISignal<Set<T>>} A computed signal referencing that Set.
142
- * @experimental
143
- */
144
- declare function unstable_createComputedSet<T>(source: IWritableSignal<Set<T>>): ISignal<Set<T>>;
145
- /**
146
- * Creates an equality-based computed signal, only updating when the new value
147
- * is not deeply equal to the old value.
148
- *
149
- * @example
150
- * ```typescript
151
- * const eqComp = createEqualityComputed(() => {
152
- * return { foo: 'bar' };
153
- * });
154
- * ```
155
- *
156
- * @template T - The type of the computed value.
157
- * @param {() => T} getter - A function returning the value to compare.
158
- * @returns {ISignal<T>} An equality computed signal.
159
- * @experimental
160
- */
161
- declare function unstable_createEqualityComputed<T>(getter: () => T): ISignal<T>;
60
+ declare function createEffect<T>(fn: () => T): () => void;
162
61
  /**
163
62
  * React hook returning `[value, setValue]` for a given Alien Signal.
164
63
  * Uses useSyncExternalStore for concurrency-safe re-renders.
@@ -184,7 +83,7 @@ declare function useSignal<T>(alienSignal: IWritableSignal<T>): [T, (val: T | ((
184
83
  * @example
185
84
  * ```typescript
186
85
  * const countSignal = createSignal(0);
187
- * const doubleSignal = createComputed(() => countSignal.get() * 2);
86
+ * const doubleSignal = createComputed(() => countSignal() * 2);
188
87
  * function Display() {
189
88
  * const count = useSignalValue(countSignal);
190
89
  * const double = useSignalValue(doubleSignal);
@@ -223,7 +122,7 @@ declare function useSetSignal<T>(alienSignal: IWritableSignal<T>): (val: T | ((o
223
122
  * ```typescript
224
123
  * function Logger() {
225
124
  * useSignalEffect(() => {
226
- * console.log('Signal changed:', someSignal.get());
125
+ * console.log('Signal changed:', someSignal());
227
126
  * });
228
127
  * return null;
229
128
  * }
@@ -232,72 +131,6 @@ declare function useSetSignal<T>(alienSignal: IWritableSignal<T>): (val: T | ((o
232
131
  * @param {() => void} fn - The effect function to run.
233
132
  */
234
133
  declare function useSignalEffect(fn: () => void): void;
235
- /**
236
- * React hook for managing an Alien Signals effect scope.
237
- * All signals/effects created inside this scope run when the component mounts,
238
- * and are stopped automatically when the component unmounts.
239
- *
240
- * @example
241
- * ```typescript
242
- * function ScopedEffects() {
243
- * const scope = useSignalScope();
244
- * useEffect(() => {
245
- * scope.run(() => {
246
- * createEffect(() => {
247
- * console.log('Scoped effect:', someSignal.get());
248
- * });
249
- * });
250
- * }, [scope]);
251
- * return null;
252
- * }
253
- * ```
254
- *
255
- * @returns {EffectScope} The created effect scope.
256
- */
257
- declare function useSignalScope(): EffectScope;
258
- /**
259
- * React hook to read from an async computed signal.
260
- * The hook fetches the current value, subscribing to changes via useSyncExternalStore,
261
- * and triggers a get() call to retrieve updated data. Maintains an internal state
262
- * for the resolved value of the promise.
263
- *
264
- * @example
265
- * ```typescript
266
- * const asyncSignal = createAsyncComputed<number>(async function*() {
267
- * const val = someSignal.get();
268
- * yield Promise.resolve(someSignal); // track async dep
269
- * return val * 2;
270
- * });
271
- *
272
- * function AsyncDisplay() {
273
- * const value = useAsyncComputedValue(asyncSignal);
274
- * return <div>Value: {String(value)}</div>;
275
- * }
276
- * ```
277
- *
278
- * @template T - The type of the computed value.
279
- * @param {AsyncComputed<T>} alienAsyncComp - The async computed signal to read.
280
- * @returns {T | undefined} The resolved value (or undefined if not yet resolved).
281
- * @experimental
282
- */
283
- declare function unstable_useAsyncComputedValue<T>(alienAsyncComp: AsyncComputed<T>): T | undefined;
284
- /**
285
- * React hook to run an asynchronous effect whenever the component mounts,
286
- * cleaning up when it unmounts.
287
- *
288
- * @example
289
- * ```typescript
290
- * useAsyncEffect(async function* () {
291
- * yield someDependency;
292
- * console.log('Async side effect complete!');
293
- * });
294
- * ```
295
- *
296
- * @template T - The type of the effect value.
297
- * @param {() => AsyncGenerator<Dependency, T>} fn - An async generator representing the effect logic.
298
- * @experimental
299
- */
300
- declare function unstable_useAsyncEffect<T>(fn: () => AsyncGenerator<Dependency, T>): void;
301
134
  /**
302
135
  * React hook to initialize a signal with a value when hydrating from server.
303
136
  * @param alienSignal
@@ -310,4 +143,4 @@ declare function unstable_useAsyncEffect<T>(fn: () => AsyncGenerator<Dependency,
310
143
  */
311
144
  declare function useHydrateSignal<T>(alienSignal: IWritableSignal<T>, value: T): void;
312
145
 
313
- export { createComputed, createEffect, createSignal, createSignalScope, unstable_createAsyncComputed, unstable_createAsyncEffect, unstable_createComputedArray, unstable_createComputedSet, unstable_createEqualityComputed, unstable_useAsyncComputedValue, unstable_useAsyncEffect, useHydrateSignal, useSetSignal, useSignal, useSignalEffect, useSignalScope, useSignalValue };
146
+ export { createComputed, createEffect, createSignal, createSignalStorage, useHydrateSignal, useSetSignal, useSignal, useSignalEffect, useSignalValue };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import {signal,computed,effect,effectScope,unstable}from'alien-signals';import {useSyncExternalStore,useEffect,useMemo,useState}from'react';var f=new WeakMap,S=e=>{let t=f.get(e);return t||(t=new WeakSet,f.set(e,t)),t};function b(e){return signal(e)}function x(e){return computed(e)}function I(e){return effect(e)}function E(){return effectScope()}function V(e){return unstable.asyncComputed(e)}async function W(e){return await unstable.asyncEffect(e).run()}function C(e,t){return unstable.computedArray(e,t)}function v(e){return unstable.computedSet(e)}function A(e){return unstable.equalityComputed(e)}function _(e){return [useSyncExternalStore(n=>{let a=effect(()=>{e.get(),n();});return ()=>a.stop()},()=>e.get(),()=>e.get()),n=>{typeof n=="function"?e.set(n(e.get())):e.set(n);}]}function h(e){return useSyncExternalStore(t=>{let r=effect(()=>{e.get(),t();});return ()=>r.stop()},()=>e.get(),()=>e.get())}function k(e){return t=>{typeof t=="function"?e.set(t(e.get())):e.set(t);}}function w(e){useEffect(()=>{let t=effect(e);return ()=>t.stop()},[e]);}function D(){let e=useMemo(()=>effectScope(),[]);return useEffect(()=>()=>{e.stop();},[e]),e}function M(e){let[t,r]=useState(e.currentValue);return useSyncExternalStore(n=>{let a=effect(()=>{e.currentValue,n();});return ()=>a.stop()},()=>e.currentValue),useEffect(()=>{let n=!0;return (async()=>{let i=await e.get();n&&r(i);})(),()=>{n=!1;}},[e]),t}function O(e){useEffect(()=>{let t=unstable.asyncEffect(e);return ()=>t.stop()},[e]);}function G(e,t){let r=S(e);r.has(e)||(r.add(e),e.set(t));}export{x as createComputed,I as createEffect,b as createSignal,E as createSignalScope,V as unstable_createAsyncComputed,W as unstable_createAsyncEffect,C as unstable_createComputedArray,v as unstable_createComputedSet,A as unstable_createEqualityComputed,M as unstable_useAsyncComputedValue,O as unstable_useAsyncEffect,G as useHydrateSignal,k as useSetSignal,_ as useSignal,w as useSignalEffect,D as useSignalScope,h as useSignalValue};
1
+ import {signal,computed,effect}from'alien-signals';import {useSyncExternalStore,useEffect}from'react';var u=new WeakMap,l=e=>{let t=u.get(e);return t||(t=new WeakSet,u.set(e,t)),t};function d(e){return signal(e)}function x(e,t){let o=localStorage.getItem(e),r;if(o)try{r=JSON.parse(o);}catch(T){console.error(`Error parsing localStorage for key "${e}", using initial value.`,T),r=t;}else r=t;let n=d(r);return p(()=>{localStorage.setItem(e,JSON.stringify(n()));}),n}function b(e){return computed(e)}function p(e){return effect(e)}function m(e){return [useSyncExternalStore(r=>{let n=effect(()=>{e(),r();});return ()=>n()},()=>e(),()=>e()),r=>{e(typeof r=="function"?r(e()):r);}]}function v(e){return useSyncExternalStore(t=>{let o=effect(()=>{e(),t();});return ()=>o()},()=>e(),()=>e())}function V(e){return t=>{e(typeof t=="function"?t(e()):t);}}function h(e){useEffect(()=>{let t=effect(e);return ()=>t()},[e]);}function E(e,t){let o=l(e);o.has(e)||(o.add(e),e(t));}export{b as createComputed,p as createEffect,d as createSignal,x as createSignalStorage,E as useHydrateSignal,V as useSetSignal,m as useSignal,h as useSignalEffect,v as useSignalValue};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reactjs-signal",
3
- "version": "1.0.5",
3
+ "version": "1.1.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",
@@ -42,7 +42,6 @@
42
42
  "commit-msg": "npm run verify-commit"
43
43
  },
44
44
  "peerDependencies": {
45
- "alien-signals": ">=0.4",
46
45
  "react": ">=18"
47
46
  },
48
47
  "bugs": {
@@ -52,5 +51,8 @@
52
51
  "repository": {
53
52
  "type": "git",
54
53
  "url": "git+https://github.com/hunghg255/reactjs-signal.git"
54
+ },
55
+ "dependencies": {
56
+ "alien-signals": "^1.0.1"
55
57
  }
56
58
  }