@livestore/react 0.4.0-dev.20 → 0.4.0-dev.21
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/dist/.tsbuildinfo +1 -1
- package/dist/LiveStoreContext.d.ts +27 -0
- package/dist/LiveStoreContext.d.ts.map +1 -1
- package/dist/LiveStoreContext.js +18 -0
- package/dist/LiveStoreContext.js.map +1 -1
- package/dist/LiveStoreProvider.d.ts +9 -2
- package/dist/LiveStoreProvider.d.ts.map +1 -1
- package/dist/LiveStoreProvider.js +2 -1
- package/dist/LiveStoreProvider.js.map +1 -1
- package/dist/experimental/multi-store/StoreRegistry.d.ts +60 -16
- package/dist/experimental/multi-store/StoreRegistry.d.ts.map +1 -1
- package/dist/experimental/multi-store/StoreRegistry.js +125 -216
- package/dist/experimental/multi-store/StoreRegistry.js.map +1 -1
- package/dist/experimental/multi-store/StoreRegistry.test.js +224 -307
- package/dist/experimental/multi-store/StoreRegistry.test.js.map +1 -1
- package/dist/experimental/multi-store/types.d.ts +4 -23
- package/dist/experimental/multi-store/types.d.ts.map +1 -1
- package/dist/experimental/multi-store/useStore.d.ts +1 -1
- package/dist/experimental/multi-store/useStore.d.ts.map +1 -1
- package/dist/experimental/multi-store/useStore.js +5 -10
- package/dist/experimental/multi-store/useStore.js.map +1 -1
- package/dist/experimental/multi-store/useStore.test.js +95 -41
- package/dist/experimental/multi-store/useStore.test.js.map +1 -1
- package/dist/useClientDocument.d.ts +33 -0
- package/dist/useClientDocument.d.ts.map +1 -1
- package/dist/useClientDocument.js.map +1 -1
- package/dist/useStore.d.ts +51 -0
- package/dist/useStore.d.ts.map +1 -1
- package/dist/useStore.js +51 -0
- package/dist/useStore.js.map +1 -1
- package/package.json +6 -6
- package/src/LiveStoreContext.ts +27 -0
- package/src/LiveStoreProvider.tsx +9 -0
- package/src/experimental/multi-store/StoreRegistry.test.ts +236 -349
- package/src/experimental/multi-store/StoreRegistry.ts +171 -265
- package/src/experimental/multi-store/types.ts +31 -49
- package/src/experimental/multi-store/useStore.test.tsx +120 -48
- package/src/experimental/multi-store/useStore.ts +5 -13
- package/src/useClientDocument.ts +35 -0
- package/src/useStore.ts +51 -0
package/dist/useStore.js
CHANGED
|
@@ -2,6 +2,19 @@ import React from 'react';
|
|
|
2
2
|
import { LiveStoreContext } from "./LiveStoreContext.js";
|
|
3
3
|
import { useClientDocument } from "./useClientDocument.js";
|
|
4
4
|
import { useQuery } from "./useQuery.js";
|
|
5
|
+
/**
|
|
6
|
+
* Augments a Store instance with React-specific methods (`useQuery`, `useClientDocument`).
|
|
7
|
+
*
|
|
8
|
+
* This is called automatically by `useStore()` and `LiveStoreProvider`. You typically
|
|
9
|
+
* don't need to call it directly unless you're building custom integrations.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* // Usually not needed—useStore() does this automatically
|
|
14
|
+
* const store = withReactApi(myStore)
|
|
15
|
+
* const todos = store.useQuery(tables.todos.all())
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
5
18
|
export const withReactApi = (store) => {
|
|
6
19
|
// @ts-expect-error TODO properly implement this
|
|
7
20
|
store.useQuery = (queryable) => useQuery(queryable, { store });
|
|
@@ -9,6 +22,44 @@ export const withReactApi = (store) => {
|
|
|
9
22
|
store.useClientDocument = (table, idOrOptions, options) => useClientDocument(table, idOrOptions, options, { store });
|
|
10
23
|
return store;
|
|
11
24
|
};
|
|
25
|
+
/**
|
|
26
|
+
* Returns the current Store instance from React context, augmented with React-specific methods.
|
|
27
|
+
*
|
|
28
|
+
* Use this hook when you need direct access to the Store for operations like
|
|
29
|
+
* `store.commit()`, `store.subscribe()`, or accessing `store.sessionId`.
|
|
30
|
+
*
|
|
31
|
+
* For reactive queries, prefer `useQuery()` or `useClientDocument()` which handle
|
|
32
|
+
* subscriptions and re-renders automatically.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* const MyComponent = () => {
|
|
37
|
+
* const { store } = useStore()
|
|
38
|
+
*
|
|
39
|
+
* const handleClick = () => {
|
|
40
|
+
* store.commit(events.todoCreated({ id: nanoid(), text: 'New todo' }))
|
|
41
|
+
* }
|
|
42
|
+
*
|
|
43
|
+
* return <button onClick={handleClick}>Add Todo</button>
|
|
44
|
+
* }
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* // Access store metadata
|
|
50
|
+
* const { store } = useStore()
|
|
51
|
+
* console.log('Session ID:', store.sessionId)
|
|
52
|
+
* console.log('Client ID:', store.clientId)
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* // Use with an explicit store instance (bypasses context)
|
|
58
|
+
* const { store } = useStore({ store: myExternalStore })
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* @throws Error if called outside of `<LiveStoreProvider>` or before the store is running
|
|
62
|
+
*/
|
|
12
63
|
export const useStore = (options) => {
|
|
13
64
|
if (options?.store !== undefined) {
|
|
14
65
|
return { store: withReactApi(options.store) };
|
package/dist/useStore.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStore.js","sourceRoot":"","sources":["../src/useStore.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,MAAM,OAAO,CAAA;AAGzB,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAA;AAC1D,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAExC,MAAM,CAAC,MAAM,YAAY,GAAG,CAAkC,KAAqB,EAA6B,EAAE;IAChH,gDAAgD;IAEhD,KAAK,CAAC,QAAQ,GAAG,CAAC,SAAS,EAAE,EAAE,CAAC,QAAQ,CAAC,SAAS,EAAE,EAAE,KAAK,EAAE,CAAC,CAAA;IAC9D,gDAAg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
|
"dependencies": {
|
|
11
11
|
"@opentelemetry/api": "1.9.0",
|
|
12
|
-
"@livestore/common": "0.4.0-dev.
|
|
13
|
-
"@livestore/
|
|
14
|
-
"@livestore/
|
|
12
|
+
"@livestore/common": "0.4.0-dev.21",
|
|
13
|
+
"@livestore/utils": "0.4.0-dev.21",
|
|
14
|
+
"@livestore/livestore": "0.4.0-dev.21"
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
17
|
"@opentelemetry/sdk-trace-base": "2.0.1",
|
|
@@ -26,8 +26,8 @@
|
|
|
26
26
|
"typescript": "5.9.2",
|
|
27
27
|
"vite": "7.2.4",
|
|
28
28
|
"vitest": "3.2.4",
|
|
29
|
-
"@livestore/
|
|
30
|
-
"@livestore/
|
|
29
|
+
"@livestore/adapter-web": "0.4.0-dev.21",
|
|
30
|
+
"@livestore/utils-dev": "0.4.0-dev.21"
|
|
31
31
|
},
|
|
32
32
|
"files": [
|
|
33
33
|
"package.json",
|
package/src/LiveStoreContext.ts
CHANGED
|
@@ -4,11 +4,38 @@ import React from 'react'
|
|
|
4
4
|
import type { useClientDocument } from './useClientDocument.ts'
|
|
5
5
|
import type { useQuery } from './useQuery.ts'
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* React-specific methods added to the Store when used via React hooks.
|
|
9
|
+
*
|
|
10
|
+
* These methods are attached by `withReactApi()` and `useStore()`, allowing you
|
|
11
|
+
* to call `store.useQuery()` and `store.useClientDocument()` directly on the
|
|
12
|
+
* Store instance.
|
|
13
|
+
*/
|
|
7
14
|
export type ReactApi = {
|
|
15
|
+
/** Hook version of query subscription—re-renders component when query result changes */
|
|
8
16
|
useQuery: typeof useQuery
|
|
17
|
+
/** Hook for reading and writing client-document tables with React state semantics */
|
|
9
18
|
useClientDocument: typeof useClientDocument
|
|
10
19
|
}
|
|
11
20
|
|
|
21
|
+
/**
|
|
22
|
+
* React context for accessing the LiveStore instance.
|
|
23
|
+
*
|
|
24
|
+
* This context is provided by `<LiveStoreProvider>` and consumed by hooks like
|
|
25
|
+
* `useStore()`, `useQuery()`, and `useClientDocument()`.
|
|
26
|
+
*
|
|
27
|
+
* The context value is `undefined` until the Store has finished booting,
|
|
28
|
+
* then transitions to `{ stage: 'running', store: ... }`.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```tsx
|
|
32
|
+
* // Typically you don't use this directly—use useStore() instead
|
|
33
|
+
* const context = React.useContext(LiveStoreContext)
|
|
34
|
+
* if (context?.stage === 'running') {
|
|
35
|
+
* console.log('Store ready:', context.store.storeId)
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
12
39
|
export const LiveStoreContext = React.createContext<
|
|
13
40
|
{ stage: 'running'; store: LiveStoreContextRunning['store'] & ReactApi } | undefined
|
|
14
41
|
>(undefined)
|
|
@@ -62,6 +62,13 @@ export interface LiveStoreProviderProps<TSyncPayloadSchema extends Schema.Schema
|
|
|
62
62
|
* @default true
|
|
63
63
|
*/
|
|
64
64
|
confirmUnsavedChanges?: boolean
|
|
65
|
+
/**
|
|
66
|
+
* Advanced store parameters forwarded to `createStore`.
|
|
67
|
+
* Currently supports:
|
|
68
|
+
* - `leaderPushBatchSize`: max events pushed to the leader per write batch.
|
|
69
|
+
* - `eventQueryBatchSize`: chunk size used when the stream replays confirmed events.
|
|
70
|
+
*/
|
|
71
|
+
params?: CreateStoreOptions<LiveStoreSchema>['params']
|
|
65
72
|
/**
|
|
66
73
|
* Payload that will be passed to the sync backend when connecting
|
|
67
74
|
*
|
|
@@ -113,6 +120,7 @@ export const LiveStoreProvider = <TSyncPayloadSchema extends Schema.Schema<any>
|
|
|
113
120
|
disableDevtools,
|
|
114
121
|
signal,
|
|
115
122
|
confirmUnsavedChanges = true,
|
|
123
|
+
params,
|
|
116
124
|
syncPayload,
|
|
117
125
|
syncPayloadSchema,
|
|
118
126
|
debug,
|
|
@@ -129,6 +137,7 @@ export const LiveStoreProvider = <TSyncPayloadSchema extends Schema.Schema<any>
|
|
|
129
137
|
otelOptions,
|
|
130
138
|
boot,
|
|
131
139
|
disableDevtools,
|
|
140
|
+
params,
|
|
132
141
|
signal,
|
|
133
142
|
syncPayload,
|
|
134
143
|
syncPayloadSchema,
|