@rindle/react 0.1.0-rc.5

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 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/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # @rindle/react
2
+
3
+ React bindings for Rindle stores.
4
+
5
+ ```tsx
6
+ import { Rindle, useQuery } from "@rindle/react";
7
+
8
+ createRoot(root).render(
9
+ <Rindle store={app.store}>
10
+ <Issues />
11
+ </Rindle>,
12
+ );
13
+
14
+ function Issues() {
15
+ const issues = useQuery(queries.issuesPage(null));
16
+ return issues.map((issue) => <div key={issue.id}>{issue.title}</div>);
17
+ }
18
+ ```
19
+
20
+ `useQuery(query)` returns live `data`, subscribes on mount, and releases on unmount.
21
+
22
+ For local-first backends, the React-facing data view is shared by compiled local AST. Each
23
+ mounted hook still retains its own `name`/`args` lease through the Store, so backend-level
24
+ subscription refcounting remains responsible for server dedupe and teardown. Backends that
25
+ cannot split local views from remote subscriptions keep the older materialized-view fallback.
26
+
27
+ ## Other exports
28
+
29
+ - `Rindle` / `RindleProvider` — the context provider (`RindleProvider` is an alias of `Rindle`).
30
+ - `useQuery(query)` — live query data; subscribes on mount, releases on unmount.
31
+ - `useQueryStatus(query)` — the query's `ResultType`, which tracks the **server channel only**:
32
+ `unknown` while it loads (not yet server-authoritative), `complete` once the server has
33
+ answered. A pending optimistic mutation no longer moves it — "is a write pending here?" is a
34
+ separate axis on the backend (`backend.pending(qid)` / `onPending`), and `error` is reserved
35
+ and currently unproduced. Shares `useQuery`'s leased view, so reading both is one subscription.
36
+ - `useFragment(fragment, ref)` — a pure read of an already-projected fragment slice off the root
37
+ query's view (FRAGMENT-COMPOSITION-DESIGN.md §7). Adds no lease and never re-renders on its own;
38
+ it keeps a component tree waterfall-free by letting descendants read the one query the root opened.
39
+ - `useRindleStore()` — the `Store` from context, for imperative writes/reads inside components.
40
+ - `QueryCache` — the per-provider cache that dedupes a query's materialized view across hooks.
41
+ - `queryCacheKey(query)` — the stable string key (`@rindle/client`'s `stableKey(ast)`) under which
42
+ the cache stores that view. Same AST → same key → one shared entry, and it matches the SSR seed
43
+ key so hydration finds the dehydrated view. Keys the view (bare AST), not the lease.
@@ -0,0 +1,74 @@
1
+ import type { ReactNode } from "react";
2
+ import type { ColsMap, Fragment, FragmentRef, Query, ResultType, Store } from "@rindle/client";
3
+ type AnyQuery = Query<any, any, any>;
4
+ export type QueryData<Q extends AnyQuery> = ReturnType<Q["materialize"]>["data"];
5
+ export type { ResultType } from "@rindle/client";
6
+ export type { Fragment, FragmentRef } from "@rindle/client";
7
+ export interface RindleProps<S extends ColsMap = ColsMap> {
8
+ store: Store<S>;
9
+ children?: ReactNode;
10
+ }
11
+ interface QueryLease {
12
+ id: number;
13
+ viewKey: string;
14
+ }
15
+ declare class RindleContextValue {
16
+ readonly store: Store<ColsMap>;
17
+ readonly cache: QueryCache;
18
+ constructor(store: Store<ColsMap>);
19
+ }
20
+ export declare function Rindle<S extends ColsMap>({ store, children }: RindleProps<S>): import("react").FunctionComponentElement<import("react").ProviderProps<RindleContextValue | null>>;
21
+ export declare const RindleProvider: typeof Rindle;
22
+ export declare function useRindleStore<S extends ColsMap = ColsMap>(): Store<S>;
23
+ export declare function useQuery<Q extends AnyQuery>(query: Q): QueryData<Q>;
24
+ /** The SERVER-CHANNEL state of a query's view (`@rindle/client` {@link ResultType}): `unknown` while
25
+ * it loads (not yet server-authoritative), `complete` once the server has answered. A pending
26
+ * optimistic mutation no longer moves this — that is a separate axis now (FOLDED-MUTATIONS-DESIGN
27
+ * §7); the `error` variant is reserved and currently unproduced. Shares the same cached/leased view
28
+ * as {@link useQuery} (so reading both for one query is one subscription), and re-renders only when
29
+ * the status changes. */
30
+ export declare function useQueryStatus(query: AnyQuery): ResultType;
31
+ /**
32
+ * Read a {@link Fragment}'s slice off the shared root view (FRAGMENT-COMPOSITION-DESIGN.md §7).
33
+ *
34
+ * The waterfall-free contract: the ROOT component `useQuery`s the composed query (every child
35
+ * fragment spread into it), and every descendant calls `useFragment` — a PURE READ of the
36
+ * already-projected node at this fragment's alias — instead of opening its own `useQuery`. So a
37
+ * whole component tree resolves through the ONE query the root opened, with no request waterfall;
38
+ * a descendant that opens its own `useQuery` reintroduces the waterfall at that boundary.
39
+ *
40
+ * Phase 0 is a typed pass-through: `ref` already IS the projected node (the parent got it from
41
+ * `useQuery` and handed the slice down), so this returns it typed as the fragment's
42
+ * {@link FragmentRef}. `fragment` declares *which* selection this component reads (co-location +
43
+ * the seam masking plugs into); masking (Phase 2) narrows the returned shape with no call-site
44
+ * change. Not a subscription — it adds no lease and never re-renders on its own.
45
+ */
46
+ export declare function useFragment<F extends Fragment<any, any, any>>(fragment: F, ref: FragmentRef<F>): FragmentRef<F>;
47
+ export declare class QueryCache {
48
+ private readonly entries;
49
+ private nextLeaseId;
50
+ private readonly store;
51
+ constructor(store: Store<ColsMap>);
52
+ retain<Q extends AnyQuery>(viewKey: string, query: Q): QueryLease;
53
+ release(lease: QueryLease): void;
54
+ subscribe(viewKey: string, listener: () => void): () => void;
55
+ snapshot(viewKey: string, one: boolean): unknown;
56
+ /** A query's current {@link ResultType} (from its view), or `unknown` before it is retained. */
57
+ resultType(viewKey: string): ResultType;
58
+ /** The SSR/hydration snapshot for `viewKey` — the store's preloaded/dehydrated seed, read
59
+ * WITHOUT retaining (the server never opens a subscription). Falls back to empty so a
60
+ * non-preloaded query renders like an unhydrated one. */
61
+ serverSnapshot(viewKey: string, one: boolean): unknown;
62
+ /** The SSR {@link ResultType}: `complete` when a seed exists (server-authoritative first paint),
63
+ * else `unknown`. */
64
+ serverResultType(viewKey: string): ResultType;
65
+ size(): number;
66
+ private createSplitEntry;
67
+ private createSplitLease;
68
+ private createMaterializedEntry;
69
+ private createMaterializedLease;
70
+ private chooseCanonical;
71
+ private setCanonical;
72
+ }
73
+ export declare function queryCacheKey(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AO,CAAC,gBAAgB;IAQxB,OAAO,CAAC,uBAAuB;IAa/B,OAAO,CAAC,uBAAuB;IAS/B,OAAO,CAAC,eAAe;IAIvB,OAAO,CAAC,YAAY;CAOrB;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,QAAQ,GAAG,MAAM,CAErD"}
package/dist/index.js ADDED
@@ -0,0 +1,252 @@
1
+ import { createContext, createElement, useCallback, useContext, useMemo, useRef, useSyncExternalStore, } from "react";
2
+ import { stableKey } from "@rindle/client";
3
+ const EMPTY_ARRAY = Object.freeze([]);
4
+ class RindleContextValue {
5
+ store;
6
+ cache;
7
+ constructor(store) {
8
+ this.store = store;
9
+ this.cache = new QueryCache(store);
10
+ }
11
+ }
12
+ const RindleContext = createContext(null);
13
+ export function Rindle({ store, children }) {
14
+ const value = useMemo(() => new RindleContextValue(store), [store]);
15
+ return createElement(RindleContext.Provider, { value }, children);
16
+ }
17
+ export const RindleProvider = Rindle;
18
+ export function useRindleStore() {
19
+ return useRindleContext().store;
20
+ }
21
+ export function useQuery(query) {
22
+ const ctx = useRindleContext();
23
+ const descriptor = useMemo(() => describeQuery(query), [query]);
24
+ const queryRef = useRef(query);
25
+ queryRef.current = query;
26
+ const subscribe = useCallback((onStoreChange) => {
27
+ const lease = ctx.cache.retain(descriptor.viewKey, queryRef.current);
28
+ const unsubscribe = ctx.cache.subscribe(descriptor.viewKey, onStoreChange);
29
+ return () => {
30
+ unsubscribe();
31
+ ctx.cache.release(lease);
32
+ };
33
+ }, [ctx.cache, descriptor.leaseKey, descriptor.viewKey]);
34
+ const getSnapshot = useCallback(() => ctx.cache.snapshot(descriptor.viewKey, descriptor.one), [ctx.cache, descriptor.viewKey, descriptor.one]);
35
+ // SSR (SSR-DESIGN.md §6): the server calls `getServerSnapshot` (never `subscribe`), so it must
36
+ // surface the dehydrated/preloaded seed directly — not the live cache, which is never retained
37
+ // on the server. The client's hydration pass reads the same seed, matching the SSR markup.
38
+ const getServerSnapshot = useCallback(() => ctx.cache.serverSnapshot(descriptor.viewKey, descriptor.one), [ctx.cache, descriptor.viewKey, descriptor.one]);
39
+ return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
40
+ }
41
+ /** The SERVER-CHANNEL state of a query's view (`@rindle/client` {@link ResultType}): `unknown` while
42
+ * it loads (not yet server-authoritative), `complete` once the server has answered. A pending
43
+ * optimistic mutation no longer moves this — that is a separate axis now (FOLDED-MUTATIONS-DESIGN
44
+ * §7); the `error` variant is reserved and currently unproduced. Shares the same cached/leased view
45
+ * as {@link useQuery} (so reading both for one query is one subscription), and re-renders only when
46
+ * the status changes. */
47
+ export function useQueryStatus(query) {
48
+ const ctx = useRindleContext();
49
+ const descriptor = useMemo(() => describeQuery(query), [query]);
50
+ const queryRef = useRef(query);
51
+ queryRef.current = query;
52
+ const subscribe = useCallback((onStoreChange) => {
53
+ const lease = ctx.cache.retain(descriptor.viewKey, queryRef.current);
54
+ const unsubscribe = ctx.cache.subscribe(descriptor.viewKey, onStoreChange);
55
+ return () => {
56
+ unsubscribe();
57
+ ctx.cache.release(lease);
58
+ };
59
+ }, [ctx.cache, descriptor.leaseKey, descriptor.viewKey]);
60
+ const getSnapshot = useCallback(() => ctx.cache.resultType(descriptor.viewKey), [ctx.cache, descriptor.viewKey]);
61
+ // SSR: a seeded query is server-authoritative for first paint (`complete`); otherwise `unknown`.
62
+ const getServerSnapshot = useCallback(() => ctx.cache.serverResultType(descriptor.viewKey), [ctx.cache, descriptor.viewKey]);
63
+ return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
64
+ }
65
+ /**
66
+ * Read a {@link Fragment}'s slice off the shared root view (FRAGMENT-COMPOSITION-DESIGN.md §7).
67
+ *
68
+ * The waterfall-free contract: the ROOT component `useQuery`s the composed query (every child
69
+ * fragment spread into it), and every descendant calls `useFragment` — a PURE READ of the
70
+ * already-projected node at this fragment's alias — instead of opening its own `useQuery`. So a
71
+ * whole component tree resolves through the ONE query the root opened, with no request waterfall;
72
+ * a descendant that opens its own `useQuery` reintroduces the waterfall at that boundary.
73
+ *
74
+ * Phase 0 is a typed pass-through: `ref` already IS the projected node (the parent got it from
75
+ * `useQuery` and handed the slice down), so this returns it typed as the fragment's
76
+ * {@link FragmentRef}. `fragment` declares *which* selection this component reads (co-location +
77
+ * the seam masking plugs into); masking (Phase 2) narrows the returned shape with no call-site
78
+ * change. Not a subscription — it adds no lease and never re-renders on its own.
79
+ */
80
+ export function useFragment(fragment, ref) {
81
+ void fragment;
82
+ return ref;
83
+ }
84
+ export class QueryCache {
85
+ entries = new Map();
86
+ nextLeaseId = 1;
87
+ store;
88
+ constructor(store) {
89
+ this.store = store;
90
+ }
91
+ retain(viewKey, query) {
92
+ let entry = this.entries.get(viewKey);
93
+ if (!entry) {
94
+ entry = this.store.canRetainRemoteQueries()
95
+ ? this.createSplitEntry(viewKey, query)
96
+ : this.createMaterializedEntry(viewKey, query);
97
+ this.entries.set(viewKey, entry);
98
+ const lease = entry.leases[0];
99
+ return { id: lease.id, viewKey };
100
+ }
101
+ if (entry.mode === "split") {
102
+ const lease = this.createSplitLease(viewKey, entry.handle, query);
103
+ entry.leases.push(lease);
104
+ return { id: lease.id, viewKey };
105
+ }
106
+ const lease = this.createMaterializedLease(viewKey, query);
107
+ entry.leases.push(lease);
108
+ if (!entry.canonical.remote && lease.remote)
109
+ this.setCanonical(entry, lease);
110
+ return { id: lease.id, viewKey };
111
+ }
112
+ release(lease) {
113
+ const entry = this.entries.get(lease.viewKey);
114
+ if (!entry)
115
+ return;
116
+ const index = entry.leases.findIndex((l) => l.id === lease.id);
117
+ if (index < 0)
118
+ return;
119
+ if (entry.mode === "split") {
120
+ const [released] = entry.leases.splice(index, 1);
121
+ released.releaseRemote();
122
+ if (entry.leases.length > 0)
123
+ return;
124
+ entry.canonicalUnsubscribe();
125
+ entry.handle.destroy();
126
+ this.entries.delete(lease.viewKey);
127
+ return;
128
+ }
129
+ const [released] = entry.leases.splice(index, 1);
130
+ if (entry.canonical.id === released.id) {
131
+ entry.canonicalUnsubscribe();
132
+ entry.canonicalUnsubscribe = () => { };
133
+ }
134
+ released.view.destroy();
135
+ if (entry.leases.length === 0) {
136
+ this.entries.delete(lease.viewKey);
137
+ return;
138
+ }
139
+ if (entry.canonical.id === released.id)
140
+ this.setCanonical(entry, this.chooseCanonical(entry.leases));
141
+ }
142
+ subscribe(viewKey, listener) {
143
+ const entry = this.entries.get(viewKey);
144
+ if (!entry)
145
+ return () => { };
146
+ entry.listeners.add(listener);
147
+ return () => {
148
+ entry.listeners.delete(listener);
149
+ };
150
+ }
151
+ snapshot(viewKey, one) {
152
+ const entry = this.entries.get(viewKey);
153
+ if (entry)
154
+ return entry.mode === "split" ? entry.handle.view.data : entry.canonical.view.data;
155
+ return one ? null : EMPTY_ARRAY;
156
+ }
157
+ /** A query's current {@link ResultType} (from its view), or `unknown` before it is retained. */
158
+ resultType(viewKey) {
159
+ const entry = this.entries.get(viewKey);
160
+ if (!entry)
161
+ return "unknown";
162
+ return entry.mode === "split" ? entry.handle.view.resultType : entry.canonical.view.resultType;
163
+ }
164
+ /** The SSR/hydration snapshot for `viewKey` — the store's preloaded/dehydrated seed, read
165
+ * WITHOUT retaining (the server never opens a subscription). Falls back to empty so a
166
+ * non-preloaded query renders like an unhydrated one. */
167
+ serverSnapshot(viewKey, one) {
168
+ const seed = this.store.seedSnapshot(viewKey);
169
+ if (!seed)
170
+ return one ? null : EMPTY_ARRAY;
171
+ return one ? (seed.rows[0] ?? null) : seed.rows;
172
+ }
173
+ /** The SSR {@link ResultType}: `complete` when a seed exists (server-authoritative first paint),
174
+ * else `unknown`. */
175
+ serverResultType(viewKey) {
176
+ return this.store.seedSnapshot(viewKey) ? "complete" : "unknown";
177
+ }
178
+ size() {
179
+ return this.entries.size;
180
+ }
181
+ createSplitEntry(viewKey, query) {
182
+ const handle = this.store.createCachedQueryView(query);
183
+ const entry = {
184
+ mode: "split",
185
+ handle,
186
+ leases: [],
187
+ canonicalUnsubscribe: () => { },
188
+ listeners: new Set(),
189
+ };
190
+ entry.leases.push(this.createSplitLease(viewKey, handle, query));
191
+ entry.canonicalUnsubscribe = handle.view.subscribe(() => {
192
+ for (const listener of entry.listeners)
193
+ listener();
194
+ });
195
+ return entry;
196
+ }
197
+ createSplitLease(viewKey, handle, query) {
198
+ return { id: this.nextLeaseId++, viewKey, releaseRemote: handle.retain(query) };
199
+ }
200
+ createMaterializedEntry(viewKey, query) {
201
+ const lease = this.createMaterializedLease(viewKey, query);
202
+ const entry = {
203
+ mode: "materialized",
204
+ leases: [lease],
205
+ canonical: lease,
206
+ canonicalUnsubscribe: () => { },
207
+ listeners: new Set(),
208
+ };
209
+ this.setCanonical(entry, lease);
210
+ return entry;
211
+ }
212
+ createMaterializedLease(viewKey, query) {
213
+ return {
214
+ id: this.nextLeaseId++,
215
+ viewKey,
216
+ view: this.store.materialize(query),
217
+ remote: typeof query.name === "string",
218
+ };
219
+ }
220
+ chooseCanonical(leases) {
221
+ return leases.find((lease) => lease.remote) ?? leases[0];
222
+ }
223
+ setCanonical(entry, lease) {
224
+ entry.canonicalUnsubscribe();
225
+ entry.canonical = lease;
226
+ entry.canonicalUnsubscribe = lease.view.subscribe(() => {
227
+ for (const listener of entry.listeners)
228
+ listener();
229
+ });
230
+ }
231
+ }
232
+ export function queryCacheKey(query) {
233
+ return describeQuery(query).viewKey;
234
+ }
235
+ function useRindleContext() {
236
+ const ctx = useContext(RindleContext);
237
+ if (!ctx)
238
+ throw new Error("Rindle context is missing. Wrap this tree in <Rindle store={store}>.");
239
+ return ctx;
240
+ }
241
+ function describeQuery(query) {
242
+ const ast = query.ast();
243
+ const remote = typeof query.name === "string" ? { name: query.name, args: query.args } : null;
244
+ return {
245
+ // `viewKey` MUST match the SSR seed key (`@rindle/client`'s `stableKey(ast)`) so
246
+ // `getServerSnapshot` finds the dehydrated entry — one canonical serializer, shared.
247
+ viewKey: stableKey(ast),
248
+ leaseKey: stableKey({ ast, remote }),
249
+ one: ast.one === true,
250
+ };
251
+ }
252
+ //# sourceM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AAC,GAAG,CAAC;QACvB,QAAQ,EAAE,SAAS,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC;QACpC,GAAG,EAAE,GAAG,CAAC,GAAG,KAAK,IAAI;KACtB,CAAC;AACJ,CAAC"}
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@rindle/react",
3
+ "version": "0.1.0-rc.5",
4
+ "license": "Apache-2.0",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/rindle-sh/rindle.git",
8
+ "directory": "packages/react"
9
+ },
10
+ "type": "module",
11
+ "description": "React bindings for Rindle: context provider plus useQuery with shared AST-keyed view retain/release.",
12
+ "main": "./dist/index.js",
13
+ "types": "./dist/index.d.ts",
14
+ "sideEffects": false,
15
+ "exports": {
16
+ ".": {
17
+ "@rindle/source": "./src/index.ts",
18
+ "types": "./dist/index.d.ts",
19
+ "default": "./dist/index.js"
20
+ }
21
+ },
22
+ "files": [
23
+ "dist",
24
+ "src",
25
+ "README.md"
26
+ ],
27
+ "dependencies": {
28
+ "@rindle/client": "0.1.0-rc.5"
29
+ },
30
+ "peerDependencies": {
31
+ "react": ">=18.0.0"
32
+ },
33
+ "devDependencies": {
34
+ "@types/node": "^22.10.0",
35
+ "@types/react": "^19.0.0",
36
+ "@types/jsdom": "^21.1.0",
37
+ "@types/react-dom": "^19.0.0",
38
+ "jsdom": "^25.0.0",
39
+ "react": "^19.1.0",
40
+ "react-dom": "^19.1.0",
41
+ "typescript": "^5.7.0"
42
+ },
43
+ "scripts": {
44
+ "build": "tsc -p tsconfig.build.json",
45
+ "typecheck": "tsc --noEmit",
46
+ "test": "tsc --noEmit && node --conditions=@rindle/source --test test/*.test.ts"
47
+ }
48
+ }
package/src/index.ts ADDED
@@ -0,0 +1,360 @@
1
+ import {
2
+ createContext,
3
+ createElement,
4
+ useCallback,
5
+ useContext,
6
+ useMemo,
7
+ useRef,
8
+ useSyncExternalStore,
9
+ } from "react";
10
+ import type { ReactNode } from "react";
11
+ import { stableKey } from "@rindle/client";
12
+ import type { CachedQueryView, ColsMap, Fragment, FragmentRef, Query, ResultType, Store } from "@rindle/client";
13
+
14
+ type AnyQuery = Query<any, any, any>;
15
+ type AnyView = ReturnType<AnyQuery["materialize"]>;
16
+
17
+ export type QueryData<Q extends AnyQuery> = ReturnType<Q["materialize"]>["data"];
18
+
19
+ export type { ResultType } from "@rindle/client";
20
+ export type { Fragment, FragmentRef } from "@rindle/client";
21
+
22
+ export interface RindleProps<S extends ColsMap = ColsMap> {
23
+ store: Store<S>;
24
+ children?: ReactNode;
25
+ }
26
+
27
+ interface QueryDescriptor {
28
+ viewKey: string;
29
+ leaseKey: string;
30
+ one: boolean;
31
+ }
32
+
33
+ interface QueryLease {
34
+ id: number;
35
+ viewKey: string;
36
+ }
37
+
38
+ interface MaterializedLease extends QueryLease {
39
+ view: AnyView;
40
+ remote: boolean;
41
+ }
42
+
43
+ interface SplitLease extends QueryLease {
44
+ releaseRemote: () => void;
45
+ }
46
+
47
+ type CacheLease = MaterializedLease | SplitLease;
48
+
49
+ interface BaseCacheEntry {
50
+ leases: CacheLease[];
51
+ canonicalUnsubscribe: () => void;
52
+ listeners: Set<() => void>;
53
+ }
54
+
55
+ interface SplitCacheEntry extends BaseCacheEntry {
56
+ mode: "split";
57
+ handle: CachedQueryView<AnyQuery>;
58
+ leases: SplitLease[];
59
+ }
60
+
61
+ interface MaterializedCacheEntry extends BaseCacheEntry {
62
+ mode: "materialized";
63
+ leases: MaterializedLease[];
64
+ canonical: MaterializedLease;
65
+ }
66
+
67
+ type CacheEntry = SplitCacheEntry | MaterializedCacheEntry;
68
+
69
+ const EMPTY_ARRAY: readonly never[] = Object.freeze([]);
70
+
71
+ class RindleContextValue {
72
+ readonly store: Store<ColsMap>;
73
+ readonly cache: QueryCache;
74
+
75
+ constructor(store: Store<ColsMap>) {
76
+ this.store = store;
77
+ this.cache = new QueryCache(store);
78
+ }
79
+ }
80
+
81
+ const RindleContext = createContext<RindleContextValue | null>(null);
82
+
83
+ export function Rindle<S extends ColsMap>({ store, children }: RindleProps<S>) {
84
+ const value = useMemo(() => new RindleContextValue(store as unknown as Store<ColsMap>), [store]);
85
+ return createElement(RindleContext.Provider, { value }, children);
86
+ }
87
+
88
+ export const RindleProvider = Rindle;
89
+
90
+ export function useRindleStore<S extends ColsMap = ColsMap>(): Store<S> {
91
+ return useRindleContext().store as unknown as Store<S>;
92
+ }
93
+
94
+ export function useQuery<Q extends AnyQuery>(query: Q): QueryData<Q> {
95
+ const ctx = useRindleContext();
96
+ const descriptor = useMemo(() => describeQuery(query), [query]);
97
+ const queryRef = useRef(query);
98
+ queryRef.current = query;
99
+
100
+ const subscribe = useCallback(
101
+ (onStoreChange: () => void) => {
102
+ const lease = ctx.cache.retain(descriptor.viewKey, queryRef.current);
103
+ const unsubscribe = ctx.cache.subscribe(descriptor.viewKey, onStoreChange);
104
+ return () => {
105
+ unsubscribe();
106
+ ctx.cache.release(lease);
107
+ };
108
+ },
109
+ [ctx.cache, descriptor.leaseKey, descriptor.viewKey],
110
+ );
111
+
112
+ const getSnapshot = useCallback(
113
+ () => ctx.cache.snapshot(descriptor.viewKey, descriptor.one) as QueryData<Q>,
114
+ [ctx.cache, descriptor.viewKey, descriptor.one],
115
+ );
116
+
117
+ // SSR (SSR-DESIGN.md §6): the server calls `getServerSnapshot` (never `subscribe`), so it must
118
+ // surface the dehydrated/preloaded seed directly — not the live cache, which is never retained
119
+ // on the server. The client's hydration pass reads the same seed, matching the SSR markup.
120
+ const getServerSnapshot = useCallback(
121
+ () => ctx.cache.serverSnapshot(descriptor.viewKey, descriptor.one) as QueryData<Q>,
122
+ [ctx.cache, descriptor.viewKey, descriptor.one],
123
+ );
124
+
125
+ return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
126
+ }
127
+
128
+ /** The SERVER-CHANNEL state of a query's view (`@rindle/client` {@link ResultType}): `unknown` while
129
+ * it loads (not yet server-authoritative), `complete` once the server has answered. A pending
130
+ * optimistic mutation no longer moves this — that is a separate axis now (FOLDED-MUTATIONS-DESIGN
131
+ * §7); the `error` variant is reserved and currently unproduced. Shares the same cached/leased view
132
+ * as {@link useQuery} (so reading both for one query is one subscription), and re-renders only when
133
+ * the status changes. */
134
+ export function useQueryStatus(query: AnyQuery): ResultType {
135
+ const ctx = useRindleContext();
136
+ const descriptor = useMemo(() => describeQuery(query), [query]);
137
+ const queryRef = useRef(query);
138
+ queryRef.current = query;
139
+
140
+ const subscribe = useCallback(
141
+ (onStoreChange: () => void) => {
142
+ const lease = ctx.cache.retain(descriptor.viewKey, queryRef.current);
143
+ const unsubscribe = ctx.cache.subscribe(descriptor.viewKey, onStoreChange);
144
+ return () => {
145
+ unsubscribe();
146
+ ctx.cache.release(lease);
147
+ };
148
+ },
149
+ [ctx.cache, descriptor.leaseKey, descriptor.viewKey],
150
+ );
151
+
152
+ const getSnapshot = useCallback(() => ctx.cache.resultType(descriptor.viewKey), [ctx.cache, descriptor.viewKey]);
153
+
154
+ // SSR: a seeded query is server-authoritative for first paint (`complete`); otherwise `unknown`.
155
+ const getServerSnapshot = useCallback(
156
+ () => ctx.cache.serverResultType(descriptor.viewKey),
157
+ [ctx.cache, descriptor.viewKey],
158
+ );
159
+
160
+ return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
161
+ }
162
+
163
+ /**
164
+ * Read a {@link Fragment}'s slice off the shared root view (FRAGMENT-COMPOSITION-DESIGN.md §7).
165
+ *
166
+ * The waterfall-free contract: the ROOT component `useQuery`s the composed query (every child
167
+ * fragment spread into it), and every descendant calls `useFragment` — a PURE READ of the
168
+ * already-projected node at this fragment's alias — instead of opening its own `useQuery`. So a
169
+ * whole component tree resolves through the ONE query the root opened, with no request waterfall;
170
+ * a descendant that opens its own `useQuery` reintroduces the waterfall at that boundary.
171
+ *
172
+ * Phase 0 is a typed pass-through: `ref` already IS the projected node (the parent got it from
173
+ * `useQuery` and handed the slice down), so this returns it typed as the fragment's
174
+ * {@link FragmentRef}. `fragment` declares *which* selection this component reads (co-location +
175
+ * the seam masking plugs into); masking (Phase 2) narrows the returned shape with no call-site
176
+ * change. Not a subscription — it adds no lease and never re-renders on its own.
177
+ */
178
+ export function useFragment<F extends Fragment<any, any, any>>(fragment: F, ref: FragmentRef<F>): FragmentRef<F> {
179
+ void fragment;
180
+ return ref;
181
+ }
182
+
183
+ export class QueryCache {
184
+ private readonly entries = new Map<string, CacheEntry>();
185
+ private nextLeaseId = 1;
186
+ private readonly store: Store<ColsMap>;
187
+
188
+ constructor(store: Store<ColsMap>) {
189
+ this.store = store;
190
+ }
191
+
192
+ retain<Q extends AnyQuery>(viewKey: string, query: Q): QueryLease {
193
+ let entry = this.entries.get(viewKey);
194
+ if (!entry) {
195
+ entry = this.store.canRetainRemoteQueries()
196
+ ? this.createSplitEntry(viewKey, query)
197
+ : this.createMaterializedEntry(viewKey, query);
198
+ this.entries.set(viewKey, entry);
199
+ const lease = entry.leases[0];
200
+ return { id: lease.id, viewKey };
201
+ }
202
+ if (entry.mode === "split") {
203
+ const lease = this.createSplitLease(viewKey, entry.handle, query);
204
+ entry.leases.push(lease);
205
+ return { id: lease.id, viewKey };
206
+ }
207
+ const lease = this.createMaterializedLease(viewKey, query);
208
+ entry.leases.push(lease);
209
+ if (!entry.canonical.remote && lease.remote) this.setCanonical(entry, lease);
210
+ return { id: lease.id, viewKey };
211
+ }
212
+
213
+ release(lease: QueryLease): void {
214
+ const entry = this.entries.get(lease.viewKey);
215
+ if (!entry) return;
216
+ const index = entry.leases.findIndex((l) => l.id === lease.id);
217
+ if (index < 0) return;
218
+ if (entry.mode === "split") {
219
+ const [released] = entry.leases.splice(index, 1);
220
+ released.releaseRemote();
221
+ if (entry.leases.length > 0) return;
222
+ entry.canonicalUnsubscribe();
223
+ entry.handle.destroy();
224
+ this.entries.delete(lease.viewKey);
225
+ return;
226
+ }
227
+ const [released] = entry.leases.splice(index, 1);
228
+ if (entry.canonical.id === released.id) {
229
+ entry.canonicalUnsubscribe();
230
+ entry.canonicalUnsubscribe = () => {};
231
+ }
232
+ released.view.destroy();
233
+ if (entry.leases.length === 0) {
234
+ this.entries.delete(lease.viewKey);
235
+ return;
236
+ }
237
+ if (entry.canonical.id === released.id) this.setCanonical(entry, this.chooseCanonical(entry.leases));
238
+ }
239
+
240
+ subscribe(viewKey: string, listener: () => void): () => void {
241
+ const entry = this.entries.get(viewKey);
242
+ if (!entry) return () => {};
243
+ entry.listeners.add(listener);
244
+ return () => {
245
+ entry.listeners.delete(listener);
246
+ };
247
+ }
248
+
249
+ snapshot(viewKey: string, one: boolean): unknown {
250
+ const entry = this.entries.get(viewKey);
251
+ if (entry) return entry.mode === "split" ? entry.handle.view.data : entry.canonical.view.data;
252
+ return one ? null : EMPTY_ARRAY;
253
+ }
254
+
255
+ /** A query's current {@link ResultType} (from its view), or `unknown` before it is retained. */
256
+ resultType(viewKey: string): ResultType {
257
+ const entry = this.entries.get(viewKey);
258
+ if (!entry) return "unknown";
259
+ return entry.mode === "split" ? entry.handle.view.resultType : entry.canonical.view.resultType;
260
+ }
261
+
262
+ /** The SSR/hydration snapshot for `viewKey` — the store's preloaded/dehydrated seed, read
263
+ * WITHOUT retaining (the server never opens a subscription). Falls back to empty so a
264
+ * non-preloaded query renders like an unhydrated one. */
265
+ serverSnapshot(viewKey: string, one: boolean): unknown {
266
+ const seed = this.store.seedSnapshot(viewKey);
267
+ if (!seed) return one ? null : EMPTY_ARRAY;
268
+ return one ? (seed.rows[0] ?? null) : seed.rows;
269
+ }
270
+
271
+ /** The SSR {@link ResultType}: `complete` when a seed exists (server-authoritative first paint),
272
+ * else `unknown`. */
273
+ serverResultType(viewKey: string): ResultType {
274
+ return this.store.seedSnapshot(viewKey) ? "complete" : "unknown";
275
+ }
276
+
277
+ size(): number {
278
+ return this.entries.size;
279
+ }
280
+
281
+ private createSplitEntry<Q extends AnyQuery>(viewKey: string, query: Q): SplitCacheEntry {
282
+ const handle = this.store.createCachedQueryView(query) as unknown as CachedQueryView<AnyQuery>;
283
+ const entry: SplitCacheEntry = {
284
+ mode: "split",
285
+ handle,
286
+ leases: [],
287
+ canonicalUnsubscribe: () => {},
288
+ listeners: new Set(),
289
+ };
290
+ entry.leases.push(this.createSplitLease(viewKey, handle, query));
291
+ entry.canonicalUnsubscribe = handle.view.subscribe(() => {
292
+ for (const listener of entry.listeners) listener();
293
+ });
294
+ return entry;
295
+ }
296
+
297
+ private createSplitLease<Q extends AnyQuery>(
298
+ viewKey: string,
299
+ handle: CachedQueryView<AnyQuery>,
300
+ query: Q,
301
+ ): SplitLease {
302
+ return { id: this.nextLeaseId++, viewKey, releaseRemote: handle.retain(query) };
303
+ }
304
+
305
+ private createMaterializedEntry<Q extends AnyQuery>(viewKey: string, query: Q): MaterializedCacheEntry {
306
+ const lease = this.createMaterializedLease(viewKey, query);
307
+ const entry: MaterializedCacheEntry = {
308
+ mode: "materialized",
309
+ leases: [lease],
310
+ canonical: lease,
311
+ canonicalUnsubscribe: () => {},
312
+ listeners: new Set(),
313
+ };
314
+ this.setCanonical(entry, lease);
315
+ return entry;
316
+ }
317
+
318
+ private createMaterializedLease<Q extends AnyQuery>(viewKey: string, query: Q): MaterializedLease {
319
+ return {
320
+ id: this.nextLeaseId++,
321
+ viewKey,
322
+ view: this.store.materialize(query) as AnyView,
323
+ remote: typeof query.name === "string",
324
+ };
325
+ }
326
+
327
+ private chooseCanonical(leases: MaterializedLease[]): MaterializedLease {
328
+ return leases.find((lease) => lease.remote) ?? leases[0];
329
+ }
330
+
331
+ private setCanonical(entry: MaterializedCacheEntry, lease: MaterializedLease): void {
332
+ entry.canonicalUnsubscribe();
333
+ entry.canonical = lease;
334
+ entry.canonicalUnsubscribe = lease.view.subscribe(() => {
335
+ for (const listener of entry.listeners) listener();
336
+ });
337
+ }
338
+ }
339
+
340
+ export function queryCacheKey(query: AnyQuery): string {
341
+ return describeQuery(query).viewKey;
342
+ }
343
+
344
+ function useRindleContext(): RindleContextValue {
345
+ const ctx = useContext(RindleContext);
346
+ if (!ctx) throw new Error("Rindle context is missing. Wrap this tree in <Rindle store={store}>.");
347
+ return ctx;
348
+ }
349
+
350
+ function describeQuery(query: AnyQuery): QueryDescriptor {
351
+ const ast = query.ast();
352
+ const remote = typeof query.name === "string" ? { name: query.name, args: query.args } : null;
353
+ return {
354
+ // `viewKey` MUST match the SSR seed key (`@rindle/client`'s `stableKey(ast)`) so
355
+ // `getServerSnapshot` finds the dehydrated entry — one canonical serializer, shared.
356
+ viewKey: stableKey(ast),
357
+ leaseKey: stableKey({ ast, remote }),
358
+ one: ast.one === true,
359
+ };
360
+ }