@livestore/react 0.3.0-dev.9 → 0.3.1-dev.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/dist/.tsbuildinfo +1 -1
- package/dist/LiveStoreContext.d.ts +10 -4
- package/dist/LiveStoreContext.d.ts.map +1 -1
- package/dist/LiveStoreContext.js +1 -11
- package/dist/LiveStoreContext.js.map +1 -1
- package/dist/LiveStoreProvider.d.ts +29 -12
- package/dist/LiveStoreProvider.d.ts.map +1 -1
- package/dist/LiveStoreProvider.js +84 -55
- package/dist/LiveStoreProvider.js.map +1 -1
- package/dist/LiveStoreProvider.test.js +80 -29
- package/dist/LiveStoreProvider.test.js.map +1 -1
- package/dist/__tests__/fixture.d.ts +122 -556
- package/dist/__tests__/fixture.d.ts.map +1 -1
- package/dist/__tests__/fixture.js +71 -30
- package/dist/__tests__/fixture.js.map +1 -1
- package/dist/experimental/components/LiveList.d.ts +2 -2
- package/dist/experimental/components/LiveList.d.ts.map +1 -1
- package/dist/experimental/components/LiveList.js +10 -6
- package/dist/experimental/components/LiveList.js.map +1 -1
- package/dist/mod.d.ts +4 -5
- package/dist/mod.d.ts.map +1 -1
- package/dist/mod.js +4 -5
- package/dist/mod.js.map +1 -1
- package/dist/useClientDocument.d.ts +61 -0
- package/dist/useClientDocument.d.ts.map +1 -0
- package/dist/useClientDocument.js +79 -0
- package/dist/useClientDocument.js.map +1 -0
- package/dist/useClientDocument.test.d.ts +2 -0
- package/dist/useClientDocument.test.d.ts.map +1 -0
- package/dist/useClientDocument.test.js +175 -0
- package/dist/useClientDocument.test.js.map +1 -0
- package/dist/useQuery.d.ts +25 -3
- package/dist/useQuery.d.ts.map +1 -1
- package/dist/useQuery.js +67 -47
- package/dist/useQuery.js.map +1 -1
- package/dist/useQuery.test.d.ts +1 -1
- package/dist/useQuery.test.d.ts.map +1 -1
- package/dist/useQuery.test.js +86 -24
- package/dist/useQuery.test.js.map +1 -1
- package/dist/useRcResource.d.ts +76 -0
- package/dist/useRcResource.d.ts.map +1 -0
- package/dist/useRcResource.js +152 -0
- package/dist/useRcResource.js.map +1 -0
- package/dist/useRcResource.test.d.ts +2 -0
- package/dist/useRcResource.test.d.ts.map +1 -0
- package/dist/useRcResource.test.js +122 -0
- package/dist/useRcResource.test.js.map +1 -0
- package/dist/useStore.d.ts +9 -0
- package/dist/useStore.d.ts.map +1 -0
- package/dist/useStore.js +28 -0
- package/dist/useStore.js.map +1 -0
- package/dist/utils/useStateRefWithReactiveInput.d.ts.map +1 -1
- package/package.json +20 -13
- package/src/LiveStoreContext.ts +11 -16
- package/src/LiveStoreProvider.test.tsx +176 -37
- package/src/LiveStoreProvider.tsx +156 -81
- package/src/__snapshots__/useClientDocument.test.tsx.snap +613 -0
- package/src/__snapshots__/useQuery.test.tsx.snap +2011 -0
- package/src/__tests__/fixture.tsx +74 -47
- package/src/experimental/components/LiveList.tsx +10 -7
- package/src/mod.ts +5 -6
- package/src/useClientDocument.test.tsx +306 -0
- package/src/useClientDocument.ts +157 -0
- package/src/useQuery.test.tsx +182 -71
- package/src/useQuery.ts +95 -58
- package/src/useRcResource.test.tsx +167 -0
- package/src/useRcResource.ts +182 -0
- package/src/useStore.ts +36 -0
- package/dist/useAtom.d.ts +0 -5
- package/dist/useAtom.d.ts.map +0 -1
- package/dist/useAtom.js +0 -38
- package/dist/useAtom.js.map +0 -1
- package/dist/useRow.d.ts +0 -50
- package/dist/useRow.d.ts.map +0 -1
- package/dist/useRow.js +0 -93
- package/dist/useRow.js.map +0 -1
- package/dist/useRow.test.d.ts +0 -2
- package/dist/useRow.test.d.ts.map +0 -1
- package/dist/useRow.test.js +0 -202
- package/dist/useRow.test.js.map +0 -1
- package/dist/useScopedQuery.d.ts +0 -33
- package/dist/useScopedQuery.d.ts.map +0 -1
- package/dist/useScopedQuery.js +0 -87
- package/dist/useScopedQuery.js.map +0 -1
- package/dist/useScopedQuery.test.d.ts +0 -2
- package/dist/useScopedQuery.test.d.ts.map +0 -1
- package/dist/useScopedQuery.test.js +0 -60
- package/dist/useScopedQuery.test.js.map +0 -1
- package/src/__snapshots__/useRow.test.tsx.snap +0 -360
- package/src/useAtom.ts +0 -52
- package/src/useRow.test.tsx +0 -344
- package/src/useRow.ts +0 -188
- package/src/useScopedQuery.test.tsx +0 -96
- package/src/useScopedQuery.ts +0 -143
- package/tsconfig.json +0 -20
- package/vitest.config.js +0 -17
package/src/useScopedQuery.ts
DELETED
@@ -1,143 +0,0 @@
|
|
1
|
-
import type { QueryInfo } from '@livestore/common'
|
2
|
-
import type { LiveQuery } from '@livestore/livestore'
|
3
|
-
import * as otel from '@opentelemetry/api'
|
4
|
-
import React from 'react'
|
5
|
-
|
6
|
-
import { useStore } from './LiveStoreContext.js'
|
7
|
-
import { useQueryRef } from './useQuery.js'
|
8
|
-
|
9
|
-
// NOTE Given `useMemo` will be called multiple times (e.g. when using React Strict mode or Fast Refresh),
|
10
|
-
// we are using this cache to avoid starting multiple queries/spans for the same component.
|
11
|
-
// This is somewhat against some recommended React best practices, but it should be fine in our case below.
|
12
|
-
// Please definitely open an issue if you see or run into any problems with this approach!
|
13
|
-
const cache = new Map<
|
14
|
-
string,
|
15
|
-
| {
|
16
|
-
_tag: 'active'
|
17
|
-
rc: number
|
18
|
-
query$: LiveQuery<any, any>
|
19
|
-
span: otel.Span
|
20
|
-
otelContext: otel.Context
|
21
|
-
}
|
22
|
-
| {
|
23
|
-
_tag: 'destroyed'
|
24
|
-
}
|
25
|
-
>()
|
26
|
-
|
27
|
-
export type DepKey = string | number | ReadonlyArray<string | number>
|
28
|
-
|
29
|
-
/**
|
30
|
-
* Creates a query, subscribes and destroys it when the component unmounts.
|
31
|
-
*
|
32
|
-
* The `key` is used to determine whether the a new query should be created or if the existing one should be reused.
|
33
|
-
* This hook should be used instead of `useQuery` when the query should be dynamically created based on some props.
|
34
|
-
* Otherwise when using `useQuery` the query will be leaked (i.e. never destroyed) when the component re-renders/unmounts.
|
35
|
-
*
|
36
|
-
* Example:
|
37
|
-
* ```tsx
|
38
|
-
* const issue = useScopedQuery(() => queryDb(tables.issues.query.where('id', issueId).first()), ['issue-details', issueId])
|
39
|
-
* ```
|
40
|
-
*
|
41
|
-
* Important: On Expo/React Native please make sure the key contains a globally unique identifier, otherwise the query might get reused unintentionally.
|
42
|
-
* Example: `['issue-details', issueId]`
|
43
|
-
* See this issue to track progress: https://github.com/livestorejs/livestore/issues/231
|
44
|
-
*/
|
45
|
-
export const useScopedQuery = <TResult>(makeQuery: () => LiveQuery<TResult>, key: DepKey): TResult =>
|
46
|
-
useScopedQueryRef(makeQuery, key).current
|
47
|
-
|
48
|
-
export const useScopedQueryRef = <TResult>(
|
49
|
-
makeQuery: () => LiveQuery<TResult>,
|
50
|
-
key: DepKey,
|
51
|
-
): React.RefObject<TResult> => {
|
52
|
-
const { query$ } = useMakeScopedQuery(makeQuery, key)
|
53
|
-
|
54
|
-
return useQueryRef(query$)
|
55
|
-
}
|
56
|
-
|
57
|
-
export const useMakeScopedQuery = <TResult, TQueryInfo extends QueryInfo>(
|
58
|
-
makeQuery: (otelContext: otel.Context) => LiveQuery<TResult, TQueryInfo>,
|
59
|
-
key: DepKey,
|
60
|
-
options?: {
|
61
|
-
otel?: {
|
62
|
-
spanName?: string
|
63
|
-
attributes?: otel.Attributes
|
64
|
-
}
|
65
|
-
},
|
66
|
-
): { query$: LiveQuery<TResult, TQueryInfo>; otelContext: otel.Context } => {
|
67
|
-
const { store } = useStore()
|
68
|
-
const fullKey = React.useMemo(
|
69
|
-
// NOTE We're using the `makeQuery` function body string to make sure the key is unique across the app
|
70
|
-
// TODO we should figure out whether this could cause some problems and/or if there's a better way to do this
|
71
|
-
() => (Array.isArray(key) ? key.join('-') : key) + '-' + store.reactivityGraph.id + '-' + makeQuery.toString(),
|
72
|
-
[key, makeQuery, store.reactivityGraph.id],
|
73
|
-
)
|
74
|
-
const fullKeyRef = React.useRef<string | undefined>(undefined)
|
75
|
-
|
76
|
-
const { query$, otelContext } = React.useMemo(() => {
|
77
|
-
if (fullKeyRef.current !== undefined && fullKeyRef.current !== fullKey) {
|
78
|
-
// console.debug('fullKey changed', 'prev', fullKeyRef.current.split('-')[0]!, '-> new', fullKey.split('-')[0]!)
|
79
|
-
|
80
|
-
const cachedItem = cache.get(fullKeyRef.current)
|
81
|
-
if (cachedItem !== undefined && cachedItem._tag === 'active') {
|
82
|
-
cachedItem.rc--
|
83
|
-
|
84
|
-
if (cachedItem.rc === 0) {
|
85
|
-
// console.debug('rc=0-changed', cachedItem.query$.id, cachedItem.query$.label)
|
86
|
-
cachedItem.query$.destroy()
|
87
|
-
cachedItem.span.end()
|
88
|
-
cache.set(fullKeyRef.current, { _tag: 'destroyed' })
|
89
|
-
}
|
90
|
-
}
|
91
|
-
}
|
92
|
-
|
93
|
-
const cachedItem = cache.get(fullKey)
|
94
|
-
if (cachedItem !== undefined && cachedItem._tag === 'active') {
|
95
|
-
// console.debug('rc++', cachedItem.query$.id, cachedItem.query$.label)
|
96
|
-
cachedItem.rc++
|
97
|
-
|
98
|
-
return cachedItem
|
99
|
-
}
|
100
|
-
|
101
|
-
const spanName = options?.otel?.spanName ?? `LiveStore:useScopedQuery:${key}`
|
102
|
-
|
103
|
-
const span = store.otel.tracer.startSpan(
|
104
|
-
spanName,
|
105
|
-
{ attributes: options?.otel?.attributes },
|
106
|
-
store.otel.queriesSpanContext,
|
107
|
-
)
|
108
|
-
|
109
|
-
const otelContext = otel.trace.setSpan(otel.context.active(), span)
|
110
|
-
// console.debug('useScopedQuery:startSpan', fullKey, spanName)
|
111
|
-
|
112
|
-
const query$ = makeQuery(otelContext)
|
113
|
-
|
114
|
-
cache.set(fullKey, { _tag: 'active', rc: 1, query$, span, otelContext })
|
115
|
-
|
116
|
-
return { query$, otelContext }
|
117
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
118
|
-
}, [fullKey])
|
119
|
-
|
120
|
-
fullKeyRef.current = fullKey
|
121
|
-
|
122
|
-
React.useEffect(() => {
|
123
|
-
return () => {
|
124
|
-
const fullKey = fullKeyRef.current!
|
125
|
-
const cachedItem = cache.get(fullKey)
|
126
|
-
// NOTE in case the fullKey changed then the query was already destroyed in the useMemo above
|
127
|
-
if (cachedItem === undefined || cachedItem._tag === 'destroyed') return
|
128
|
-
|
129
|
-
// console.debug('rc--', cachedItem.query$.id, cachedItem.query$.label)
|
130
|
-
|
131
|
-
cachedItem.rc--
|
132
|
-
|
133
|
-
if (cachedItem.rc === 0) {
|
134
|
-
// console.debug('rc=0', cachedItem.query$.id, cachedItem.query$.label)
|
135
|
-
cachedItem.query$.destroy()
|
136
|
-
cachedItem.span.end()
|
137
|
-
cache.delete(fullKey)
|
138
|
-
}
|
139
|
-
}
|
140
|
-
}, [])
|
141
|
-
|
142
|
-
return { query$, otelContext }
|
143
|
-
}
|
package/tsconfig.json
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
{
|
2
|
-
"extends": "../../../tsconfig.base.json",
|
3
|
-
"compilerOptions": {
|
4
|
-
"outDir": "./dist",
|
5
|
-
"rootDir": "./src",
|
6
|
-
"jsx": "react",
|
7
|
-
"skipLibCheck": true,
|
8
|
-
"resolveJsonModule": true,
|
9
|
-
// "jsx": "preserve",
|
10
|
-
"tsBuildInfoFile": "./dist/.tsbuildinfo"
|
11
|
-
},
|
12
|
-
"include": ["./src"],
|
13
|
-
"references": [
|
14
|
-
{ "path": "../db-schema" },
|
15
|
-
{ "path": "../common" },
|
16
|
-
{ "path": "../web" },
|
17
|
-
{ "path": "../livestore" },
|
18
|
-
{ "path": "../utils" }
|
19
|
-
]
|
20
|
-
}
|
package/vitest.config.js
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
import { defineConfig } from 'vite'
|
2
|
-
|
3
|
-
export default defineConfig({
|
4
|
-
test: {
|
5
|
-
// Needed for React hook tests
|
6
|
-
environment: 'jsdom',
|
7
|
-
},
|
8
|
-
esbuild: {
|
9
|
-
// TODO remove once `using` keyword supported OOTB with Vite https://github.com/vitejs/vite/issues/15464#issuecomment-1872485703
|
10
|
-
target: 'es2020',
|
11
|
-
},
|
12
|
-
resolve: {
|
13
|
-
alias: {
|
14
|
-
'@livestore/wa-sqlite/dist/wa-sqlite.mjs': '@livestore/wa-sqlite/dist/wa-sqlite.node.mjs',
|
15
|
-
},
|
16
|
-
},
|
17
|
-
})
|