@sigrea/react 0.4.0 → 0.6.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
@@ -1,6 +1,6 @@
1
1
  # @sigrea/react
2
2
 
3
- `@sigrea/react` adapts [@sigrea/core](https://www.npmjs.com/package/@sigrea/core) molecule modules and signals for use in React components. It binds scope-aware lifecycles to `useEffect`, synchronizes signal subscriptions with React rendering, and provides hooks for both shallow and deep reactivity.
3
+ `@sigrea/react` adapts [@sigrea/core](https://www.npmjs.com/package/@sigrea/core) molecule modules and signals for use in React components. It binds scope-aware lifecycles to React commits, synchronizes signal subscriptions with React rendering, and provides hooks for both shallow and deep reactivity.
4
4
 
5
5
  - **Signal subscriptions.** `useSignal` subscribes to signals and computed values, triggering re-renders when they change.
6
6
  - **Computed subscriptions.** `useComputed` subscribes to computed values and memoizes them per component instance.
@@ -30,7 +30,7 @@
30
30
  npm install @sigrea/react @sigrea/core react react-dom
31
31
  ```
32
32
 
33
- Requires React 18+ and Node.js 20 or later.
33
+ Requires React 18+ and Node.js 24 or later.
34
34
 
35
35
  ## Quick Start
36
36
 
@@ -51,32 +51,50 @@ export function CounterLabel() {
51
51
  ### Bridge Framework-Agnostic Molecules
52
52
 
53
53
  ```tsx
54
- import { molecule, signal } from "@sigrea/core";
54
+ import { molecule, readonly, signal } from "@sigrea/core";
55
55
  import { useMolecule, useSignal } from "@sigrea/react";
56
56
 
57
- const CounterMolecule = molecule((props: { initialCount: number }) => {
57
+ type CounterProps = {
58
+ initialCount: number;
59
+ initialStep: number;
60
+ };
61
+
62
+ const CounterMolecule = molecule((props: CounterProps) => {
58
63
  const count = signal(props.initialCount);
64
+ const step = signal(props.initialStep);
59
65
 
60
- const increment = () => {
61
- count.value += 1;
62
- };
66
+ function setStep(next: number) {
67
+ step.value = next;
68
+ }
63
69
 
64
- const reset = () => {
70
+ function increment() {
71
+ count.value += step.value;
72
+ }
73
+
74
+ function reset() {
65
75
  count.value = props.initialCount;
66
- };
76
+ }
67
77
 
68
- return { count, increment, reset };
78
+ return {
79
+ count: readonly(count),
80
+ step: readonly(step),
81
+ setStep,
82
+ increment,
83
+ reset,
84
+ };
69
85
  });
70
86
 
71
- export function Counter(props: { initialCount: number }) {
87
+ export function Counter(props: CounterProps) {
72
88
  const counter = useMolecule(CounterMolecule, props);
73
- const value = useSignal(counter.count);
89
+ const count = useSignal(counter.count);
90
+ const step = useSignal(counter.step);
74
91
 
75
92
  return (
76
93
  <div>
77
- <span>{value}</span>
94
+ <span>{count}</span>
78
95
  <button onClick={counter.increment}>Increment</button>
79
96
  <button onClick={counter.reset}>Reset</button>
97
+ <button onClick={() => counter.setStep(step + 1)}>Step +</button>
80
98
  </div>
81
99
  );
82
100
  }
@@ -88,7 +106,7 @@ export function Counter(props: { initialCount: number }) {
88
106
  import { deepSignal } from "@sigrea/core";
89
107
  import { useDeepSignal } from "@sigrea/react";
90
108
 
91
- const form = deepSignal({ name: "Sigrea" });
109
+ const form = deepSignal({ name: "Mendako" });
92
110
 
93
111
  export function ProfileForm() {
94
112
  const state = useDeepSignal(form);
@@ -112,10 +130,12 @@ export function ProfileForm() {
112
130
  ### useSignal
113
131
 
114
132
  ```tsx
115
- function useSignal<T>(signal: Signal<T> | ReadonlySignal<T>): T
133
+ function useSignal<T>(
134
+ signal: Signal<T> | ReadonlySignal<T> | Computed<T>
135
+ ): T
116
136
  ```
117
137
 
118
- Subscribes to a signal or computed value and returns its current value. The component re-renders when the signal changes.
138
+ Subscribes to a signal or computed value and returns its current value. The component re-renders when the source changes.
119
139
 
120
140
  ### useComputed
121
141
 
@@ -123,7 +143,7 @@ Subscribes to a signal or computed value and returns its current value. The comp
123
143
  function useComputed<T>(source: Computed<T>): T
124
144
  ```
125
145
 
126
- Subscribes to a computed value and returns its current value. The component re-renders when the computed value changes, and the subscription is cleaned up when the component unmounts.
146
+ Subscribes to a computed value and returns its current value. This behaves like `useSignal(source)` for computed sources, but keeps the call site explicit when the source is known to be computed.
127
147
 
128
148
  ### useDeepSignal
129
149
 
@@ -136,13 +156,28 @@ Exposes a deep signal object for direct mutation within the component. Updates t
136
156
  ### useMolecule
137
157
 
138
158
  ```tsx
139
- function useMolecule<TProps, TReturn>(
140
- molecule: MoleculeFactory<TProps, TReturn>,
141
- props?: TProps
142
- ): TReturn
159
+ function useMolecule<TReturn extends object, TProps extends object | void = void>(
160
+ molecule: MoleculeFactory<TReturn, TProps>,
161
+ ...args: MoleculeArgs<TProps>
162
+ ): MoleculeInstance<TReturn>
143
163
  ```
144
164
 
145
- Mounts a molecule factory and returns its public API. The molecule's scope is bound to the component lifecycle: `onMount` callbacks run after the component mounts, and `onUnmount` callbacks run before it unmounts.
165
+ Mounts a molecule factory and returns its MoleculeInstance. The molecule's scope is bound to the component lifecycle: `onMount` callbacks run after the component mounts, and `onUnmount` callbacks run before it unmounts.
166
+
167
+ **Lifecycle Timing**
168
+
169
+ Molecule lifecycles are bound to React commits for precise timing control:
170
+
171
+ - In **browser environments**, molecule mounting happens synchronously after the component commits but before paint (via `useLayoutEffect`). This matches Vue 3's `onMounted` timing, ensuring consistent behavior across frameworks.
172
+ - In **SSR environments**, `useMolecule` supports both `renderToString` and streaming server rendering (`renderToPipeableStream`, `renderToReadableStream`). The molecule instance is created during render so components can read its state, but it is never mounted on the server.
173
+ - `onMount`, `watch`, and `watchEffect` registered during setup do not run during server rendering.
174
+ - After a **server render** finishes, the unmounted molecule instance is disposed automatically in a microtask so setup-scope `onDispose` cleanups do not leak across requests.
175
+
176
+ This design ensures that `onMount` callbacks and `watch` effects activate at the right moment—early enough to set up subscriptions before the first paint, yet safely after the component has committed to the DOM.
177
+
178
+ **Props Handling**
179
+
180
+ Props are treated as an initial snapshot. Updating component props does not recreate the molecule instance or update the snapshot; model dynamic values via signals or explicit molecule methods (for example, `setStep`).
146
181
 
147
182
  ## Testing
148
183
 
@@ -189,7 +224,7 @@ createRoot(document.getElementById("root")!).render(<App />);
189
224
 
190
225
  ## Development
191
226
 
192
- This repo targets Node.js 20 or later.
227
+ This repo targets Node.js 24 or later.
193
228
 
194
229
  If you use mise:
195
230
 
package/dist/index.cjs CHANGED
@@ -3,28 +3,51 @@
3
3
  const react = require('react');
4
4
  const core = require('@sigrea/core');
5
5
 
6
+ const useIsomorphicLayoutEffect = typeof window !== "undefined" ? react.useLayoutEffect : react.useEffect;
7
+ const isServerEnvironment = typeof window === "undefined";
8
+ function schedulePendingDispose(stateRef, instance, token) {
9
+ queueMicrotask(() => {
10
+ const current = stateRef.current;
11
+ if (current === void 0 || current.instance !== instance || current.subscribers > 0 || current.disposed || current.pendingDisposeToken !== token) {
12
+ return;
13
+ }
14
+ current.disposed = true;
15
+ current.pendingDisposeToken = null;
16
+ stateRef.current = void 0;
17
+ core.disposeMolecule(instance);
18
+ });
19
+ }
6
20
  function useMolecule(molecule, ...args) {
7
21
  const props = args.length === 0 ? void 0 : args[0];
22
+ if (props !== void 0 && (typeof props !== "object" || props === null)) {
23
+ throw new TypeError("useMolecule props must be an object.");
24
+ }
8
25
  const stateRef = react.useRef(
9
26
  void 0
10
27
  );
11
28
  const currentState = stateRef.current;
12
- const shouldRemount = currentState === void 0 || currentState.molecule !== molecule || !Object.is(currentState.props, props);
29
+ const shouldRemount = currentState === void 0 || currentState.molecule !== molecule;
13
30
  if (shouldRemount) {
14
31
  if (currentState !== void 0) {
15
32
  currentState.pendingDisposeToken = null;
16
33
  core.disposeMolecule(currentState.instance);
17
34
  stateRef.current = void 0;
18
35
  }
19
- const moleculeArgs = props === void 0 ? [] : [props];
20
- stateRef.current = {
36
+ const snapshot = props === void 0 ? void 0 : { ...props };
37
+ const moleculeArgs = snapshot === void 0 ? [] : [snapshot];
38
+ const nextState = {
21
39
  instance: molecule(...moleculeArgs),
22
40
  molecule,
23
- props,
24
41
  subscribers: 0,
25
42
  disposed: false,
26
43
  pendingDisposeToken: null
27
44
  };
45
+ stateRef.current = nextState;
46
+ if (isServerEnvironment) {
47
+ const token = Symbol("pending-server-dispose");
48
+ nextState.pendingDisposeToken = token;
49
+ schedulePendingDispose(stateRef, nextState.instance, token);
50
+ }
28
51
  }
29
52
  const state = stateRef.current;
30
53
  if (state === void 0) {
@@ -33,7 +56,7 @@ function useMolecule(molecule, ...args) {
33
56
  );
34
57
  }
35
58
  const instance = state.instance;
36
- react.useEffect(() => {
59
+ useIsomorphicLayoutEffect(() => {
37
60
  const state2 = stateRef.current;
38
61
  if (state2 === void 0 || state2.instance !== instance) {
39
62
  return () => {
@@ -43,6 +66,9 @@ function useMolecule(molecule, ...args) {
43
66
  state2.pendingDisposeToken = null;
44
67
  }
45
68
  state2.subscribers += 1;
69
+ if (state2.subscribers === 1) {
70
+ core.mountMolecule(instance);
71
+ }
46
72
  return () => {
47
73
  const latest = stateRef.current;
48
74
  if (latest === void 0 || latest.instance !== instance) {
@@ -54,18 +80,10 @@ function useMolecule(molecule, ...args) {
54
80
  latest.subscribers = 0;
55
81
  }
56
82
  if (!latest.disposed && latest.subscribers === 0) {
83
+ core.unmountMolecule(instance);
57
84
  const token = Symbol("pending-dispose");
58
85
  latest.pendingDisposeToken = token;
59
- queueMicrotask(() => {
60
- const current = stateRef.current;
61
- if (current === void 0 || current.instance !== instance || current.subscribers > 0 || current.disposed || current.pendingDisposeToken !== token) {
62
- return;
63
- }
64
- current.disposed = true;
65
- current.pendingDisposeToken = null;
66
- stateRef.current = void 0;
67
- core.disposeMolecule(instance);
68
- });
86
+ schedulePendingDispose(stateRef, instance, token);
69
87
  }
70
88
  };
71
89
  }, [instance]);
package/dist/index.d.cts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { MoleculeFactory, MoleculeArgs, MoleculeInstance, Signal, ReadonlySignal, Computed, DeepSignal, SnapshotHandler } from '@sigrea/core';
2
2
 
3
- declare function useMolecule<TReturn extends object, TProps = void>(molecule: MoleculeFactory<TReturn, TProps>, ...args: MoleculeArgs<TProps>): MoleculeInstance<TReturn>;
3
+ declare function useMolecule<TReturn extends object, TProps extends object | void = void>(molecule: MoleculeFactory<TReturn, TProps>, ...args: MoleculeArgs<TProps>): MoleculeInstance<TReturn>;
4
4
 
5
- type ReadableSignal<T> = Signal<T> | ReadonlySignal<T>;
5
+ type ReadableSignal<T> = Signal<T> | ReadonlySignal<T> | Computed<T>;
6
6
  declare function useSignal<T>(source: ReadableSignal<T>): T;
7
7
 
8
8
  declare function useComputed<T>(source: Computed<T>): T;
package/dist/index.d.mts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { MoleculeFactory, MoleculeArgs, MoleculeInstance, Signal, ReadonlySignal, Computed, DeepSignal, SnapshotHandler } from '@sigrea/core';
2
2
 
3
- declare function useMolecule<TReturn extends object, TProps = void>(molecule: MoleculeFactory<TReturn, TProps>, ...args: MoleculeArgs<TProps>): MoleculeInstance<TReturn>;
3
+ declare function useMolecule<TReturn extends object, TProps extends object | void = void>(molecule: MoleculeFactory<TReturn, TProps>, ...args: MoleculeArgs<TProps>): MoleculeInstance<TReturn>;
4
4
 
5
- type ReadableSignal<T> = Signal<T> | ReadonlySignal<T>;
5
+ type ReadableSignal<T> = Signal<T> | ReadonlySignal<T> | Computed<T>;
6
6
  declare function useSignal<T>(source: ReadableSignal<T>): T;
7
7
 
8
8
  declare function useComputed<T>(source: Computed<T>): T;
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { MoleculeFactory, MoleculeArgs, MoleculeInstance, Signal, ReadonlySignal, Computed, DeepSignal, SnapshotHandler } from '@sigrea/core';
2
2
 
3
- declare function useMolecule<TReturn extends object, TProps = void>(molecule: MoleculeFactory<TReturn, TProps>, ...args: MoleculeArgs<TProps>): MoleculeInstance<TReturn>;
3
+ declare function useMolecule<TReturn extends object, TProps extends object | void = void>(molecule: MoleculeFactory<TReturn, TProps>, ...args: MoleculeArgs<TProps>): MoleculeInstance<TReturn>;
4
4
 
5
- type ReadableSignal<T> = Signal<T> | ReadonlySignal<T>;
5
+ type ReadableSignal<T> = Signal<T> | ReadonlySignal<T> | Computed<T>;
6
6
  declare function useSignal<T>(source: ReadableSignal<T>): T;
7
7
 
8
8
  declare function useComputed<T>(source: Computed<T>): T;
package/dist/index.mjs CHANGED
@@ -1,28 +1,51 @@
1
- import { useRef, useEffect, useCallback, useSyncExternalStore, useMemo } from 'react';
2
- import { disposeMolecule, createSignalHandler, createComputedHandler, createDeepSignalHandler } from '@sigrea/core';
1
+ import { useRef, useLayoutEffect, useEffect, useCallback, useSyncExternalStore, useMemo } from 'react';
2
+ import { disposeMolecule, mountMolecule, unmountMolecule, createSignalHandler, createComputedHandler, createDeepSignalHandler } from '@sigrea/core';
3
3
 
4
+ const useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
5
+ const isServerEnvironment = typeof window === "undefined";
6
+ function schedulePendingDispose(stateRef, instance, token) {
7
+ queueMicrotask(() => {
8
+ const current = stateRef.current;
9
+ if (current === void 0 || current.instance !== instance || current.subscribers > 0 || current.disposed || current.pendingDisposeToken !== token) {
10
+ return;
11
+ }
12
+ current.disposed = true;
13
+ current.pendingDisposeToken = null;
14
+ stateRef.current = void 0;
15
+ disposeMolecule(instance);
16
+ });
17
+ }
4
18
  function useMolecule(molecule, ...args) {
5
19
  const props = args.length === 0 ? void 0 : args[0];
20
+ if (props !== void 0 && (typeof props !== "object" || props === null)) {
21
+ throw new TypeError("useMolecule props must be an object.");
22
+ }
6
23
  const stateRef = useRef(
7
24
  void 0
8
25
  );
9
26
  const currentState = stateRef.current;
10
- const shouldRemount = currentState === void 0 || currentState.molecule !== molecule || !Object.is(currentState.props, props);
27
+ const shouldRemount = currentState === void 0 || currentState.molecule !== molecule;
11
28
  if (shouldRemount) {
12
29
  if (currentState !== void 0) {
13
30
  currentState.pendingDisposeToken = null;
14
31
  disposeMolecule(currentState.instance);
15
32
  stateRef.current = void 0;
16
33
  }
17
- const moleculeArgs = props === void 0 ? [] : [props];
18
- stateRef.current = {
34
+ const snapshot = props === void 0 ? void 0 : { ...props };
35
+ const moleculeArgs = snapshot === void 0 ? [] : [snapshot];
36
+ const nextState = {
19
37
  instance: molecule(...moleculeArgs),
20
38
  molecule,
21
- props,
22
39
  subscribers: 0,
23
40
  disposed: false,
24
41
  pendingDisposeToken: null
25
42
  };
43
+ stateRef.current = nextState;
44
+ if (isServerEnvironment) {
45
+ const token = Symbol("pending-server-dispose");
46
+ nextState.pendingDisposeToken = token;
47
+ schedulePendingDispose(stateRef, nextState.instance, token);
48
+ }
26
49
  }
27
50
  const state = stateRef.current;
28
51
  if (state === void 0) {
@@ -31,7 +54,7 @@ function useMolecule(molecule, ...args) {
31
54
  );
32
55
  }
33
56
  const instance = state.instance;
34
- useEffect(() => {
57
+ useIsomorphicLayoutEffect(() => {
35
58
  const state2 = stateRef.current;
36
59
  if (state2 === void 0 || state2.instance !== instance) {
37
60
  return () => {
@@ -41,6 +64,9 @@ function useMolecule(molecule, ...args) {
41
64
  state2.pendingDisposeToken = null;
42
65
  }
43
66
  state2.subscribers += 1;
67
+ if (state2.subscribers === 1) {
68
+ mountMolecule(instance);
69
+ }
44
70
  return () => {
45
71
  const latest = stateRef.current;
46
72
  if (latest === void 0 || latest.instance !== instance) {
@@ -52,18 +78,10 @@ function useMolecule(molecule, ...args) {
52
78
  latest.subscribers = 0;
53
79
  }
54
80
  if (!latest.disposed && latest.subscribers === 0) {
81
+ unmountMolecule(instance);
55
82
  const token = Symbol("pending-dispose");
56
83
  latest.pendingDisposeToken = token;
57
- queueMicrotask(() => {
58
- const current = stateRef.current;
59
- if (current === void 0 || current.instance !== instance || current.subscribers > 0 || current.disposed || current.pendingDisposeToken !== token) {
60
- return;
61
- }
62
- current.disposed = true;
63
- current.pendingDisposeToken = null;
64
- stateRef.current = void 0;
65
- disposeMolecule(instance);
66
- });
84
+ schedulePendingDispose(stateRef, instance, token);
67
85
  }
68
86
  };
69
87
  }, [instance]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sigrea/react",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "React adapter bindings for Sigrea molecule modules.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -17,7 +17,7 @@
17
17
  "url": "https://github.com/sigrea/react/issues"
18
18
  },
19
19
  "engines": {
20
- "node": ">=20"
20
+ "node": ">=24"
21
21
  },
22
22
  "sideEffects": false,
23
23
  "exports": {
@@ -29,16 +29,8 @@
29
29
  },
30
30
  "main": "./dist/index.cjs",
31
31
  "types": "./dist/index.d.ts",
32
- "files": [
33
- "dist"
34
- ],
35
- "keywords": [
36
- "signals",
37
- "reactivity",
38
- "react",
39
- "molecule",
40
- "typescript"
41
- ],
32
+ "files": ["dist"],
33
+ "keywords": ["signals", "reactivity", "react", "molecule", "typescript"],
42
34
  "scripts": {
43
35
  "dev": "vite --config playground/vite.config.ts",
44
36
  "build": "unbuild",
@@ -53,17 +45,20 @@
53
45
  "cicheck": "pnpm test && pnpm typecheck && pnpm format:fix"
54
46
  },
55
47
  "peerDependencies": {
56
- "@sigrea/core": "^0.4.3",
48
+ "@sigrea/core": "^0.6.0",
57
49
  "react": "^18.0.0 || ^19.0.0",
58
50
  "react-dom": "^18.0.0 || ^19.0.0"
59
51
  },
60
52
  "devDependencies": {
61
53
  "@biomejs/biome": "1.9.4",
62
- "@vitejs/plugin-react": "^4.3.3",
54
+ "@sigrea/core": "^0.6.0",
63
55
  "@types/react": "^19.0.0",
64
56
  "@types/react-dom": "^19.0.0",
65
- "changelogen": "^0.6.2",
57
+ "@vitejs/plugin-react": "^4.3.3",
66
58
  "@vitest/coverage-v8": "^3.2.4",
59
+ "baseline-browser-mapping": "^2.9.13",
60
+ "changelogen": "^0.6.2",
61
+ "jsdom": "^24.1.3",
67
62
  "lefthook": "1.13.6",
68
63
  "react": "^19.0.0",
69
64
  "react-dom": "^19.0.0",
@@ -71,13 +66,9 @@
71
66
  "typescript": "5.9.3",
72
67
  "unbuild": "3.6.1",
73
68
  "vite": "^5.4.6",
74
- "vitest": "^3.2.4",
75
- "jsdom": "^24.1.3"
69
+ "vitest": "^3.2.4"
76
70
  },
77
71
  "pnpm": {
78
- "onlyBuiltDependencies": [
79
- "lefthook",
80
- "@biomejs/biome"
81
- ]
72
+ "onlyBuiltDependencies": ["lefthook", "@biomejs/biome"]
82
73
  }
83
74
  }