reactjs-signal 2.0.1 → 2.0.4
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 +15 -0
- package/dist/index.d.ts +32 -16
- package/dist/index.js +1 -1
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -207,6 +207,21 @@ useHydrateSignal(countSignal, 10);
|
|
|
207
207
|
|
|
208
208
|
- `EffectScope`: The created effect scope.
|
|
209
209
|
|
|
210
|
+
### `getSignal`
|
|
211
|
+
|
|
212
|
+
Gets the current value of an Alien Signal without subscribing to updates.
|
|
213
|
+
|
|
214
|
+
- Use case: reading signal value outside React components.
|
|
215
|
+
|
|
216
|
+
#### Example
|
|
217
|
+
|
|
218
|
+
```typescript
|
|
219
|
+
const countSignal = createSignal(0);
|
|
220
|
+
const { value, setValue } = getSignal(countSignal);
|
|
221
|
+
console.log(value()); // current value
|
|
222
|
+
setValue(10); // set value to 10
|
|
223
|
+
```
|
|
224
|
+
|
|
210
225
|
|
|
211
226
|
## Refer
|
|
212
227
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { signal, computed, effect } from 'alien-signals';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
*
|
|
3
5
|
* React Alien Signals is a **TypeScript** library that provides hooks built on top of [Alien Signals](https://github.com/stackblitz/alien-signals).
|
|
@@ -5,6 +7,7 @@
|
|
|
5
7
|
*
|
|
6
8
|
* @module reactjs-signal
|
|
7
9
|
*/
|
|
10
|
+
|
|
8
11
|
type TWritableSignal<T> = {
|
|
9
12
|
(): T;
|
|
10
13
|
(value: T): void;
|
|
@@ -22,16 +25,7 @@ type TWritableSignal<T> = {
|
|
|
22
25
|
* @param {T} initialValue - The initial value of the signal.
|
|
23
26
|
* @returns {TWritableSignal<T>} The created Alien Signal.
|
|
24
27
|
*/
|
|
25
|
-
declare
|
|
26
|
-
/**
|
|
27
|
-
* Creates a writable Alien Signal that persists its value in localStorage.
|
|
28
|
-
*
|
|
29
|
-
* @template T - The type of the signal value.
|
|
30
|
-
* @param {string} key - The localStorage key to use for persistence.
|
|
31
|
-
* @param {T} initialValue - The initial value of the signal.
|
|
32
|
-
* @returns {TWritableSignal<T>} The created Alien Signal.
|
|
33
|
-
*/
|
|
34
|
-
declare function createSignalStorage<T>(key: string, initialValue: T): TWritableSignal<T>;
|
|
28
|
+
declare const createSignal: typeof signal;
|
|
35
29
|
/**
|
|
36
30
|
* Creates a computed Alien Signal based on a getter function.
|
|
37
31
|
*
|
|
@@ -45,7 +39,7 @@ declare function createSignalStorage<T>(key: string, initialValue: T): TWritable
|
|
|
45
39
|
* @param {() => T} fn - A getter function returning a computed value.
|
|
46
40
|
* @returns {ISignal<T>} The created computed signal.
|
|
47
41
|
*/
|
|
48
|
-
declare
|
|
42
|
+
declare const createComputed: typeof computed;
|
|
49
43
|
/**
|
|
50
44
|
* Creates a side effect in Alien Signals.
|
|
51
45
|
*
|
|
@@ -61,7 +55,7 @@ declare function createComputed<T>(fn: () => T): () => T;
|
|
|
61
55
|
* @param {() => T} fn - A function that will run whenever its tracked signals update.
|
|
62
56
|
* @returns {Effect<T>} The created effect object.
|
|
63
57
|
*/
|
|
64
|
-
declare
|
|
58
|
+
declare const createEffect: typeof effect;
|
|
65
59
|
/**
|
|
66
60
|
* React hook returning `[value, setValue]` for a given Alien Signal.
|
|
67
61
|
* Uses useSyncExternalStore for concurrency-safe re-renders.
|
|
@@ -137,14 +131,36 @@ declare function useSetSignal<T>(alienSignal: TWritableSignal<T>): (val: T | ((o
|
|
|
137
131
|
declare function useSignalEffect(fn: () => void): void;
|
|
138
132
|
/**
|
|
139
133
|
* React hook to initialize a signal with a value when hydrating from server.
|
|
140
|
-
*
|
|
141
|
-
* @
|
|
142
|
-
*
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```typescript
|
|
137
|
+
* const countSignal = createSignal(0);
|
|
138
|
+
* useHydrateSignal(countSignal, 10);
|
|
139
|
+
* ```
|
|
143
140
|
*
|
|
144
141
|
* @template T - The type of the signal value.
|
|
145
142
|
* @param {TWritableSignal<T>} alienSignal - The signal to hydrate.
|
|
146
143
|
* @param {T} value - The value to hydrate the signal with.
|
|
147
144
|
*/
|
|
148
145
|
declare function useHydrateSignal<T>(alienSignal: TWritableSignal<T>, value: T): void;
|
|
146
|
+
/**
|
|
147
|
+
* Utility function to get the current value and setter of an Alien Signal.
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```typescript
|
|
151
|
+
* const countSignal = createSignal(0);
|
|
152
|
+
* const { value, setValue } = getSignal(countSignal);
|
|
153
|
+
* console.log(value()); // current value
|
|
154
|
+
* setValue(10); // set value to 10
|
|
155
|
+
* ```
|
|
156
|
+
*
|
|
157
|
+
* @template T - The type of the signal value.
|
|
158
|
+
* @param {TWritableSignal<T>} alienSignal - The signal to read/write.
|
|
159
|
+
* @returns {{ value: () => T; setValue: (val: T | ((oldVal: T) => T)) => void }} An object with `value` and `setValue`.
|
|
160
|
+
*/
|
|
161
|
+
declare function getSignal<T>(alienSignal: TWritableSignal<T>): {
|
|
162
|
+
value: () => T;
|
|
163
|
+
setValue: (val: T | ((oldVal: T) => T)) => void;
|
|
164
|
+
};
|
|
149
165
|
|
|
150
|
-
export { type TWritableSignal, createComputed, createEffect, createSignal,
|
|
166
|
+
export { type TWritableSignal, createComputed, createEffect, createSignal, getSignal, useHydrateSignal, useSetSignal, useSignal, useSignalEffect, useSignalValue };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import {useSyncExternalStore,useEffect}from'react';import {
|
|
1
|
+
import {useSyncExternalStore,useCallback,useEffect}from'react';import {computed,effect,signal}from'alien-signals';var u=new WeakMap;function l(t){let e=u.get(t);return e||(e=new WeakSet,u.set(t,e)),e}var y=signal,W=computed,x=effect;function V(t){let e=useSyncExternalStore(r=>{let c=effect(()=>{t(),r();});return ()=>c()},()=>t(),()=>t()),o=useCallback(r=>{t(typeof r=="function"?r(t()):r);},[]);return [e,o]}function b(t){return useSyncExternalStore(e=>{let o=effect(()=>{t(),e();});return ()=>o()},()=>t(),()=>t())}function S(t){return useCallback(e=>{t(typeof e=="function"?e(t()):e);},[])}function m(t){useEffect(()=>{let e=effect(t);return ()=>e()},[]);}function g(t,e){let o=l(t);o.has(t)||(o.add(t),t(e));}function v(t){return {value:()=>t(),setValue:o=>{t(typeof o=="function"?o(t()):o);}}}export{W as createComputed,x as createEffect,y as createSignal,v as getSignal,g as useHydrateSignal,S as useSetSignal,V as useSignal,m as useSignalEffect,b as useSignalValue};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "reactjs-signal",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.
|
|
4
|
+
"version": "2.0.4",
|
|
5
5
|
"description": "",
|
|
6
6
|
"author": "",
|
|
7
7
|
"license": "ISC",
|
|
@@ -30,20 +30,20 @@
|
|
|
30
30
|
"files": [
|
|
31
31
|
"dist"
|
|
32
32
|
],
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsup",
|
|
35
|
+
"dev": "tsup --watch"
|
|
36
|
+
},
|
|
33
37
|
"dependencies": {
|
|
34
|
-
"alien-signals": "^
|
|
38
|
+
"alien-signals": "^3.1.2"
|
|
35
39
|
},
|
|
36
40
|
"peerDependencies": {
|
|
37
|
-
"alien-signals": ">=
|
|
41
|
+
"alien-signals": ">=3.1.2",
|
|
38
42
|
"react": ">=18"
|
|
39
43
|
},
|
|
40
44
|
"devDependencies": {
|
|
41
45
|
"@types/react": "^19.0.2",
|
|
42
46
|
"tsup": "^8.0.1",
|
|
43
47
|
"typescript": "^5.3.3"
|
|
44
|
-
},
|
|
45
|
-
"scripts": {
|
|
46
|
-
"build": "tsup",
|
|
47
|
-
"dev": "tsup --watch"
|
|
48
48
|
}
|
|
49
|
-
}
|
|
49
|
+
}
|