@zeruxjs/react 0.0.0-stage → 0.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/LICENSE-APACHE +201 -0
- package/LICENSE-MIT +9 -0
- package/README.md +36 -2
- package/dist/bridge.d.ts +11 -0
- package/dist/bridge.js +11 -0
- package/dist/bridge.js.map +1 -0
- package/dist/hooks.d.ts +22 -0
- package/dist/hooks.js +40 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/store.d.ts +19 -0
- package/dist/store.js +70 -0
- package/dist/store.js.map +1 -0
- package/package.json +36 -4
package/LICENSE-APACHE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/LICENSE-MIT
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ZeruxJS.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,37 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @zeruxjs/react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Framework-neutral external-store primitives and injected React Hooks adapters.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import {createReactStore,createReactBindings} from '@zeruxjs/react';
|
|
7
|
+
import {useMemo,useSyncExternalStore} from 'react';
|
|
8
|
+
const bindings = createReactBindings({useMemo,useSyncExternalStore});
|
|
9
|
+
const store = createReactStore({count:0});
|
|
10
|
+
// Inside a component:
|
|
11
|
+
const count = bindings.useSelector(store, state => state.count);
|
|
12
|
+
store.batch(() => {store.update(s=>({count:s.count+1}));});
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
For real components, stabilize selectors with `useCallback` or a module-level function when possible. `createReactBindings` memoizes selector stores using the supplied real `useMemo`; `useReactSelector` also caches a stable selector/store pair for direct calls. The store supports `getServerSnapshot`, transactions, cleanup, and selector equality. `observableToReactStore`/`reactStoreToObservable` bridge Zyro-style observables via structural typing with no bundled React or Zyro dependency.
|
|
16
|
+
|
|
17
|
+
## Section 78: hydration and ownership contract
|
|
18
|
+
|
|
19
|
+
`ExternalStore<T>` may expose `getServerSnapshot()` for React 18+ SSR. The snapshot returned on the server **must be serialized into the client hydration payload**, including its data shape. `createReactStore(initial, {serverSnapshot})` pins its server snapshot rather than returning a subsequently mutated client value. `selectReactStore` memoizes selected client **and server** values using the supplied comparator, preventing newly allocated selectors from changing snapshot identity on every read.
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
import {createReactBindings,createReactStore} from '@zeruxjs/react';
|
|
23
|
+
import {useMemo,useSyncExternalStore,useCallback} from 'react';
|
|
24
|
+
const bindings = createReactBindings({useMemo,useSyncExternalStore});
|
|
25
|
+
const store = createReactStore({count:0});
|
|
26
|
+
function Counter(){
|
|
27
|
+
const selector = useCallback((value:{count:number})=>value.count,[]);
|
|
28
|
+
const count = bindings.useSelector(store,selector);
|
|
29
|
+
return <button onClick={()=>store.update(s=>({count:s.count+1}))}>{count}</button>;
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
`createReactBindings` invokes the real React Hooks only inside components. The standalone `useReactSelector` convenience helper uses a weak cache and needs a stable selector. When passing an explicit `serverSnapshot` callback, return stable data; React requires consistent snapshots across SSR, hydration and the first client commit. Unsubscribe is idempotent; the external store does not own React's component lifecycle. React itself is **not installed, vendored or automatically registered** by this package.
|
|
34
|
+
|
|
35
|
+
## Release 0.0.2
|
|
36
|
+
|
|
37
|
+
Prepared to validate npm trusted publishing through GitHub Actions.
|
package/dist/bridge.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ExternalStore } from './store.js';
|
|
2
|
+
/** Adapt an Observable (including @zeruxjs/zyro) without a runtime dependency. */
|
|
3
|
+
export declare function observableToReactStore<T>(source: {
|
|
4
|
+
get(): T;
|
|
5
|
+
subscribe(listener: (value: T) => void): () => void;
|
|
6
|
+
}): ExternalStore<T>;
|
|
7
|
+
/** Observe a readable React store through the shared Observable contract. */
|
|
8
|
+
export declare function reactStoreToObservable<T>(store: ExternalStore<T>): {
|
|
9
|
+
get: () => T;
|
|
10
|
+
subscribe(listener: (value: T) => void): () => void;
|
|
11
|
+
};
|
package/dist/bridge.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.observableToReactStore = observableToReactStore;
|
|
4
|
+
exports.reactStoreToObservable = reactStoreToObservable;
|
|
5
|
+
/** Adapt an Observable (including @zeruxjs/zyro) without a runtime dependency. */
|
|
6
|
+
function observableToReactStore(source) {
|
|
7
|
+
return { getSnapshot: () => source.get(), subscribe(listener) { return source.subscribe(() => listener()); } };
|
|
8
|
+
}
|
|
9
|
+
/** Observe a readable React store through the shared Observable contract. */
|
|
10
|
+
function reactStoreToObservable(store) { return { get: store.getSnapshot, subscribe(listener) { return store.subscribe(() => listener(store.getSnapshot())); } }; }
|
|
11
|
+
//# sourceMappingURL=bridge.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bridge.js","sourceRoot":"","sources":["../src/bridge.ts"],"names":[],"mappings":";;AAEA,wDAEC;AAED,wDAA+L;AAL/L,kFAAkF;AAClF,SAAgB,sBAAsB,CAAI,MAA6D;IACtG,OAAM,EAAC,WAAW,EAAC,GAAE,EAAE,CAAA,MAAM,CAAC,GAAG,EAAE,EAAC,SAAS,CAAC,QAAQ,IAAE,OAAO,MAAM,CAAC,SAAS,CAAC,GAAE,EAAE,CAAA,QAAQ,EAAE,CAAC,CAAA,CAAA,CAAC,EAAC,CAAC;AACnG,CAAC;AACD,6EAA6E;AAC7E,SAAgB,sBAAsB,CAAI,KAAsB,IAAE,OAAM,EAAC,GAAG,EAAC,KAAK,CAAC,WAAW,EAAC,SAAS,CAAC,QAAwB,IAAE,OAAO,KAAK,CAAC,SAAS,CAAC,GAAE,EAAE,CAAA,QAAQ,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC,CAAC,CAAA,CAAA,CAAC,EAAC,CAAA,CAAA,CAAC"}
|
package/dist/hooks.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type ExternalStore } from './store.js';
|
|
2
|
+
/** A React.useSyncExternalStore-compatible injection (React remains a peer). */
|
|
3
|
+
export type UseSyncExternalStore = <T>(subscribe: (listener: () => void) => () => void, getSnapshot: () => T, getServerSnapshot?: () => T) => T;
|
|
4
|
+
export interface ReactHookRuntime {
|
|
5
|
+
useSyncExternalStore: UseSyncExternalStore;
|
|
6
|
+
useMemo: <T>(create: () => T, dependencies: readonly unknown[]) => T;
|
|
7
|
+
}
|
|
8
|
+
/** On SSR, React requires the server snapshot to match the hydration payload. */
|
|
9
|
+
export declare function useReactStore<T>(useSyncExternalStore: UseSyncExternalStore, store: ExternalStore<T>, serverSnapshot?: () => T): T;
|
|
10
|
+
/** Prefer createReactBindings inside actual components: it uses React's own hook memoization. */
|
|
11
|
+
export declare function useReactSelector<A, B>(useSyncExternalStore: UseSyncExternalStore, store: ExternalStore<A>, selector: (state: A) => B, options?: {
|
|
12
|
+
equal?: (a: B, b: B) => boolean;
|
|
13
|
+
serverSnapshot?: () => A;
|
|
14
|
+
}): B;
|
|
15
|
+
/** Correct React hook lifecycle: one selector store per stable dependency set. */
|
|
16
|
+
export declare function createReactBindings(runtime: ReactHookRuntime): {
|
|
17
|
+
useStore<T>(store: ExternalStore<T>, serverSnapshot?: () => T): T;
|
|
18
|
+
useSelector<A, B>(store: ExternalStore<A>, selector: (state: A) => B, options?: {
|
|
19
|
+
equal?: (a: B, b: B) => boolean;
|
|
20
|
+
serverSnapshot?: () => A;
|
|
21
|
+
}): B;
|
|
22
|
+
};
|
package/dist/hooks.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useReactStore = useReactStore;
|
|
4
|
+
exports.useReactSelector = useReactSelector;
|
|
5
|
+
exports.createReactBindings = createReactBindings;
|
|
6
|
+
const store_js_1 = require("./store.js");
|
|
7
|
+
/** On SSR, React requires the server snapshot to match the hydration payload. */
|
|
8
|
+
function useReactStore(useSyncExternalStore, store, serverSnapshot) {
|
|
9
|
+
return useSyncExternalStore(store.subscribe, store.getSnapshot, serverSnapshot ?? store.getServerSnapshot);
|
|
10
|
+
}
|
|
11
|
+
const selectors = new WeakMap();
|
|
12
|
+
/** Prefer createReactBindings inside actual components: it uses React's own hook memoization. */
|
|
13
|
+
function useReactSelector(useSyncExternalStore, store, selector, options = {}) {
|
|
14
|
+
let cached = selectors.get(store);
|
|
15
|
+
if (!cached) {
|
|
16
|
+
cached = new WeakMap();
|
|
17
|
+
selectors.set(store, cached);
|
|
18
|
+
}
|
|
19
|
+
let entry = cached.get(selector);
|
|
20
|
+
const equal = options.equal ?? Object.is;
|
|
21
|
+
if (!entry || entry.comparator !== equal) {
|
|
22
|
+
entry = { comparator: equal, store: (0, store_js_1.selectReactStore)(store, selector, equal) };
|
|
23
|
+
cached.set(selector, entry);
|
|
24
|
+
}
|
|
25
|
+
const derived = entry.store;
|
|
26
|
+
const serverRead = options.serverSnapshot;
|
|
27
|
+
return useSyncExternalStore(derived.subscribe, derived.getSnapshot, serverRead ? () => selector(serverRead()) : derived.getServerSnapshot);
|
|
28
|
+
}
|
|
29
|
+
/** Correct React hook lifecycle: one selector store per stable dependency set. */
|
|
30
|
+
function createReactBindings(runtime) {
|
|
31
|
+
return {
|
|
32
|
+
useStore(store, serverSnapshot) { return useReactStore(runtime.useSyncExternalStore, store, serverSnapshot); },
|
|
33
|
+
useSelector(store, selector, options = {}) {
|
|
34
|
+
const derived = runtime.useMemo(() => (0, store_js_1.selectReactStore)(store, selector, options.equal), [store, selector, options.equal]);
|
|
35
|
+
const serverRead = options.serverSnapshot;
|
|
36
|
+
return runtime.useSyncExternalStore(derived.subscribe, derived.getSnapshot, serverRead ? () => selector(serverRead()) : derived.getServerSnapshot);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=hooks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.js","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":";;AAQA,sCAEC;AAGD,4CAOC;AAED,kDASC;AA/BD,yCAA+D;AAO/D,iFAAiF;AACjF,SAAgB,aAAa,C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iB,CAAC,CAAC;AACpI,CAAC;AACD,kFAAkF;AAClF,SAAgB,mBAAmB,CAAC,OAAwB;IAC3D,OAAM;QACL,QAAQ,CAAI,KAAsB,EAAC,cAAqB,IAAE,OAAO,aAAa,CAAC,OAAO,CAAC,oBAAoB,EAAC,KAAK,EAAC,cAAc,CAAC,CAAA,CAAA,CAAC;QAClI,WAAW,CAAM,KAAsB,EAAC,QAAqB,EAAC,UAA0D,EAAE;YACzH,MAAM,OAAO,GAAC,OAAO,CAAC,OAAO,CAAC,GAAE,EAAE,CAAA,IAAA,2BAAgB,EAAC,KAAK,EAAC,QAAQ,EAAC,OAAO,CAAC,KAAK,CAAC,EAAC,CAAC,KAAK,EAAC,QAAQ,EAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;YACjH,MAAM,UAAU,GAAC,OAAO,CAAC,cAAc,CAAC;YACxC,OAAO,OAAO,CAAC,oBAAoB,CAAC,OAAO,CAAC,SAAS,EAAC,OAAO,CAAC,WAAW,EAAC,UAAU,CAAA,CAAC,CAAA,GAAE,EAAE,CAAA,QAAQ,CAAC,UAAU,EAAE,CAAC,CAAA,CAAC,CAAA,OAAO,CAAC,iBAAiB,CAAC,CAAC;QAC5I,CAAC;KACD,CAAC;AACH,CAAC"}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./store.js"), exports);
|
|
18
|
+
__exportStar(require("./hooks.js"), exports);
|
|
19
|
+
__exportStar(require("./bridge.js"), exports);
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAA2B;AAC3B,6CAA2B;AAC3B,8CAA4B"}
|
package/dist/store.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface ExternalStore<T> {
|
|
2
|
+
getSnapshot(): T;
|
|
3
|
+
subscribe(listener: () => void): () => void;
|
|
4
|
+
getServerSnapshot?(): T;
|
|
5
|
+
}
|
|
6
|
+
export type Updater<T> = T | ((previous: T) => T);
|
|
7
|
+
export interface WritableExternalStore<T> extends ExternalStore<T> {
|
|
8
|
+
set(value: Updater<T>): void;
|
|
9
|
+
update(fn: (state: T) => T): void;
|
|
10
|
+
batch<R>(fn: () => R): R;
|
|
11
|
+
getServerSnapshot(): T;
|
|
12
|
+
destroy(): void;
|
|
13
|
+
}
|
|
14
|
+
/** Snapshot updates are synchronous; a batch notifies subscribers once. */
|
|
15
|
+
export declare function createReactStore<T>(initial: T, options?: {
|
|
16
|
+
serverSnapshot?: T;
|
|
17
|
+
}): WritableExternalStore<T>;
|
|
18
|
+
/** Memoizes selected snapshots instead of producing fresh references on every read. */
|
|
19
|
+
export declare function selectReactStore<A, B>(source: ExternalStore<A>, selector: (value: A) => B, equal?: (a: B, b: B) => boolean): ExternalStore<B>;
|
package/dist/store.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createReactStore = createReactStore;
|
|
4
|
+
exports.selectReactStore = selectReactStore;
|
|
5
|
+
/** Snapshot updates are synchronous; a batch notifies subscribers once. */
|
|
6
|
+
function createReactStore(initial, options = {}) {
|
|
7
|
+
let current = initial, active = true, depth = 0, pending = false, emitting = false;
|
|
8
|
+
const listeners = new Set();
|
|
9
|
+
const getSnapshot = () => current;
|
|
10
|
+
const flush = () => { if (depth || emitting || !pending || !active)
|
|
11
|
+
return; emitting = true; try {
|
|
12
|
+
while (pending && !depth && active) {
|
|
13
|
+
pending = false;
|
|
14
|
+
for (const listener of [...listeners])
|
|
15
|
+
if (listeners.has(listener))
|
|
16
|
+
listener();
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
finally {
|
|
20
|
+
emitting = false;
|
|
21
|
+
} };
|
|
22
|
+
const set = (value) => {
|
|
23
|
+
if (!active)
|
|
24
|
+
return;
|
|
25
|
+
const next = typeof value === 'function' ? value(current) : value;
|
|
26
|
+
if (Object.is(next, current))
|
|
27
|
+
return;
|
|
28
|
+
current = next;
|
|
29
|
+
pending = true;
|
|
30
|
+
flush();
|
|
31
|
+
};
|
|
32
|
+
const serverValue = options.serverSnapshot === undefined ? initial : options.serverSnapshot;
|
|
33
|
+
return { getSnapshot, getServerSnapshot: () => serverValue, subscribe(listener) { if (!active)
|
|
34
|
+
return () => { }; listeners.add(listener); return () => listeners.delete(listener); },
|
|
35
|
+
set, update: set, batch(fn) { depth++; try {
|
|
36
|
+
return fn();
|
|
37
|
+
}
|
|
38
|
+
finally {
|
|
39
|
+
depth--;
|
|
40
|
+
flush();
|
|
41
|
+
} }, destroy() { active = false; pending = false; listeners.clear(); } };
|
|
42
|
+
}
|
|
43
|
+
/** Memoizes selected snapshots instead of producing fresh references on every read. */
|
|
44
|
+
function selectReactStore(source, selector, equal = Object.is) {
|
|
45
|
+
let lastSource = source.getSnapshot(), lastSelected = selector(lastSource);
|
|
46
|
+
const getSnapshot = () => { const current = source.getSnapshot(); if (!Object.is(lastSource, current)) {
|
|
47
|
+
lastSource = current;
|
|
48
|
+
const selected = selector(current);
|
|
49
|
+
if (!equal(lastSelected, selected))
|
|
50
|
+
lastSelected = selected;
|
|
51
|
+
} return lastSelected; };
|
|
52
|
+
let lastServerSource = source.getServerSnapshot?.();
|
|
53
|
+
let lastServerSelected = source.getServerSnapshot ? selector(lastServerSource) : undefined;
|
|
54
|
+
const getServerSnapshot = source.getServerSnapshot ? () => {
|
|
55
|
+
const current = source.getServerSnapshot();
|
|
56
|
+
if (!Object.is(lastServerSource, current)) {
|
|
57
|
+
lastServerSource = current;
|
|
58
|
+
const selected = selector(current);
|
|
59
|
+
if (lastServerSelected === undefined || !equal(lastServerSelected, selected))
|
|
60
|
+
lastServerSelected = selected;
|
|
61
|
+
}
|
|
62
|
+
return lastServerSelected;
|
|
63
|
+
} : undefined;
|
|
64
|
+
return { getSnapshot, getServerSnapshot,
|
|
65
|
+
subscribe(listener) { let previous = getSnapshot(); return source.subscribe(() => { const next = getSnapshot(); if (!equal(previous, next)) {
|
|
66
|
+
previous = next;
|
|
67
|
+
listener();
|
|
68
|
+
} }); } };
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store.js","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":";;AAIA,4CASC;AAED,4CAeC;AA3BD,2EAA2E;AAC3E,SAAgB,gBAAgB,CAAI,OAAS,EAAC,UAA4B,EAAE;IAC3E,IAAI,OAAO,GAAC,OAAO,EAAC,MAAM,GAAC,IAAI,EAAC,KAAK,GAAC,CAAC,EAAC,OAAO,GAAC,KAAK,EAAC,QAAQ,GAAC,KAAK,CAAC;IACrE,MAAM,SAAS,GAAC,IAAI,GAAG,EAAY,CAAC;IAAA,MAAM,WAAW,GAAC,GAAE,EAAE,CA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AiB,GAAC,MAAM,CAAC,iBAAiB,CAAA,CAAC,CAAA,GAAE,EAAE;QACpD,MAAM,OAAO,GAAC,MAAM,CAAC,iBAAkB,EAAE,CAAC;QAC1C,IAAG,CAAC,MAAM,CAAC,EAAE,CAAC,gBAAgB,EAAC,OAAO,CAAC,EAAC,CAAC;YACxC,gBAAgB,GAAC,OAAO,CAAC;YAAA,MAAM,QAAQ,GAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;YAC1D,IAAG,kBAAkB,KAAG,SAAS,IAAE,CAAC,KAAK,CAAC,kBAAkB,EAAC,QAAQ,CAAC;gBAAC,kBAAkB,GAAC,QAAQ,CAAC;QACpG,CAAC;QACD,OAAO,kBAAuB,CAAC;IAChC,CAAC,CAAA,CAAC,CAAA,SAAS,CAAC;IACZ,OAAM,EAAC,WAAW,EAAC,iBAAiB;QACnC,SAAS,CAAC,QAAQ,IAAE,IAAI,QAAQ,GAAC,WAAW,EAAE,CAAC,CAAA,OAAO,MAAM,CAAC,SAAS,CAAC,GAAE,EAAE,GAAC,MAAM,IAAI,GAAC,WAAW,EAAE,CAAC,CAAA,IAAG,CAAC,KAAK,CAAC,QAAQ,EAAC,IAAI,CAAC,EAAC,CAAC;YAAA,QAAQ,GAAC,IAAI,CAAC;YAAA,QAAQ,EAAE,CAAA;QAAA,CAAC,CAAA,CAAC,CAAC,CAAA,CAAA,CAAC,EAAC,CAAC;AAC/J,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zeruxjs/react",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "External stores, stable selectors and React useSyncExternalStore bridge without bundled React.",
|
|
5
|
+
"homepage": "https://github.com/zeruxjs/zeruxjs#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/zeruxjs/zeruxjs/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/zeruxjs/zeruxjs.git"
|
|
12
|
+
},
|
|
13
|
+
"license": "(MIT OR Apache-2.0)",
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "tsc",
|
|
18
|
+
"dev": "tsc --watch",
|
|
19
|
+
"test": "node --test test/*.test.mjs",
|
|
20
|
+
"prepack": "npm run build"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"Library - ReactJS support"
|
|
24
|
+
],
|
|
25
|
+
"files": [
|
|
26
|
+
"dist/",
|
|
27
|
+
"README.md",
|
|
28
|
+
"LICENSE-MIT",
|
|
29
|
+
"LICENSE-APACHE"
|
|
30
|
+
],
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"typescript": "^5.9.3",
|
|
36
|
+
"@types/node": "^25.4.0"
|
|
37
|
+
}
|
|
38
|
+
}
|