reactjs-signal 1.0.1 → 1.0.2
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 +382 -1
- package/package.json +9 -1
package/README.md
CHANGED
|
@@ -1 +1,382 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://www.npmjs.com/package/reactjs-signal" target="_blank" rel="noopener noreferrer">
|
|
3
|
+
<img src="https://api.iconify.design/uil:comment-verify.svg?color=%23b3ff75" alt="logo" width='100'/></a>
|
|
4
|
+
</p>
|
|
5
|
+
|
|
6
|
+
<p align="center">
|
|
7
|
+
Share Store State with Signal Pattern
|
|
8
|
+
</p>
|
|
9
|
+
|
|
10
|
+
<p align="center">
|
|
11
|
+
<a href="https://www.npmjs.com/package/reactjs-signal" target="_blank" rel="noopener noreferrer"><img src="https://badge.fury.io/js/reactjs-signal.svg" alt="NPM Version" /></a>
|
|
12
|
+
<a href="https://www.npmjs.com/package/reactjs-signal" target="_blank" rel="noopener noreferrer"><img src="https://img.shields.io/npm/dt/reactjs-signal.svg?logo=npm" alt="NPM Downloads" /></a>
|
|
13
|
+
<a href="https://bundlephobia.com/result?p=reactjs-signal" target="_blank" rel="noopener noreferrer"><img src="https://img.shields.io/bundlephobia/minzip/reactjs-signal" alt="Minizip" /></a>
|
|
14
|
+
<a href="https://github.com/hunghg255/reactjs-signal/graphs/contributors" target="_blank" rel="noopener noreferrer"><img src="https://img.shields.io/badge/all_contributors-1-orange.svg" alt="Contributors" /></a>
|
|
15
|
+
<a href="https://github.com/hunghg255/reactjs-signal/blob/main/LICENSE" target="_blank" rel="noopener noreferrer"><img src="https://badgen.net/github/license/hunghg255/reactjs-signal" alt="License" /></a>
|
|
16
|
+
</p>
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install reactjs-signal
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
import React from 'react';
|
|
29
|
+
import { useSignal } from 'reactjs-signal';
|
|
30
|
+
|
|
31
|
+
const App = () => {
|
|
32
|
+
const [state, setState] = useSignal({ count: 0 });
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<div>
|
|
36
|
+
<h1>{state.count}</h1>
|
|
37
|
+
<button onClick={() => setState({ count: state.count + 1 })}>Increment</button>
|
|
38
|
+
</div>
|
|
39
|
+
);
|
|
40
|
+
};
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## API Documentation
|
|
44
|
+
|
|
45
|
+
### `createSignal`
|
|
46
|
+
|
|
47
|
+
Creates a writable Alien Signal.
|
|
48
|
+
|
|
49
|
+
#### Example
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
const countSignal = createSignal(0);
|
|
53
|
+
countSignal.set(10); // sets the value to 10
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
#### Parameters
|
|
57
|
+
|
|
58
|
+
- `initialValue` (`T`): The initial value of the signal.
|
|
59
|
+
|
|
60
|
+
#### Returns
|
|
61
|
+
|
|
62
|
+
- `IWritableSignal<T>`: The created Alien Signal.
|
|
63
|
+
|
|
64
|
+
### `createComputed`
|
|
65
|
+
|
|
66
|
+
Creates a computed Alien Signal based on a getter function.
|
|
67
|
+
|
|
68
|
+
#### Example
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
const countSignal = createSignal(1);
|
|
72
|
+
const doubleSignal = createComputed(() => countSignal.get() * 2);
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
#### Parameters
|
|
76
|
+
|
|
77
|
+
- `fn` (`() => T`): A getter function returning a computed value.
|
|
78
|
+
|
|
79
|
+
#### Returns
|
|
80
|
+
|
|
81
|
+
- `ISignal<T>`: The created computed signal.
|
|
82
|
+
|
|
83
|
+
### `createEffect`
|
|
84
|
+
|
|
85
|
+
Creates a side effect in Alien Signals.
|
|
86
|
+
|
|
87
|
+
#### Example
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
const countSignal = createSignal(1);
|
|
91
|
+
createEffect(() => {
|
|
92
|
+
console.log('Count is', countSignal.get());
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
#### Parameters
|
|
97
|
+
|
|
98
|
+
- `fn` (`() => T`): A function that will run whenever its tracked signals update.
|
|
99
|
+
|
|
100
|
+
#### Returns
|
|
101
|
+
|
|
102
|
+
- `Effect<T>`: The created effect object.
|
|
103
|
+
|
|
104
|
+
### `createSignalScope`
|
|
105
|
+
|
|
106
|
+
Creates an Alien Signals effect scope. This scope can manage multiple effects, allowing you to stop or start them together.
|
|
107
|
+
|
|
108
|
+
#### Example
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
const scope = createSignalScope();
|
|
112
|
+
scope.run(() => {
|
|
113
|
+
// create effects in here...
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
#### Returns
|
|
118
|
+
|
|
119
|
+
- `EffectScope`: The created effect scope.
|
|
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
|
+
### `useSignal`
|
|
225
|
+
|
|
226
|
+
React hook returning `[value, setValue]` for a given Alien Signal. Uses `useSyncExternalStore` for concurrency-safe re-renders.
|
|
227
|
+
|
|
228
|
+
#### Example
|
|
229
|
+
|
|
230
|
+
```typescript
|
|
231
|
+
const countSignal = createSignal(0);
|
|
232
|
+
function Counter() {
|
|
233
|
+
const [count, setCount] = useSignal(countSignal);
|
|
234
|
+
return <button onClick={() => setCount(count + 1)}>{count}</button>;
|
|
235
|
+
}
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
#### Parameters
|
|
239
|
+
|
|
240
|
+
- `alienSignal` (`IWritableSignal<T>`): The signal to read/write.
|
|
241
|
+
|
|
242
|
+
#### Returns
|
|
243
|
+
|
|
244
|
+
- `[T, (val: T | ((oldVal: T) => T)) => void]`: A tuple `[currentValue, setValue]`.
|
|
245
|
+
|
|
246
|
+
### `useSignalValue`
|
|
247
|
+
|
|
248
|
+
React hook returning only the current value of an Alien Signal (or computed). No setter is provided.
|
|
249
|
+
|
|
250
|
+
#### Example
|
|
251
|
+
|
|
252
|
+
```typescript
|
|
253
|
+
const countSignal = createSignal(0);
|
|
254
|
+
const doubleSignal = createComputed(() => countSignal.get() * 2);
|
|
255
|
+
function Display() {
|
|
256
|
+
const count = useSignalValue(countSignal);
|
|
257
|
+
const double = useSignalValue(doubleSignal);
|
|
258
|
+
return <div>{count}, {double}</div>;
|
|
259
|
+
}
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
#### Parameters
|
|
263
|
+
|
|
264
|
+
- `alienSignal` (`IWritableSignal<T>`): The signal to read.
|
|
265
|
+
|
|
266
|
+
#### Returns
|
|
267
|
+
|
|
268
|
+
- `T`: The current value.
|
|
269
|
+
|
|
270
|
+
### `useSetSignal`
|
|
271
|
+
|
|
272
|
+
React hook returning only a setter function for an Alien Signal. No current value is provided, similar to Jotai's `useSetAtom`.
|
|
273
|
+
|
|
274
|
+
#### Example
|
|
275
|
+
|
|
276
|
+
```typescript
|
|
277
|
+
const countSignal = createSignal(0);
|
|
278
|
+
function Incrementor() {
|
|
279
|
+
const setCount = useSetSignal(countSignal);
|
|
280
|
+
return <button onClick={() => setCount((c) => c + 1)}>+1</button>;
|
|
281
|
+
}
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
#### Parameters
|
|
285
|
+
|
|
286
|
+
- `alienSignal` (`IWritableSignal<T>`): The signal to write.
|
|
287
|
+
|
|
288
|
+
#### Returns
|
|
289
|
+
|
|
290
|
+
- `(val: T | ((oldVal: T) => T)) => void`: A setter function.
|
|
291
|
+
|
|
292
|
+
### `useSignalEffect`
|
|
293
|
+
|
|
294
|
+
React hook for running a side effect whenever Alien Signals' dependencies used in `fn` change. The effect is cleaned up on component unmount.
|
|
295
|
+
|
|
296
|
+
#### Example
|
|
297
|
+
|
|
298
|
+
```typescript
|
|
299
|
+
function Logger() {
|
|
300
|
+
useSignalEffect(() => {
|
|
301
|
+
console.log('Signal changed:', someSignal.get());
|
|
302
|
+
});
|
|
303
|
+
return null;
|
|
304
|
+
}
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
#### Parameters
|
|
308
|
+
|
|
309
|
+
- `fn` (`() => void`): The effect function to run.
|
|
310
|
+
|
|
311
|
+
### `useSignalScope`
|
|
312
|
+
|
|
313
|
+
React hook for managing an Alien Signals effect scope. All signals/effects created inside this scope run when the component mounts, and are stopped automatically when the component unmounts.
|
|
314
|
+
|
|
315
|
+
#### Example
|
|
316
|
+
|
|
317
|
+
```typescript
|
|
318
|
+
function ScopedEffects() {
|
|
319
|
+
const scope = useSignalScope();
|
|
320
|
+
useEffect(() => {
|
|
321
|
+
scope.run(() => {
|
|
322
|
+
createEffect(() => {
|
|
323
|
+
console.log('Scoped effect:', someSignal.get());
|
|
324
|
+
});
|
|
325
|
+
});
|
|
326
|
+
}, [scope]);
|
|
327
|
+
return null;
|
|
328
|
+
}
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
#### Returns
|
|
332
|
+
|
|
333
|
+
- `EffectScope`: The created effect scope.
|
|
334
|
+
|
|
335
|
+
### `unstable_useAsyncComputedValue`
|
|
336
|
+
|
|
337
|
+
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.
|
|
338
|
+
|
|
339
|
+
#### Example
|
|
340
|
+
|
|
341
|
+
```typescript
|
|
342
|
+
const asyncSignal = createAsyncComputed<number>(async function*() {
|
|
343
|
+
const val = someSignal.get();
|
|
344
|
+
yield Promise.resolve(someSignal); // track async dep
|
|
345
|
+
return val * 2;
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
function AsyncDisplay() {
|
|
349
|
+
const value = useAsyncComputedValue(asyncSignal);
|
|
350
|
+
return <div>Value: {String(value)}</div>;
|
|
351
|
+
}
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
#### Parameters
|
|
355
|
+
|
|
356
|
+
- `alienAsyncComp` (`AsyncComputed<T>`): The async computed signal to read.
|
|
357
|
+
|
|
358
|
+
#### Returns
|
|
359
|
+
|
|
360
|
+
- `T | undefined`: The resolved value (or undefined if not yet resolved).
|
|
361
|
+
|
|
362
|
+
### `unstable_useAsyncEffect`
|
|
363
|
+
|
|
364
|
+
React hook to run an asynchronous effect whenever the component mounts, cleaning up when it unmounts.
|
|
365
|
+
|
|
366
|
+
#### Example
|
|
367
|
+
|
|
368
|
+
```typescript
|
|
369
|
+
useAsyncEffect(async function* () {
|
|
370
|
+
yield someDependency;
|
|
371
|
+
console.log('Async side effect complete!');
|
|
372
|
+
});
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
#### Parameters
|
|
376
|
+
|
|
377
|
+
- `fn` (`() => AsyncGenerator<Dependency, T>`): An async generator representing the effect logic.
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
## Refer
|
|
381
|
+
|
|
382
|
+
React Alien Signals is a **TypeScript** library that provides hooks built on top of [Alien Signals](https://github.com/stackblitz/alien-signals).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "reactjs-signal",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -44,5 +44,13 @@
|
|
|
44
44
|
"peerDependencies": {
|
|
45
45
|
"alien-signals": ">=0.4",
|
|
46
46
|
"react": ">=18"
|
|
47
|
+
},
|
|
48
|
+
"bugs": {
|
|
49
|
+
"url": "https://github.com/hunghg255/reactjs-signal/issues"
|
|
50
|
+
},
|
|
51
|
+
"homepage": "https://github.com/hunghg255/reactjs-signal#readme",
|
|
52
|
+
"repository": {
|
|
53
|
+
"type": "git",
|
|
54
|
+
"url": "git+https://github.com/hunghg255/reactjs-signal.git"
|
|
47
55
|
}
|
|
48
56
|
}
|