@octanejs/livestore 0.0.1
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 +201 -0
- package/README.md +45 -0
- package/UPSTREAM.md +81 -0
- package/package.json +78 -0
- package/src/StoreRegistryContext.tsrx +26 -0
- package/src/experimental/components/LiveList.tsrx +75 -0
- package/src/experimental/mod.ts +1 -0
- package/src/internal.ts +38 -0
- package/src/mod.ts +14 -0
- package/src/useClientDocument.ts +113 -0
- package/src/useQuery.ts +139 -0
- package/src/useRcResource.ts +109 -0
- package/src/useStore.ts +64 -0
- package/src/useSyncStatus.ts +41 -0
- package/src/utils/useStateRefWithReactiveInput.ts +41 -0
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 2025 Overengineering Studio
|
|
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,45 @@
|
|
|
1
|
+
# @octanejs/livestore
|
|
2
|
+
|
|
3
|
+
Octane bindings for [LiveStore](https://livestore.dev/), ported from
|
|
4
|
+
`@livestore/react@0.4.0` at commit
|
|
5
|
+
`c80acb39066b9472da426a35c81969df4919ae2d`.
|
|
6
|
+
|
|
7
|
+
The package reuses LiveStore's published framework-neutral store, schema,
|
|
8
|
+
query, registry, and framework-toolkit packages. It replaces only the React
|
|
9
|
+
context, Suspense, hook, and component layer with Octane equivalents.
|
|
10
|
+
The exact upstream source, tests, snapshots, package metadata, and license are
|
|
11
|
+
vendored byte-for-byte under `upstream/`; [UPSTREAM.md](./UPSTREAM.md) records
|
|
12
|
+
the export and test crosswalk.
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
pnpm add @octanejs/livestore @livestore/adapter-web
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { StoreRegistry, storeOptions, useStore } from '@octanejs/livestore';
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
The stable root surface includes `StoreRegistryProvider`, `useStoreRegistry`,
|
|
23
|
+
`useStore`, `useQuery`, `useQueryRef`, `useClientDocument`, and
|
|
24
|
+
`useSyncStatus`. `LiveList` is available from both the root and
|
|
25
|
+
`@octanejs/livestore/experimental`, matching the pinned upstream release.
|
|
26
|
+
|
|
27
|
+
`ReactApi` and `withReactApi` retain their upstream names for source
|
|
28
|
+
compatibility, but attach Octane hooks and do not depend on React.
|
|
29
|
+
|
|
30
|
+
## Compatibility
|
|
31
|
+
|
|
32
|
+
- LiveStore dependencies are pinned to the coherent 0.4.0 release closure,
|
|
33
|
+
which uses Effect 3.
|
|
34
|
+
- Query errors are labeled `@livestore/octane:useQuery`. The pinned
|
|
35
|
+
framework-toolkit still uses an internal refresh-reason tag named `react`;
|
|
36
|
+
this diagnostic-only upstream detail is intentionally not forked.
|
|
37
|
+
- React Strict Mode double invocation is not emulated.
|
|
38
|
+
- The upstream short-`unusedCacheTime` retain timing limitation remains:
|
|
39
|
+
`useStore` retains in a passive effect after a successful render.
|
|
40
|
+
|
|
41
|
+
LiveStore is Apache-2.0 licensed. This adaptation preserves that license and
|
|
42
|
+
documents Octane-specific changes in this repository.
|
|
43
|
+
|
|
44
|
+
Maintainers can verify the pinned evidence with
|
|
45
|
+
`pnpm --dir packages/livestore upstream:verify`.
|
package/UPSTREAM.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# LiveStore React upstream contract
|
|
2
|
+
|
|
3
|
+
## Pin and source boundary
|
|
4
|
+
|
|
5
|
+
| Field | Value |
|
|
6
|
+
|---|---|
|
|
7
|
+
| Package | `@livestore/react` |
|
|
8
|
+
| Version | `0.4.0` |
|
|
9
|
+
| Canonical tag commit | `c80acb39066b9472da426a35c81969df4919ae2d` |
|
|
10
|
+
| Supported upstream range | exactly `0.4.0` |
|
|
11
|
+
| React oracle | `19.2.3` with `@types/react@19.2.7` |
|
|
12
|
+
| License | Apache-2.0 |
|
|
13
|
+
|
|
14
|
+
The npm package publishes `src/` and built `dist/`, but its repository contains
|
|
15
|
+
the authoritative React source, tests, snapshots, configuration, package
|
|
16
|
+
metadata, and license. The byte-exact `packages/@livestore/react` directory from
|
|
17
|
+
the canonical tag commit is vendored under `upstream/` and locked file-by-file
|
|
18
|
+
by `upstream/SHA256SUMS`. It is excluded from the published package by the
|
|
19
|
+
explicit `files` allowlist.
|
|
20
|
+
|
|
21
|
+
The port reuses LiveStore's released framework-neutral packages unchanged. Only
|
|
22
|
+
the React-facing binding under the pinned directory is adapted. `src/` mirrors
|
|
23
|
+
the upstream module layout; `.tsx` components become `.tsrx`, while hook modules
|
|
24
|
+
retain their upstream paths.
|
|
25
|
+
|
|
26
|
+
Run `pnpm --dir packages/livestore upstream:verify` to verify every vendored
|
|
27
|
+
byte and the recorded source/test inventory. The pristine React-parity lane
|
|
28
|
+
runs that same verifier before copying or executing the upstream suite.
|
|
29
|
+
|
|
30
|
+
## Runtime export crosswalk
|
|
31
|
+
|
|
32
|
+
| Upstream export | Octane disposition | Evidence |
|
|
33
|
+
|---|---|---|
|
|
34
|
+
| `Dispatch`, `SetStateAction`, `SetStateActionPartial`, `StateSetters` | Re-exported from the unchanged framework toolkit | `typetests/public-api.test-d.ts` |
|
|
35
|
+
| `captureStackInfo` | Re-exported from the unchanged framework toolkit | `tests/package-contract.test.ts` |
|
|
36
|
+
| `StoreRegistry`, `storeOptions` | Re-exported from the unchanged LiveStore core | `tests/package-contract.test.ts` |
|
|
37
|
+
| `StoreRegistryContext` | Ported to Octane context at `src/StoreRegistryContext.tsrx` | `tests/conformance/registry-context.test.ts` |
|
|
38
|
+
| `StoreRegistryProvider`, `StoreRegistryProviderProps` | Ported to Octane TSRX at the mirrored module path | `tests/lifecycle.test.ts`, `typetests/public-api.test-d.ts` |
|
|
39
|
+
| `useStoreRegistry` | Ported to Octane context with the upstream override behavior | `tests/conformance/registry-context.test.ts` |
|
|
40
|
+
| `useStore` | Ported to Octane Suspense, retention, and slot ownership | `tests/lifecycle.test.ts` |
|
|
41
|
+
| `ReactApi` | Historical upstream public name retained; members use Octane hooks | `typetests/public-api.test-d.ts` |
|
|
42
|
+
| `withReactApi` | Historical upstream public name retained and augments the same Store object | `tests/lifecycle.test.ts` |
|
|
43
|
+
| `useQuery`, `useQueryRef` | Ported over LiveStore's unchanged reactive-query core | `tests/query.test.ts` |
|
|
44
|
+
| `UseClientDocumentResult`, `useClientDocument` | Ported over the unchanged client-document core | `tests/document-sync.test.ts` |
|
|
45
|
+
| `useSyncStatus` | Ported to Octane's external-store subscription boundary | `tests/conformance/sync-status.test.ts`, `tests/ssr/server.test.ts` |
|
|
46
|
+
| `LiveList`, `LiveListProps` | Ported to keyed Octane `@for`; exported from root and `./experimental` | `tests/conformance/live-list.test.ts`, `typetests/public-api.test-d.ts` |
|
|
47
|
+
|
|
48
|
+
`useRcResource`, `useStateRefWithReactiveInput`, and
|
|
49
|
+
`__resetUseRcResourceCache` are upstream-private modules. They remain internal
|
|
50
|
+
implementation/test support and are not added to the public namespace.
|
|
51
|
+
|
|
52
|
+
## Test-suite disposition
|
|
53
|
+
|
|
54
|
+
The pinned React binding contains four runtime test files, one shared fixture,
|
|
55
|
+
and two snapshots. The source artifacts are all present under `upstream/`.
|
|
56
|
+
|
|
57
|
+
Parity-owned adapted cases live only in `tests/document-sync.test.ts`,
|
|
58
|
+
`tests/lifecycle.test.ts`, and `tests/query.test.ts`. Octane-only unpaired
|
|
59
|
+
cases live under `tests/conformance/` and are classified outside React-parity
|
|
60
|
+
ownership in `audit/test-classifications.json`.
|
|
61
|
+
|
|
62
|
+
| Upstream artifact | Disposition |
|
|
63
|
+
|---|---|
|
|
64
|
+
| `src/useRcResource.test.tsx` | Adapted by `tests/lifecycle.test.ts`; stable reuse, last-consumer cleanup, key replacement, scope replacement, and rapid replacement are covered. React StrictMode double-invoke is not applicable because Octane has no StrictMode double-invoke. |
|
|
65
|
+
| `src/useStore.test.tsx` | Adapted by `tests/lifecycle.test.ts`; shared registry promise identity belongs to unchanged `StoreRegistry`, while Suspense, stable re-render, error propagation, store switching, retention, and `useActionState` transition commit (`does not block useActionState transitions from committing`, upstream L176) are binding contracts. The upstream skipped `unusedCacheTime=0` case remains an upstream core issue, not a parity claim. Registry context override/missing-provider coverage is Octane-only in `tests/conformance/registry-context.test.ts`. |
|
|
66
|
+
| `src/useQuery.test.tsx` | Adapted by `tests/query.test.ts`; synchronous reads, commits, query identity changes, signals/query builders, and per-store query derivation are the observable contracts. React-window-specific lifecycle stress is React-only; keyed `LiveList` supplies Octane's equivalent list integration evidence in `tests/conformance/live-list.test.ts` (outside parity ownership). Query-resource last-consumer cleanup is Octane-only in `tests/conformance/query-resource.test.ts`. Internal reactivity-graph snapshots are deliberately not copied because repository testing guidance forbids pinning private graph shape. |
|
|
67
|
+
| `src/useClientDocument.test.tsx` | Adapted by `tests/document-sync.test.ts`; ID changes, value/functional/external updates, query chaining (`should work for a useClientDocument query chained with a useTemporary query`, upstream L195), and KV overwrite semantics (`kv client document overwrites value (Schema.Any, no partial merge)`, upstream L238) are binding contracts. Sync-status subscription/hydration is Octane-only in `tests/conformance/sync-status.test.ts`. OpenTelemetry span snapshots exercise unchanged LiveStore instrumentation internals and are not re-snapshotted by the Octane binding. |
|
|
68
|
+
| `src/__tests__/fixture.tsx` | Adapted through the released `@livestore/framework-toolkit/testing` TodoMVC fixture used by the Octane tests. |
|
|
69
|
+
| `src/__snapshots__/useQuery.test.tsx.snap` | Not ported: it pins framework-neutral private reactivity-graph representation, outside the binding observation boundary. |
|
|
70
|
+
| `src/__snapshots__/useClientDocument.test.tsx.snap` | Not ported: it pins framework-neutral OpenTelemetry and stack-frame serialization internals, outside the binding observation boundary. |
|
|
71
|
+
|
|
72
|
+
## Intentional divergences
|
|
73
|
+
|
|
74
|
+
- Octane has no StrictMode double-invocation, so the React suite's strict/non-strict
|
|
75
|
+
matrix collapses to one lifecycle lane.
|
|
76
|
+
- Upstream's framework toolkit still names the augmentation `ReactApi` and
|
|
77
|
+
`withReactApi`. Those names are preserved for source compatibility even though
|
|
78
|
+
their implementation is Octane-native.
|
|
79
|
+
- Public query instrumentation uses the label `octane`. The framework-neutral
|
|
80
|
+
toolkit's internal refresh tag remains `react` because that released core API
|
|
81
|
+
is reused unchanged.
|
package/package.json
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@octanejs/livestore",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=22.22.2"
|
|
9
|
+
},
|
|
10
|
+
"octane": {
|
|
11
|
+
"hookSlots": {
|
|
12
|
+
"manual": [
|
|
13
|
+
"src"
|
|
14
|
+
]
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"description": "LiveStore bindings for Octane, ported from @livestore/react over LiveStore's framework-neutral core.",
|
|
18
|
+
"author": {
|
|
19
|
+
"name": "Dominic Gannaway",
|
|
20
|
+
"email": "dg@domgan.com"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/octanejs/octane.git",
|
|
28
|
+
"directory": "packages/livestore"
|
|
29
|
+
},
|
|
30
|
+
"main": "src/mod.ts",
|
|
31
|
+
"module": "src/mod.ts",
|
|
32
|
+
"types": "src/mod.ts",
|
|
33
|
+
"files": [
|
|
34
|
+
"src",
|
|
35
|
+
"README.md",
|
|
36
|
+
"UPSTREAM.md",
|
|
37
|
+
"LICENSE"
|
|
38
|
+
],
|
|
39
|
+
"exports": {
|
|
40
|
+
".": "./src/mod.ts",
|
|
41
|
+
"./experimental": "./src/experimental/mod.ts"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@livestore/common": "0.4.0",
|
|
45
|
+
"@livestore/framework-toolkit": "0.4.0",
|
|
46
|
+
"@livestore/livestore": "0.4.0",
|
|
47
|
+
"@livestore/utils": "0.4.0",
|
|
48
|
+
"@opentelemetry/api": "1.9.0"
|
|
49
|
+
},
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"octane": "0.1.31"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@livestore/adapter-web": "0.4.0",
|
|
55
|
+
"@livestore/utils-dev": "0.4.0",
|
|
56
|
+
"@opentelemetry/sdk-trace-base": "2.2.0",
|
|
57
|
+
"@testing-library/dom": "10.4.1",
|
|
58
|
+
"@testing-library/react": "16.3.1",
|
|
59
|
+
"@types/react": "19.2.7",
|
|
60
|
+
"@types/react-dom": "19.2.3",
|
|
61
|
+
"@types/react-window": "1.8.8",
|
|
62
|
+
"@tsrx/react": "^0.2.56",
|
|
63
|
+
"esbuild": "^0.28.1",
|
|
64
|
+
"jsdom": "26.1.0",
|
|
65
|
+
"react": "19.2.3",
|
|
66
|
+
"react-dom": "19.2.3",
|
|
67
|
+
"react-window": "1.8.11",
|
|
68
|
+
"vite": "7.3.1",
|
|
69
|
+
"vitest": "^4.1.10",
|
|
70
|
+
"octane": "0.1.31"
|
|
71
|
+
},
|
|
72
|
+
"scripts": {
|
|
73
|
+
"test": "vitest run --config vitest.config.ts && vitest run --config vitest.ssr.config.ts",
|
|
74
|
+
"test:upstream": "node scripts/run-pristine-upstream.mjs",
|
|
75
|
+
"typecheck": "tsrx-tsc --noEmit -p tsconfig.json && tsrx-tsc --noEmit -p typetests/tsconfig.json",
|
|
76
|
+
"upstream:verify": "node scripts/verify-upstream.mjs"
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { createContext, useContext } from 'octane';
|
|
2
|
+
import type { ComponentBody, OctaneNode } from 'octane';
|
|
3
|
+
import type { StoreRegistry } from '@livestore/livestore';
|
|
4
|
+
|
|
5
|
+
export const StoreRegistryContext = createContext<StoreRegistry | undefined>(undefined);
|
|
6
|
+
|
|
7
|
+
export type StoreRegistryProviderProps = {
|
|
8
|
+
storeRegistry: StoreRegistry;
|
|
9
|
+
children?: OctaneNode;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const StoreRegistryProvider: ComponentBody<StoreRegistryProviderProps> = ({
|
|
13
|
+
storeRegistry,
|
|
14
|
+
children,
|
|
15
|
+
}) =>
|
|
16
|
+
<StoreRegistryContext.Provider value={storeRegistry}>{children}</StoreRegistryContext.Provider>;
|
|
17
|
+
|
|
18
|
+
export const useStoreRegistry = (override?: StoreRegistry): StoreRegistry => {
|
|
19
|
+
if (override !== undefined && typeof override !== 'symbol') return override;
|
|
20
|
+
|
|
21
|
+
const storeRegistry = useContext(StoreRegistryContext);
|
|
22
|
+
if (storeRegistry == null) {
|
|
23
|
+
throw new Error('useStoreRegistry() must be used within <StoreRegistryProvider>');
|
|
24
|
+
}
|
|
25
|
+
return storeRegistry;
|
|
26
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { LiveQueryDef, Store } from '@livestore/livestore';
|
|
2
|
+
import { computed } from '@livestore/livestore';
|
|
3
|
+
import { memo, useEffect, useMemo, useState } from 'octane';
|
|
4
|
+
import type { OctaneNode } from 'octane';
|
|
5
|
+
import { useQuery } from '../../useQuery';
|
|
6
|
+
|
|
7
|
+
export type LiveListProps<TItem> = {
|
|
8
|
+
items$: LiveQueryDef<ReadonlyArray<TItem>>;
|
|
9
|
+
renderItem: (item: TItem, options: { index: number; isInitialListRender: boolean }) => OctaneNode;
|
|
10
|
+
getKey: (item: TItem, index: number) => string | number;
|
|
11
|
+
store: Store<any, any>;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
type ItemWrapperProps<TItem> = {
|
|
15
|
+
itemKey: string | number;
|
|
16
|
+
item$: LiveQueryDef<TItem>;
|
|
17
|
+
index: number;
|
|
18
|
+
isInitialListRender: boolean;
|
|
19
|
+
renderItem: LiveListProps<TItem>['renderItem'];
|
|
20
|
+
store: Store<any, any>;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export function LiveList<TItem>({ items$, renderItem, getKey, store }: LiveListProps<TItem>) @{
|
|
24
|
+
const [hasMounted, setHasMounted] = useState(false);
|
|
25
|
+
useEffect(() => setHasMounted(true), []);
|
|
26
|
+
const keys = useQuery(computed((get) => get(items$).map(getKey)), { store });
|
|
27
|
+
const entries = useMemo(
|
|
28
|
+
() => keys.map(
|
|
29
|
+
(key, index) => ({
|
|
30
|
+
key,
|
|
31
|
+
index,
|
|
32
|
+
item$: computed((get) => get(items$).find((item) => getKey(item, 0) === key)!, {
|
|
33
|
+
deps: [key],
|
|
34
|
+
}) as LiveQueryDef<TItem>,
|
|
35
|
+
}),
|
|
36
|
+
),
|
|
37
|
+
[getKey, items$, keys],
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
<>
|
|
41
|
+
@for (const entry of entries; key entry.key) {
|
|
42
|
+
<ItemWrapperMemo
|
|
43
|
+
itemKey={entry.key}
|
|
44
|
+
item$={entry.item$}
|
|
45
|
+
store={store}
|
|
46
|
+
index={entry.index}
|
|
47
|
+
isInitialListRender={!hasMounted}
|
|
48
|
+
renderItem={renderItem}
|
|
49
|
+
/>
|
|
50
|
+
}
|
|
51
|
+
</>
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function ItemWrapper<TItem>({
|
|
55
|
+
item$,
|
|
56
|
+
index,
|
|
57
|
+
isInitialListRender,
|
|
58
|
+
renderItem,
|
|
59
|
+
store,
|
|
60
|
+
}: ItemWrapperProps<TItem>) @{
|
|
61
|
+
const item = useQuery(item$, { store });
|
|
62
|
+
const options = useMemo(() => ({ index, isInitialListRender }), [index, isInitialListRender]);
|
|
63
|
+
<>
|
|
64
|
+
{renderItem(item, options)}
|
|
65
|
+
</>
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const ItemWrapperMemo = memo(
|
|
69
|
+
ItemWrapper,
|
|
70
|
+
(previous, next) =>
|
|
71
|
+
previous.itemKey === next.itemKey && previous.renderItem === next.renderItem &&
|
|
72
|
+
previous.store === next.store &&
|
|
73
|
+
previous.index === next.index &&
|
|
74
|
+
previous.isInitialListRender === next.isInitialListRender,
|
|
75
|
+
) as typeof ItemWrapper;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { LiveList, type LiveListProps } from './components/LiveList.tsrx';
|
package/src/internal.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const subSlotCache = new Map<symbol, Map<string, symbol>>();
|
|
2
|
+
const bareTagCache = new Map<string, symbol>();
|
|
3
|
+
|
|
4
|
+
export function subSlot(slot: symbol | undefined, tag: string): symbol {
|
|
5
|
+
if (slot === undefined) {
|
|
6
|
+
let bare = bareTagCache.get(tag);
|
|
7
|
+
if (bare === undefined) {
|
|
8
|
+
bare = Symbol.for(`@octanejs/livestore:${tag}`);
|
|
9
|
+
bareTagCache.set(tag, bare);
|
|
10
|
+
}
|
|
11
|
+
return bare;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
let byTag = subSlotCache.get(slot);
|
|
15
|
+
if (byTag === undefined) {
|
|
16
|
+
byTag = new Map();
|
|
17
|
+
subSlotCache.set(slot, byTag);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let child = byTag.get(tag);
|
|
21
|
+
if (child === undefined) {
|
|
22
|
+
child = Symbol.for(`${slot.description ?? ''}:${tag}`);
|
|
23
|
+
byTag.set(tag, child);
|
|
24
|
+
}
|
|
25
|
+
return child;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function splitSlot(
|
|
29
|
+
args: unknown[],
|
|
30
|
+
preserveSingleSymbol?: (value: symbol) => boolean,
|
|
31
|
+
): [unknown[], symbol | undefined] {
|
|
32
|
+
const tail = args[args.length - 1];
|
|
33
|
+
const slot =
|
|
34
|
+
typeof tail === 'symbol' && !(args.length === 1 && preserveSingleSymbol?.(tail))
|
|
35
|
+
? tail
|
|
36
|
+
: undefined;
|
|
37
|
+
return [slot === undefined ? args : args.slice(0, -1), slot];
|
|
38
|
+
}
|
package/src/mod.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
Dispatch,
|
|
3
|
+
SetStateAction,
|
|
4
|
+
SetStateActionPartial,
|
|
5
|
+
StateSetters,
|
|
6
|
+
} from '@livestore/framework-toolkit';
|
|
7
|
+
export { captureStackInfo } from '@livestore/framework-toolkit';
|
|
8
|
+
export { StoreRegistry, storeOptions } from '@livestore/livestore';
|
|
9
|
+
export { LiveList, type LiveListProps } from './experimental/components/LiveList.tsrx';
|
|
10
|
+
export * from './StoreRegistryContext.tsrx';
|
|
11
|
+
export { type UseClientDocumentResult, useClientDocument } from './useClientDocument';
|
|
12
|
+
export { useQuery, useQueryRef } from './useQuery';
|
|
13
|
+
export { type ReactApi, useStore, withReactApi } from './useStore';
|
|
14
|
+
export { useSyncStatus } from './useSyncStatus';
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type { RowQuery } from '@livestore/common';
|
|
2
|
+
import { SessionIdSymbol } from '@livestore/common';
|
|
3
|
+
import { State } from '@livestore/common/schema';
|
|
4
|
+
import {
|
|
5
|
+
removeUndefinedValues,
|
|
6
|
+
type StateSetters,
|
|
7
|
+
validateTableOptions,
|
|
8
|
+
} from '@livestore/framework-toolkit';
|
|
9
|
+
import type { LiveQuery, LiveQueryDef, Store } from '@livestore/livestore';
|
|
10
|
+
import { queryDb } from '@livestore/livestore';
|
|
11
|
+
import { omitUndefineds, shouldNeverHappen } from '@livestore/utils';
|
|
12
|
+
import { useMemo } from 'octane';
|
|
13
|
+
import { splitSlot, subSlot } from './internal';
|
|
14
|
+
import { useQueryRef } from './useQuery';
|
|
15
|
+
|
|
16
|
+
export type UseClientDocumentResult<
|
|
17
|
+
TTableDef extends State.SQLite.ClientDocumentTableDef.TraitAny,
|
|
18
|
+
> = [
|
|
19
|
+
row: TTableDef['Value'],
|
|
20
|
+
setRow: StateSetters<TTableDef>,
|
|
21
|
+
id: string,
|
|
22
|
+
query$: LiveQuery<TTableDef['Value']>,
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
export function useClientDocument<
|
|
26
|
+
TTableDef extends State.SQLite.ClientDocumentTableDef.Trait<
|
|
27
|
+
any,
|
|
28
|
+
any,
|
|
29
|
+
any,
|
|
30
|
+
{ partialSet: boolean; default: any }
|
|
31
|
+
>,
|
|
32
|
+
>(
|
|
33
|
+
table: TTableDef,
|
|
34
|
+
id?: State.SQLite.ClientDocumentTableDef.DefaultIdType<TTableDef> | SessionIdSymbol,
|
|
35
|
+
options?: Partial<RowQuery.GetOrCreateOptions<TTableDef>>,
|
|
36
|
+
): UseClientDocumentResult<TTableDef>;
|
|
37
|
+
export function useClientDocument<
|
|
38
|
+
TTableDef extends State.SQLite.ClientDocumentTableDef.Trait<
|
|
39
|
+
any,
|
|
40
|
+
any,
|
|
41
|
+
any,
|
|
42
|
+
{ partialSet: boolean; default: any }
|
|
43
|
+
>,
|
|
44
|
+
>(
|
|
45
|
+
table: TTableDef,
|
|
46
|
+
id: State.SQLite.ClientDocumentTableDef.DefaultIdType<TTableDef> | string | SessionIdSymbol,
|
|
47
|
+
options?: Partial<RowQuery.GetOrCreateOptions<TTableDef>>,
|
|
48
|
+
): UseClientDocumentResult<TTableDef>;
|
|
49
|
+
export function useClientDocument<TTableDef extends State.SQLite.ClientDocumentTableDef.Any>(
|
|
50
|
+
table: TTableDef,
|
|
51
|
+
idOrOptions:
|
|
52
|
+
string | SessionIdSymbol | Partial<RowQuery.GetOrCreateOptions<TTableDef>> | undefined,
|
|
53
|
+
options: Partial<RowQuery.GetOrCreateOptions<TTableDef>> | undefined,
|
|
54
|
+
storeArg: { store?: Store } | undefined,
|
|
55
|
+
slot: symbol,
|
|
56
|
+
): UseClientDocumentResult<TTableDef>;
|
|
57
|
+
export function useClientDocument<TTableDef extends State.SQLite.ClientDocumentTableDef.Any>(
|
|
58
|
+
table: TTableDef,
|
|
59
|
+
...rest: [
|
|
60
|
+
idOrOptions?: string | SessionIdSymbol | Partial<RowQuery.GetOrCreateOptions<TTableDef>>,
|
|
61
|
+
options?: Partial<RowQuery.GetOrCreateOptions<TTableDef>>,
|
|
62
|
+
storeArg?: { store?: Store },
|
|
63
|
+
slot?: symbol,
|
|
64
|
+
]
|
|
65
|
+
): UseClientDocumentResult<TTableDef> {
|
|
66
|
+
const [args, slot] = splitSlot(rest, (value) => value === SessionIdSymbol);
|
|
67
|
+
const idOrOptions = args[0] as
|
|
68
|
+
string | SessionIdSymbol | Partial<RowQuery.GetOrCreateOptions<TTableDef>> | undefined;
|
|
69
|
+
const optionsArgument = args[1] as Partial<RowQuery.GetOrCreateOptions<TTableDef>> | undefined;
|
|
70
|
+
const storeArg = args[2] as { store?: Store } | undefined;
|
|
71
|
+
const id =
|
|
72
|
+
typeof idOrOptions === 'string' || idOrOptions === SessionIdSymbol
|
|
73
|
+
? idOrOptions
|
|
74
|
+
: table[State.SQLite.ClientDocumentTableDefSymbol].options.default.id;
|
|
75
|
+
const options =
|
|
76
|
+
typeof idOrOptions === 'string' || idOrOptions === SessionIdSymbol
|
|
77
|
+
? optionsArgument
|
|
78
|
+
: idOrOptions;
|
|
79
|
+
const { default: defaultValues } = options ?? {};
|
|
80
|
+
|
|
81
|
+
useMemo(() => validateTableOptions(table), [table], subSlot(slot, 'document:validate'));
|
|
82
|
+
const tableName = table.sqliteDef.name;
|
|
83
|
+
const store = storeArg?.store ?? shouldNeverHappen('No store provided to useClientDocument');
|
|
84
|
+
const idStr: string = id === SessionIdSymbol ? store.sessionId : id;
|
|
85
|
+
type QueryDef = LiveQueryDef<TTableDef['Value']>;
|
|
86
|
+
const queryDef: QueryDef = useMemo(
|
|
87
|
+
() =>
|
|
88
|
+
queryDb(table.get(id, { default: defaultValues! }), {
|
|
89
|
+
deps: [idStr, tableName, JSON.stringify(defaultValues)],
|
|
90
|
+
}),
|
|
91
|
+
[table, id, defaultValues, idStr],
|
|
92
|
+
subSlot(slot, 'document:query'),
|
|
93
|
+
);
|
|
94
|
+
const queryRef = useQueryRef(
|
|
95
|
+
queryDef,
|
|
96
|
+
{
|
|
97
|
+
otelSpanName: `LiveStore:useClientDocument:${tableName}:${idStr}`,
|
|
98
|
+
...omitUndefineds({ store: storeArg?.store }),
|
|
99
|
+
},
|
|
100
|
+
subSlot(slot, 'document:query-ref'),
|
|
101
|
+
);
|
|
102
|
+
const setState = useMemo<StateSetters<TTableDef>>(
|
|
103
|
+
() => (newValueOrFn: TTableDef['Value']) => {
|
|
104
|
+
const newValue =
|
|
105
|
+
typeof newValueOrFn === 'function' ? newValueOrFn(queryRef.valueRef.current) : newValueOrFn;
|
|
106
|
+
if (queryRef.valueRef.current === newValue) return;
|
|
107
|
+
store.commit(table.set(removeUndefinedValues(newValue), id));
|
|
108
|
+
},
|
|
109
|
+
[id, queryRef.valueRef, store, table],
|
|
110
|
+
subSlot(slot, 'document:set'),
|
|
111
|
+
);
|
|
112
|
+
return [queryRef.valueRef.current, setState, idStr, queryRef.queryRcRef.value];
|
|
113
|
+
}
|
package/src/useQuery.ts
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import type * as otel from '@opentelemetry/api';
|
|
2
|
+
import {
|
|
3
|
+
captureStackInfo,
|
|
4
|
+
computeRcRefKey,
|
|
5
|
+
createQueryResource,
|
|
6
|
+
type NormalizedQueryable,
|
|
7
|
+
normalizeQueryable,
|
|
8
|
+
runInitialQuery,
|
|
9
|
+
} from '@livestore/framework-toolkit';
|
|
10
|
+
import type { LiveQuery, Queryable, Store } from '@livestore/livestore';
|
|
11
|
+
import type { LiveQueries } from '@livestore/livestore/internal';
|
|
12
|
+
import { deepEqual, shouldNeverHappen } from '@livestore/utils';
|
|
13
|
+
import { useDebugValue, useEffect, useMemo } from 'octane';
|
|
14
|
+
import { splitSlot, subSlot } from './internal';
|
|
15
|
+
import { useRcResource } from './useRcResource';
|
|
16
|
+
import { useStateRefWithReactiveInput } from './utils/useStateRefWithReactiveInput';
|
|
17
|
+
|
|
18
|
+
type ValueRef<T> = { current: T };
|
|
19
|
+
type QueryOptions = {
|
|
20
|
+
store?: Store;
|
|
21
|
+
otelContext?: otel.Context;
|
|
22
|
+
otelSpanName?: string;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export function useQuery<TQueryable extends Queryable<any>>(
|
|
26
|
+
queryable: TQueryable,
|
|
27
|
+
options?: { store?: Store },
|
|
28
|
+
): Queryable.Result<TQueryable>;
|
|
29
|
+
export function useQuery<TQueryable extends Queryable<any>>(
|
|
30
|
+
queryable: TQueryable,
|
|
31
|
+
...rest: [options?: { store?: Store }, slot?: symbol]
|
|
32
|
+
): Queryable.Result<TQueryable> {
|
|
33
|
+
const [args, slot] = splitSlot(rest);
|
|
34
|
+
return useQueryRef(queryable, args[0] as QueryOptions | undefined, subSlot(slot, 'query:ref'))
|
|
35
|
+
.valueRef.current;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function useQueryRef<TQueryable extends Queryable<any>>(
|
|
39
|
+
queryable: TQueryable,
|
|
40
|
+
options?: QueryOptions,
|
|
41
|
+
): {
|
|
42
|
+
valueRef: ValueRef<Queryable.Result<TQueryable>>;
|
|
43
|
+
queryRcRef: LiveQueries.RcRef<LiveQuery<Queryable.Result<TQueryable>>>;
|
|
44
|
+
};
|
|
45
|
+
export function useQueryRef<TQueryable extends Queryable<any>>(
|
|
46
|
+
queryable: TQueryable,
|
|
47
|
+
options: QueryOptions | undefined,
|
|
48
|
+
slot: symbol,
|
|
49
|
+
): {
|
|
50
|
+
valueRef: ValueRef<Queryable.Result<TQueryable>>;
|
|
51
|
+
queryRcRef: LiveQueries.RcRef<LiveQuery<Queryable.Result<TQueryable>>>;
|
|
52
|
+
};
|
|
53
|
+
export function useQueryRef<TQueryable extends Queryable<any>>(
|
|
54
|
+
queryable: TQueryable,
|
|
55
|
+
...rest: [options?: QueryOptions, slot?: symbol]
|
|
56
|
+
): {
|
|
57
|
+
valueRef: ValueRef<Queryable.Result<TQueryable>>;
|
|
58
|
+
queryRcRef: LiveQueries.RcRef<LiveQuery<Queryable.Result<TQueryable>>>;
|
|
59
|
+
} {
|
|
60
|
+
const [args, slot] = splitSlot(rest);
|
|
61
|
+
const options = args[0] as QueryOptions | undefined;
|
|
62
|
+
const store = options?.store ?? shouldNeverHappen('No store provided to useQuery');
|
|
63
|
+
type TResult = Queryable.Result<TQueryable>;
|
|
64
|
+
|
|
65
|
+
const normalized = useMemo<NormalizedQueryable<TResult>>(
|
|
66
|
+
() => normalizeQueryable(queryable as Queryable<TResult>),
|
|
67
|
+
[queryable],
|
|
68
|
+
subSlot(slot, 'query:normalized'),
|
|
69
|
+
);
|
|
70
|
+
const rcRefKey = useMemo(
|
|
71
|
+
() => computeRcRefKey(store, normalized),
|
|
72
|
+
[normalized, store],
|
|
73
|
+
subSlot(slot, 'query:key'),
|
|
74
|
+
);
|
|
75
|
+
const stackInfo = useMemo(() => captureStackInfo(), [], subSlot(slot, 'query:stack'));
|
|
76
|
+
|
|
77
|
+
const { queryRcRef, span, otelContext } = useRcResource(
|
|
78
|
+
store,
|
|
79
|
+
rcRefKey,
|
|
80
|
+
() =>
|
|
81
|
+
createQueryResource(store, normalized, stackInfo, {
|
|
82
|
+
otelSpanName: options?.otelSpanName,
|
|
83
|
+
otelContext: options?.otelContext,
|
|
84
|
+
}),
|
|
85
|
+
() => {},
|
|
86
|
+
undefined,
|
|
87
|
+
subSlot(slot, 'query:resource'),
|
|
88
|
+
);
|
|
89
|
+
const query$ = queryRcRef.value;
|
|
90
|
+
useDebugValue(
|
|
91
|
+
`LiveStore:useQuery:${query$.id}:${query$.label}`,
|
|
92
|
+
undefined,
|
|
93
|
+
subSlot(slot, 'query:debug'),
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
const initialResult = useMemo(
|
|
97
|
+
() => runInitialQuery(query$, otelContext, stackInfo, 'octane'),
|
|
98
|
+
[otelContext, query$, stackInfo],
|
|
99
|
+
subSlot(slot, 'query:initial'),
|
|
100
|
+
);
|
|
101
|
+
const [valueRef, setValue] = useStateRefWithReactiveInput(
|
|
102
|
+
initialResult,
|
|
103
|
+
subSlot(slot, 'query:state'),
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
useEffect(
|
|
107
|
+
() => {
|
|
108
|
+
query$.activeSubscriptions.add(stackInfo);
|
|
109
|
+
span.updateName(options?.otelSpanName ?? `LiveStore:useQuery:${query$.label}`);
|
|
110
|
+
return store.subscribe(
|
|
111
|
+
query$,
|
|
112
|
+
(newValue) => {
|
|
113
|
+
if (deepEqual(newValue, valueRef.current) === false) setValue(newValue);
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
onUnsubsubscribe: () => query$.activeSubscriptions.delete(stackInfo),
|
|
117
|
+
label: query$.label,
|
|
118
|
+
otelContext,
|
|
119
|
+
},
|
|
120
|
+
);
|
|
121
|
+
},
|
|
122
|
+
[stackInfo, query$, setValue, store, valueRef, otelContext, span, options?.otelSpanName],
|
|
123
|
+
subSlot(slot, 'query:subscribe'),
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
useRcResource(
|
|
127
|
+
store,
|
|
128
|
+
rcRefKey,
|
|
129
|
+
() => ({ queryRcRef, span }),
|
|
130
|
+
(resource) => {
|
|
131
|
+
resource.queryRcRef.deref();
|
|
132
|
+
resource.span.end();
|
|
133
|
+
},
|
|
134
|
+
undefined,
|
|
135
|
+
subSlot(slot, 'query:lifetime'),
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
return { valueRef, queryRcRef };
|
|
139
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { useEffect, useMemo, useRef } from 'octane';
|
|
2
|
+
import { splitSlot, subSlot } from './internal';
|
|
3
|
+
|
|
4
|
+
type Entry = { _tag: 'active'; rc: number; resource: unknown } | { _tag: 'destroyed' };
|
|
5
|
+
type Bucket = Map<string, Entry>;
|
|
6
|
+
|
|
7
|
+
let scopedBuckets = new WeakMap<object, Bucket>();
|
|
8
|
+
|
|
9
|
+
const getBucket = (scope: object): Bucket => {
|
|
10
|
+
let bucket = scopedBuckets.get(scope);
|
|
11
|
+
if (bucket === undefined) {
|
|
12
|
+
bucket = new Map();
|
|
13
|
+
scopedBuckets.set(scope, bucket);
|
|
14
|
+
}
|
|
15
|
+
return bucket;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export function useRcResource<T>(
|
|
19
|
+
scope: object,
|
|
20
|
+
key: string,
|
|
21
|
+
create: () => T,
|
|
22
|
+
dispose: (resource: NoInfer<T>) => void,
|
|
23
|
+
options?: { debugPrint?: (resource: NoInfer<T>) => ReadonlyArray<unknown> },
|
|
24
|
+
): T;
|
|
25
|
+
export function useRcResource<T>(
|
|
26
|
+
scope: object,
|
|
27
|
+
key: string,
|
|
28
|
+
create: () => T,
|
|
29
|
+
dispose: (resource: NoInfer<T>) => void,
|
|
30
|
+
options: { debugPrint?: (resource: NoInfer<T>) => ReadonlyArray<unknown> } | undefined,
|
|
31
|
+
slot: symbol,
|
|
32
|
+
): T;
|
|
33
|
+
export function useRcResource<T>(
|
|
34
|
+
scope: object,
|
|
35
|
+
key: string,
|
|
36
|
+
create: () => T,
|
|
37
|
+
dispose: (resource: NoInfer<T>) => void,
|
|
38
|
+
...rest: [
|
|
39
|
+
options?: { debugPrint?: (resource: NoInfer<T>) => ReadonlyArray<unknown> },
|
|
40
|
+
slot?: symbol,
|
|
41
|
+
]
|
|
42
|
+
): T {
|
|
43
|
+
const [args, slot] = splitSlot(rest);
|
|
44
|
+
void args[0];
|
|
45
|
+
|
|
46
|
+
const heldRef = useRef<{ scope: object; key: string; resource: T } | undefined>(
|
|
47
|
+
undefined,
|
|
48
|
+
subSlot(slot, 'rc:held'),
|
|
49
|
+
);
|
|
50
|
+
const createRef = useRef(create, subSlot(slot, 'rc:create'));
|
|
51
|
+
const disposeRef = useRef(dispose, subSlot(slot, 'rc:dispose'));
|
|
52
|
+
|
|
53
|
+
createRef.current = create;
|
|
54
|
+
disposeRef.current = dispose;
|
|
55
|
+
|
|
56
|
+
const release = (held: { scope: object; key: string; resource: T }) => {
|
|
57
|
+
const bucket = getBucket(held.scope);
|
|
58
|
+
const cachedItem = bucket.get(held.key);
|
|
59
|
+
if (cachedItem?._tag !== 'active') return;
|
|
60
|
+
cachedItem.rc--;
|
|
61
|
+
if (cachedItem.rc === 0) {
|
|
62
|
+
disposeRef.current(cachedItem.resource as T);
|
|
63
|
+
bucket.delete(held.key);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const resource = useMemo(
|
|
68
|
+
() => {
|
|
69
|
+
const previous = heldRef.current;
|
|
70
|
+
if (previous !== undefined) release(previous);
|
|
71
|
+
|
|
72
|
+
const bucket = getBucket(scope);
|
|
73
|
+
const cachedItem = bucket.get(key);
|
|
74
|
+
let next: T;
|
|
75
|
+
if (cachedItem?._tag === 'active') {
|
|
76
|
+
cachedItem.rc++;
|
|
77
|
+
next = cachedItem.resource as T;
|
|
78
|
+
} else {
|
|
79
|
+
next = createRef.current();
|
|
80
|
+
bucket.set(key, { _tag: 'active', rc: 1, resource: next });
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
heldRef.current = { scope, key, resource: next };
|
|
84
|
+
return next;
|
|
85
|
+
},
|
|
86
|
+
[scope, key],
|
|
87
|
+
subSlot(slot, 'rc:memo'),
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
useEffect(
|
|
91
|
+
() => {
|
|
92
|
+
const held = heldRef.current;
|
|
93
|
+
return () => {
|
|
94
|
+
if (held !== undefined && heldRef.current === held) {
|
|
95
|
+
release(held);
|
|
96
|
+
heldRef.current = undefined;
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
},
|
|
100
|
+
[scope, key],
|
|
101
|
+
subSlot(slot, 'rc:effect'),
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
return resource;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export const __resetUseRcResourceCache = (): void => {
|
|
108
|
+
scopedBuckets = new WeakMap();
|
|
109
|
+
};
|
package/src/useStore.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { LiveStoreSchema } from '@livestore/common/schema';
|
|
2
|
+
import { SessionIdSymbol } from '@livestore/common';
|
|
3
|
+
import type { RegistryStoreOptions, Store, SyncStatus } from '@livestore/livestore';
|
|
4
|
+
import type { Schema } from '@livestore/utils/effect';
|
|
5
|
+
import { use, useEffect } from 'octane';
|
|
6
|
+
import { useStoreRegistry } from './StoreRegistryContext.tsrx';
|
|
7
|
+
import { splitSlot, subSlot } from './internal';
|
|
8
|
+
import { useClientDocument } from './useClientDocument';
|
|
9
|
+
import { useQuery } from './useQuery';
|
|
10
|
+
import { useSyncStatus } from './useSyncStatus';
|
|
11
|
+
|
|
12
|
+
export type ReactApi = {
|
|
13
|
+
useQuery: typeof useQuery;
|
|
14
|
+
useClientDocument: typeof useClientDocument;
|
|
15
|
+
useSyncStatus: () => SyncStatus;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export function useStore<
|
|
19
|
+
TSchema extends LiveStoreSchema,
|
|
20
|
+
TContext = {},
|
|
21
|
+
TSyncPayloadSchema extends Schema.Schema<any> = typeof Schema.JsonValue,
|
|
22
|
+
>(
|
|
23
|
+
options: RegistryStoreOptions<TSchema, TContext, TSyncPayloadSchema>,
|
|
24
|
+
): Store<TSchema, TContext> & ReactApi;
|
|
25
|
+
export function useStore<
|
|
26
|
+
TSchema extends LiveStoreSchema,
|
|
27
|
+
TContext = {},
|
|
28
|
+
TSyncPayloadSchema extends Schema.Schema<any> = typeof Schema.JsonValue,
|
|
29
|
+
>(
|
|
30
|
+
options: RegistryStoreOptions<TSchema, TContext, TSyncPayloadSchema>,
|
|
31
|
+
...rest: [slot?: symbol]
|
|
32
|
+
): Store<TSchema, TContext> & ReactApi {
|
|
33
|
+
const [, slot] = splitSlot(rest);
|
|
34
|
+
const storeRegistry = useStoreRegistry();
|
|
35
|
+
const storeOrPromise = storeRegistry.getOrLoadPromise(options);
|
|
36
|
+
const store = storeOrPromise instanceof Promise ? use(storeOrPromise) : storeOrPromise;
|
|
37
|
+
useEffect(
|
|
38
|
+
() => storeRegistry.retain(options),
|
|
39
|
+
[storeRegistry, options],
|
|
40
|
+
subSlot(slot, 'store:retain'),
|
|
41
|
+
);
|
|
42
|
+
return withReactApi(store);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const withReactApi = <TSchema extends LiveStoreSchema, TContext = {}>(
|
|
46
|
+
store: Store<TSchema, TContext>,
|
|
47
|
+
): Store<TSchema, TContext> & ReactApi => {
|
|
48
|
+
const augmented = store as any;
|
|
49
|
+
augmented.useQuery = (queryable: unknown, ...rest: unknown[]) =>
|
|
50
|
+
(useQuery as (...args: unknown[]) => unknown)(queryable, { store }, ...rest);
|
|
51
|
+
augmented.useClientDocument = (table: unknown, ...rest: unknown[]) => {
|
|
52
|
+
const [args, slot] = splitSlot(rest, (value) => value === SessionIdSymbol);
|
|
53
|
+
return (useClientDocument as (...args: unknown[]) => unknown)(
|
|
54
|
+
table,
|
|
55
|
+
args[0],
|
|
56
|
+
args[1],
|
|
57
|
+
{ store },
|
|
58
|
+
slot,
|
|
59
|
+
);
|
|
60
|
+
};
|
|
61
|
+
augmented.useSyncStatus = (...rest: unknown[]) =>
|
|
62
|
+
(useSyncStatus as (...args: unknown[]) => SyncStatus)({ store }, ...rest);
|
|
63
|
+
return augmented;
|
|
64
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Store, SyncStatus } from '@livestore/livestore';
|
|
2
|
+
import { useDebugValue, useEffect, useRef, useState } from 'octane';
|
|
3
|
+
import { splitSlot, subSlot } from './internal';
|
|
4
|
+
|
|
5
|
+
export function useSyncStatus(options: { store: Store<any> }): SyncStatus;
|
|
6
|
+
export function useSyncStatus(
|
|
7
|
+
options: { store: Store<any> },
|
|
8
|
+
...rest: [slot?: symbol]
|
|
9
|
+
): SyncStatus {
|
|
10
|
+
const [, slot] = splitSlot(rest);
|
|
11
|
+
const { store } = options;
|
|
12
|
+
const [, rerender] = useState(0, subSlot(slot, 'sync:status'));
|
|
13
|
+
const current = useRef({ store, status: store.syncStatus() }, subSlot(slot, 'sync:current'));
|
|
14
|
+
if (current.current.store !== store) {
|
|
15
|
+
current.current = { store, status: store.syncStatus() };
|
|
16
|
+
}
|
|
17
|
+
useEffect(
|
|
18
|
+
function subscribeSyncStatusEffect() {
|
|
19
|
+
const subscribedStore = store;
|
|
20
|
+
return subscribedStore.subscribeSyncStatus(function onSyncStatus(status) {
|
|
21
|
+
// Ignore emissions from a previous store while effect cleanup is pending.
|
|
22
|
+
if (current.current.store !== subscribedStore) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
current.current.status = status;
|
|
26
|
+
rerender(function increment(value) {
|
|
27
|
+
return value + 1;
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
},
|
|
31
|
+
[store],
|
|
32
|
+
subSlot(slot, 'sync:effect'),
|
|
33
|
+
);
|
|
34
|
+
const { status } = current.current;
|
|
35
|
+
useDebugValue(
|
|
36
|
+
`LiveStore:useSyncStatus:${status.isSynced === true ? 'synced' : 'pending'}`,
|
|
37
|
+
undefined,
|
|
38
|
+
subSlot(slot, 'sync:debug'),
|
|
39
|
+
);
|
|
40
|
+
return status;
|
|
41
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { useCallback, useRef, useState } from 'octane';
|
|
2
|
+
import { subSlot } from '../internal';
|
|
3
|
+
|
|
4
|
+
type StateRef<T> = { current: T };
|
|
5
|
+
type StateUpdate<T> = T | ((previous: T) => T);
|
|
6
|
+
|
|
7
|
+
export function useStateRefWithReactiveInput<T>(
|
|
8
|
+
inputState: T,
|
|
9
|
+
): [StateRef<T>, (newState: StateUpdate<T>) => void];
|
|
10
|
+
export function useStateRefWithReactiveInput<T>(
|
|
11
|
+
inputState: T,
|
|
12
|
+
slot: symbol,
|
|
13
|
+
): [StateRef<T>, (newState: StateUpdate<T>) => void];
|
|
14
|
+
export function useStateRefWithReactiveInput<T>(
|
|
15
|
+
inputState: T,
|
|
16
|
+
slot?: symbol,
|
|
17
|
+
): [StateRef<T>, (newState: StateUpdate<T>) => void] {
|
|
18
|
+
const [, rerender] = useState(0, subSlot(slot, 'state-ref:render'));
|
|
19
|
+
const lastKnownInputStateRef = useRef(inputState, subSlot(slot, 'state-ref:input'));
|
|
20
|
+
const stateRef = useRef(inputState, subSlot(slot, 'state-ref:value')) as StateRef<T>;
|
|
21
|
+
|
|
22
|
+
if (lastKnownInputStateRef.current !== inputState) {
|
|
23
|
+
lastKnownInputStateRef.current = inputState;
|
|
24
|
+
stateRef.current = inputState;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const setStateAndRerender = useCallback(
|
|
28
|
+
(newState: StateUpdate<T>) => {
|
|
29
|
+
const value =
|
|
30
|
+
typeof newState === 'function'
|
|
31
|
+
? (newState as (previous: T) => T)(stateRef.current)
|
|
32
|
+
: newState;
|
|
33
|
+
stateRef.current = value;
|
|
34
|
+
rerender((count) => count + 1);
|
|
35
|
+
},
|
|
36
|
+
[],
|
|
37
|
+
subSlot(slot, 'state-ref:set'),
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
return [stateRef, setStateAndRerender];
|
|
41
|
+
}
|