@livestore/react 0.0.0-snapshot-98f3a092e4872b301f45a122e106e42c3b4ffdcd → 0.0.0-snapshot-d013d634d1c9917eaec350011bcdb6282824f1f2
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/LiveStoreProvider.d.ts +4 -4
- package/dist/LiveStoreProvider.d.ts.map +1 -1
- package/dist/LiveStoreProvider.js.map +1 -1
- package/dist/LiveStoreProvider.test.js +2 -2
- package/dist/LiveStoreProvider.test.js.map +1 -1
- package/dist/__tests__/fixture.d.ts +9 -9
- package/dist/__tests__/fixture.d.ts.map +1 -1
- package/dist/__tests__/fixture.js +2 -4
- package/dist/__tests__/fixture.js.map +1 -1
- package/dist/experimental/components/LiveList.js +2 -2
- package/dist/experimental/components/LiveList.js.map +1 -1
- package/dist/mod.d.ts +1 -1
- package/dist/mod.d.ts.map +1 -1
- package/dist/mod.js +1 -1
- package/dist/mod.js.map +1 -1
- package/dist/useRow.js +2 -2
- package/dist/useRow.js.map +1 -1
- package/dist/useRow.test.js +1 -1
- package/dist/useRow.test.js.map +1 -1
- package/dist/useScopedQuery.d.ts +22 -0
- package/dist/useScopedQuery.d.ts.map +1 -0
- package/dist/useScopedQuery.js +75 -0
- package/dist/useScopedQuery.js.map +1 -0
- package/dist/useScopedQuery.test.d.ts +2 -0
- package/dist/useScopedQuery.test.d.ts.map +1 -0
- package/dist/{useTemporaryQuery.test.js → useScopedQuery.test.js} +4 -4
- package/dist/useScopedQuery.test.js.map +1 -0
- package/dist/useTemporaryQuery.d.ts +3 -3
- package/dist/useTemporaryQuery.d.ts.map +1 -1
- package/dist/useTemporaryQuery.js +5 -5
- package/dist/useTemporaryQuery.js.map +1 -1
- package/package.json +6 -6
- package/src/LiveStoreProvider.test.tsx +5 -6
- package/src/LiveStoreProvider.tsx +7 -3
- package/src/__tests__/fixture.tsx +9 -7
- package/src/experimental/components/LiveList.tsx +2 -2
- package/src/mod.ts +1 -1
- package/src/useRow.test.tsx +1 -1
- package/src/useRow.ts +2 -2
- package/src/{useTemporaryQuery.test.tsx → useScopedQuery.test.tsx} +3 -6
- package/src/useScopedQuery.ts +131 -0
- package/src/useTemporaryQuery.ts +6 -6
- package/dist/useTemporaryQuery.test.d.ts +0 -2
- package/dist/useTemporaryQuery.test.d.ts.map +0 -1
- package/dist/useTemporaryQuery.test.js.map +0 -1
|
@@ -12,12 +12,12 @@ const cache = new Map();
|
|
|
12
12
|
*
|
|
13
13
|
* The `key` is used to determine whether the a new query should be created or if the existing one should be reused.
|
|
14
14
|
*/
|
|
15
|
-
export const
|
|
16
|
-
export const
|
|
17
|
-
const { query$ } =
|
|
15
|
+
export const useScopedQuery = (makeQuery, key) => useScopedQueryRef(makeQuery, key).current;
|
|
16
|
+
export const useScopedQueryRef = (makeQuery, key) => {
|
|
17
|
+
const { query$ } = useMakeScopedQuery(makeQuery, key);
|
|
18
18
|
return useQueryRef(query$);
|
|
19
19
|
};
|
|
20
|
-
export const
|
|
20
|
+
export const useMakeScopedQuery = (makeQuery, key, options) => {
|
|
21
21
|
const { store } = useStore();
|
|
22
22
|
const fullKey = React.useMemo(
|
|
23
23
|
// NOTE We're using the `makeQuery` function body string to make sure the key is unique across the app
|
|
@@ -44,7 +44,7 @@ export const useMakeTemporaryQuery = (makeQuery, key, options) => {
|
|
|
44
44
|
cachedItem.rc++;
|
|
45
45
|
return cachedItem;
|
|
46
46
|
}
|
|
47
|
-
const spanName = options?.otel?.spanName ?? `LiveStore:
|
|
47
|
+
const spanName = options?.otel?.spanName ?? `LiveStore:useScopedQuery:${key}`;
|
|
48
48
|
const span = store.otel.tracer.startSpan(spanName, { attributes: options?.otel?.attributes }, store.otel.queriesSpanContext);
|
|
49
49
|
const otelContext = otel.trace.setSpan(otel.context.active(), span);
|
|
50
50
|
const query$ = makeQuery(otelContext);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useTemporaryQuery.js","sourceRoot":"","sources":["../src/useTemporaryQuery.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,IAAI,MAAM,oBAAoB,CAAA;AAC1C,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAA;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAE3C,0GAA0G;AAC1G,2FAA2F;AAC3F,2GAA2G;AAC3G,0FAA0F;AAC1F,MAAM,KAAK,GAAG,IAAI,GAAG,EAYlB,CAAA;AAIH;;;;GAIG;AACH,MAAM,CAAC,MAAM,
|
|
1
|
+
{"version":3,"file":"useTemporaryQuery.js","sourceRoot":"","sources":["../src/useTemporaryQuery.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,IAAI,MAAM,oBAAoB,CAAA;AAC1C,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAA;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAE3C,0GAA0G;AAC1G,2FAA2F;AAC3F,2GAA2G;AAC3G,0FAA0F;AAC1F,MAAM,KAAK,GAAG,IAAI,GAAG,EAYlB,CAAA;AAIH;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAU,SAAmC,EAAE,GAAW,EAAW,EAAE,CACnG,iBAAiB,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC,OAAO,CAAA;AAE3C,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAC/B,SAAmC,EACnC,GAAW,EACsB,EAAE;IACnC,MAAM,EAAE,MAAM,EAAE,GAAG,kBAAkB,CAAC,SAAS,EAAE,GAAG,CAAC,CAAA;IAErD,OAAO,WAAW,CAAC,MAAM,CAAC,CAAA;AAC5B,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAChC,SAAwE,EACxE,GAAW,EACX,OAKC,EACsE,EAAE;IACzE,MA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uEAAuE;YAEvE,UAAU,CAAC,EAAE,EAAE,CAAA;YAEf,IAAI,UAAU,CAAC,EAAE,KAAK,CAAC,EAAE,CAAC;gBACxB,uEAAuE;gBACvE,UAAU,CAAC,MAAM,CAAC,OAAO,EAAE,CAAA;gBAC3B,UAAU,CAAC,IAAI,CAAC,GAAG,EAAE,CAAA;gBACrB,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;YACvB,CAAC;QACH,CAAC,CAAA;IACH,CAAC,EAAE,EAAE,CAAC,CAAA;IAEN,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,CAAA;AAChC,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@livestore/react",
|
|
3
|
-
"version": "0.0.0-snapshot-
|
|
3
|
+
"version": "0.0.0-snapshot-d013d634d1c9917eaec350011bcdb6282824f1f2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -22,10 +22,10 @@
|
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"@opentelemetry/api": "^1.9.0",
|
|
25
|
-
"@livestore/common": "0.0.0-snapshot-
|
|
26
|
-
"@livestore/utils": "0.0.0-snapshot-
|
|
27
|
-
"@livestore/livestore": "0.0.0-snapshot-
|
|
28
|
-
"@livestore/db-schema": "0.0.0-snapshot-
|
|
25
|
+
"@livestore/common": "0.0.0-snapshot-d013d634d1c9917eaec350011bcdb6282824f1f2",
|
|
26
|
+
"@livestore/utils": "0.0.0-snapshot-d013d634d1c9917eaec350011bcdb6282824f1f2",
|
|
27
|
+
"@livestore/livestore": "0.0.0-snapshot-d013d634d1c9917eaec350011bcdb6282824f1f2",
|
|
28
|
+
"@livestore/db-schema": "0.0.0-snapshot-d013d634d1c9917eaec350011bcdb6282824f1f2"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@opentelemetry/sdk-trace-base": "1.27.0",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"typescript": "5.6.3",
|
|
40
40
|
"vite": "5.4.10",
|
|
41
41
|
"vitest": "^2.1.4",
|
|
42
|
-
"@livestore/web": "0.0.0-snapshot-
|
|
42
|
+
"@livestore/web": "0.0.0-snapshot-d013d634d1c9917eaec350011bcdb6282824f1f2"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
45
|
"react": "^18"
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type { BootDb } from '@livestore/common'
|
|
2
1
|
import { sql } from '@livestore/common'
|
|
3
|
-
import { querySQL } from '@livestore/livestore'
|
|
2
|
+
import { querySQL, type Store } from '@livestore/livestore'
|
|
4
3
|
import { Schema } from '@livestore/utils/effect'
|
|
5
4
|
import { makeInMemoryAdapter } from '@livestore/web'
|
|
6
5
|
import { render, screen, waitFor, waitForElementToBeRemoved } from '@testing-library/react'
|
|
@@ -30,8 +29,8 @@ describe('LiveStoreProvider', () => {
|
|
|
30
29
|
|
|
31
30
|
const Root = ({ forceUpdate }: { forceUpdate: number }) => {
|
|
32
31
|
const bootCb = React.useCallback(
|
|
33
|
-
(
|
|
34
|
-
|
|
32
|
+
(store: Store) =>
|
|
33
|
+
store.__execute(sql`INSERT OR IGNORE INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0);`),
|
|
35
34
|
[],
|
|
36
35
|
)
|
|
37
36
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
@@ -81,8 +80,8 @@ describe('LiveStoreProvider', () => {
|
|
|
81
80
|
|
|
82
81
|
const Root = ({ forceUpdate }: { forceUpdate: number }) => {
|
|
83
82
|
const bootCb = React.useCallback(
|
|
84
|
-
(
|
|
85
|
-
|
|
83
|
+
(store: Store) =>
|
|
84
|
+
store.__execute(sql`INSERT INTO todos_mising_table (id, text, completed) VALUES ('t1', 'buy milk', 0);`),
|
|
86
85
|
[],
|
|
87
86
|
)
|
|
88
87
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Adapter,
|
|
1
|
+
import type { Adapter, BootStatus, IntentionalShutdownCause } from '@livestore/common'
|
|
2
2
|
import { UnexpectedError } from '@livestore/common'
|
|
3
3
|
import type { LiveStoreSchema } from '@livestore/common/schema'
|
|
4
4
|
import type {
|
|
@@ -7,6 +7,7 @@ import type {
|
|
|
7
7
|
GraphQLOptions,
|
|
8
8
|
LiveStoreContext as StoreContext_,
|
|
9
9
|
OtelOptions,
|
|
10
|
+
Store,
|
|
10
11
|
} from '@livestore/livestore'
|
|
11
12
|
import { createStore, StoreAbort, StoreInterrupted } from '@livestore/livestore'
|
|
12
13
|
import { errorToString } from '@livestore/utils'
|
|
@@ -17,7 +18,7 @@ import React from 'react'
|
|
|
17
18
|
|
|
18
19
|
import { LiveStoreContext } from './LiveStoreContext.js'
|
|
19
20
|
|
|
20
|
-
interface LiveStoreProviderProps<GraphQLContext> {
|
|
21
|
+
interface LiveStoreProviderProps<GraphQLContext extends BaseGraphQLContext> {
|
|
21
22
|
schema: LiveStoreSchema
|
|
22
23
|
/**
|
|
23
24
|
* The `storeId` can be used to isolate multiple stores from each other.
|
|
@@ -30,7 +31,10 @@ interface LiveStoreProviderProps<GraphQLContext> {
|
|
|
30
31
|
* @default 'default'
|
|
31
32
|
*/
|
|
32
33
|
storeId?: string
|
|
33
|
-
boot?: (
|
|
34
|
+
boot?: (
|
|
35
|
+
store: Store<GraphQLContext, LiveStoreSchema>,
|
|
36
|
+
parentSpan: otel.Span,
|
|
37
|
+
) => void | Promise<void> | Effect.Effect<void, unknown, otel.Tracer>
|
|
34
38
|
graphQLOptions?: GraphQLOptions<GraphQLContext>
|
|
35
39
|
otelOptions?: OtelOptions
|
|
36
40
|
renderLoading: (status: BootStatus) => ReactElement
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { sql } from '@livestore/common'
|
|
2
1
|
import { DbSchema, makeSchema } from '@livestore/common/schema'
|
|
3
2
|
import type { LiveStoreContextRunning } from '@livestore/livestore'
|
|
4
3
|
import { createStore, globalReactivityGraph, makeReactivityGraph } from '@livestore/livestore'
|
|
@@ -32,11 +31,15 @@ export const todos = DbSchema.table(
|
|
|
32
31
|
{ deriveMutations: true, isSingleton: false },
|
|
33
32
|
)
|
|
34
33
|
|
|
35
|
-
export const app = DbSchema.table(
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
})
|
|
34
|
+
export const app = DbSchema.table(
|
|
35
|
+
'app',
|
|
36
|
+
{
|
|
37
|
+
id: DbSchema.text({ primaryKey: true, default: 'static' }),
|
|
38
|
+
newTodoText: DbSchema.text({ default: '', nullable: true }),
|
|
39
|
+
filter: DbSchema.text({ default: 'all', nullable: false }),
|
|
40
|
+
},
|
|
41
|
+
{ isSingleton: true },
|
|
42
|
+
)
|
|
40
43
|
|
|
41
44
|
export const AppComponentSchema = DbSchema.table(
|
|
42
45
|
'UserInfo',
|
|
@@ -92,7 +95,6 @@ export const makeTodoMvcReact = ({
|
|
|
92
95
|
const store = yield* createStore({
|
|
93
96
|
schema,
|
|
94
97
|
storeId: 'default',
|
|
95
|
-
boot: (db) => db.execute(sql`INSERT OR IGNORE INTO app (id, newTodoText, filter) VALUES ('static', '', 'all');`),
|
|
96
98
|
adapter: makeInMemoryAdapter(),
|
|
97
99
|
reactivityGraph,
|
|
98
100
|
otelOptions: {
|
|
@@ -3,7 +3,7 @@ import { computed } from '@livestore/livestore'
|
|
|
3
3
|
import React from 'react'
|
|
4
4
|
|
|
5
5
|
import { useQuery } from '../../useQuery.js'
|
|
6
|
-
import {
|
|
6
|
+
import { useScopedQuery } from '../../useScopedQuery.js'
|
|
7
7
|
|
|
8
8
|
/*
|
|
9
9
|
TODO:
|
|
@@ -33,7 +33,7 @@ export const LiveList = <TItem,>({ items$, renderItem, getKey }: LiveListProps<T
|
|
|
33
33
|
React.useEffect(() => setHasMounted(true), [])
|
|
34
34
|
|
|
35
35
|
const keysCb = React.useCallback(() => computed((get) => get(items$).map(getKey)), [getKey, items$])
|
|
36
|
-
const keys =
|
|
36
|
+
const keys = useScopedQuery(keysCb, 'fixed')
|
|
37
37
|
const arr = React.useMemo(
|
|
38
38
|
() =>
|
|
39
39
|
keys.map(
|
package/src/mod.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { LiveStoreContext, useStore } from './LiveStoreContext.js'
|
|
2
2
|
export { LiveStoreProvider } from './LiveStoreProvider.js'
|
|
3
3
|
export { useQuery } from './useQuery.js'
|
|
4
|
-
export {
|
|
4
|
+
export { useScopedQuery } from './useScopedQuery.js'
|
|
5
5
|
export { useStackInfo } from './utils/stack-info.js'
|
|
6
6
|
export {
|
|
7
7
|
useRow,
|
package/src/useRow.test.tsx
CHANGED
|
@@ -221,7 +221,7 @@ describe('useRow', () => {
|
|
|
221
221
|
renderCount.inc()
|
|
222
222
|
|
|
223
223
|
const [_row, _setRow, rowState$] = LiveStoreReact.useRow(AppComponentSchema, userId, { reactivityGraph })
|
|
224
|
-
const todos = LiveStoreReact.
|
|
224
|
+
const todos = LiveStoreReact.useScopedQuery(
|
|
225
225
|
() =>
|
|
226
226
|
LiveStore.querySQL(
|
|
227
227
|
(get) => LiveStore.sql`select * from todos where text like '%${get(rowState$).text}%'`,
|
package/src/useRow.ts
CHANGED
|
@@ -10,7 +10,7 @@ import React from 'react'
|
|
|
10
10
|
|
|
11
11
|
import { useStore } from './LiveStoreContext.js'
|
|
12
12
|
import { useQueryRef } from './useQuery.js'
|
|
13
|
-
import {
|
|
13
|
+
import { useMakeScopedQuery } from './useScopedQuery.js'
|
|
14
14
|
|
|
15
15
|
export type UseRowResult<TTableDef extends DbSchema.TableDef> = [
|
|
16
16
|
row: RowResult<TTableDef>,
|
|
@@ -96,7 +96,7 @@ export const useRow: {
|
|
|
96
96
|
|
|
97
97
|
const idStr = id === SessionIdSymbol ? 'session' : id
|
|
98
98
|
|
|
99
|
-
const { query$, otelContext } =
|
|
99
|
+
const { query$, otelContext } = useMakeScopedQuery(
|
|
100
100
|
(otelContext) =>
|
|
101
101
|
DbSchema.tableIsSingleton(table)
|
|
102
102
|
? (rowQuery(table, { otelContext, reactivityGraph }) as LiveQuery<RowResult<TTableDef>, QueryInfo>)
|
|
@@ -10,7 +10,7 @@ import { describe, expect, it } from 'vitest'
|
|
|
10
10
|
import { makeTodoMvcReact, tables, todos } from './__tests__/fixture.js'
|
|
11
11
|
import * as LiveStoreReact from './mod.js'
|
|
12
12
|
|
|
13
|
-
describe('
|
|
13
|
+
describe('useScopedQuery', () => {
|
|
14
14
|
it('simple', () =>
|
|
15
15
|
Effect.gen(function* () {
|
|
16
16
|
const { wrapper, store, makeRenderCount } = yield* makeTodoMvcReact()
|
|
@@ -28,7 +28,7 @@ describe('useTemporaryQuery', () => {
|
|
|
28
28
|
(id: string) => {
|
|
29
29
|
renderCount.inc()
|
|
30
30
|
|
|
31
|
-
return LiveStoreReact.
|
|
31
|
+
return LiveStoreReact.useScopedQuery(() => {
|
|
32
32
|
const query$ = querySQL(`select * from todos where id = '${id}'`, {
|
|
33
33
|
schema: Schema.Array(tables.todos.schema),
|
|
34
34
|
})
|
|
@@ -80,10 +80,7 @@ describe('useTemporaryQuery', () => {
|
|
|
80
80
|
|
|
81
81
|
const ListItem: React.FC<{ data: ReadonlyArray<number>; index: number }> = ({ data: ids, index }) => {
|
|
82
82
|
const id = ids[index]!
|
|
83
|
-
const res = LiveStoreReact.
|
|
84
|
-
() => LiveStore.computed(() => id, { label: `ListItem.${id}` }),
|
|
85
|
-
id,
|
|
86
|
-
)
|
|
83
|
+
const res = LiveStoreReact.useScopedQuery(() => LiveStore.computed(() => id, { label: `ListItem.${id}` }), id)
|
|
87
84
|
return <div role="listitem">{res}</div>
|
|
88
85
|
}
|
|
89
86
|
|
|
@@ -0,0 +1,131 @@
|
|
|
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
|
+
*/
|
|
34
|
+
export const useScopedQuery = <TResult>(makeQuery: () => LiveQuery<TResult>, key: DepKey): TResult =>
|
|
35
|
+
useScopedQueryRef(makeQuery, key).current
|
|
36
|
+
|
|
37
|
+
export const useScopedQueryRef = <TResult>(
|
|
38
|
+
makeQuery: () => LiveQuery<TResult>,
|
|
39
|
+
key: DepKey,
|
|
40
|
+
): React.MutableRefObject<TResult> => {
|
|
41
|
+
const { query$ } = useMakeScopedQuery(makeQuery, key)
|
|
42
|
+
|
|
43
|
+
return useQueryRef(query$)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const useMakeScopedQuery = <TResult, TQueryInfo extends QueryInfo>(
|
|
47
|
+
makeQuery: (otelContext: otel.Context) => LiveQuery<TResult, TQueryInfo>,
|
|
48
|
+
key: DepKey,
|
|
49
|
+
options?: {
|
|
50
|
+
otel?: {
|
|
51
|
+
spanName?: string
|
|
52
|
+
attributes?: otel.Attributes
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
): { query$: LiveQuery<TResult, TQueryInfo>; otelContext: otel.Context } => {
|
|
56
|
+
const { store } = useStore()
|
|
57
|
+
const fullKey = React.useMemo(
|
|
58
|
+
// NOTE We're using the `makeQuery` function body string to make sure the key is unique across the app
|
|
59
|
+
// TODO we should figure out whether this could cause some problems and/or if there's a better way to do this
|
|
60
|
+
() => (Array.isArray(key) ? key.join('-') : key) + '-' + store.reactivityGraph.id + '-' + makeQuery.toString(),
|
|
61
|
+
[key, makeQuery, store.reactivityGraph.id],
|
|
62
|
+
)
|
|
63
|
+
const fullKeyRef = React.useRef<string>()
|
|
64
|
+
|
|
65
|
+
const { query$, otelContext } = React.useMemo(() => {
|
|
66
|
+
if (fullKeyRef.current !== undefined && fullKeyRef.current !== fullKey) {
|
|
67
|
+
// console.debug('fullKey changed', 'prev', fullKeyRef.current.split('-')[0]!, '-> new', fullKey.split('-')[0]!)
|
|
68
|
+
|
|
69
|
+
const cachedItem = cache.get(fullKeyRef.current)
|
|
70
|
+
if (cachedItem !== undefined && cachedItem._tag === 'active') {
|
|
71
|
+
cachedItem.rc--
|
|
72
|
+
|
|
73
|
+
if (cachedItem.rc === 0) {
|
|
74
|
+
// console.debug('rc=0-changed', cachedItem.query$.id, cachedItem.query$.label)
|
|
75
|
+
cachedItem.query$.destroy()
|
|
76
|
+
cachedItem.span.end()
|
|
77
|
+
cache.set(fullKeyRef.current, { _tag: 'destroyed' })
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const cachedItem = cache.get(fullKey)
|
|
83
|
+
if (cachedItem !== undefined && cachedItem._tag === 'active') {
|
|
84
|
+
// console.debug('rc++', cachedItem.query$.id, cachedItem.query$.label)
|
|
85
|
+
cachedItem.rc++
|
|
86
|
+
|
|
87
|
+
return cachedItem
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const spanName = options?.otel?.spanName ?? `LiveStore:useScopedQuery:${key}`
|
|
91
|
+
|
|
92
|
+
const span = store.otel.tracer.startSpan(
|
|
93
|
+
spanName,
|
|
94
|
+
{ attributes: options?.otel?.attributes },
|
|
95
|
+
store.otel.queriesSpanContext,
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
const otelContext = otel.trace.setSpan(otel.context.active(), span)
|
|
99
|
+
|
|
100
|
+
const query$ = makeQuery(otelContext)
|
|
101
|
+
|
|
102
|
+
cache.set(fullKey, { _tag: 'active', rc: 1, query$, span, otelContext })
|
|
103
|
+
|
|
104
|
+
return { query$, otelContext }
|
|
105
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
106
|
+
}, [fullKey])
|
|
107
|
+
|
|
108
|
+
fullKeyRef.current = fullKey
|
|
109
|
+
|
|
110
|
+
React.useEffect(() => {
|
|
111
|
+
return () => {
|
|
112
|
+
const fullKey = fullKeyRef.current!
|
|
113
|
+
const cachedItem = cache.get(fullKey)
|
|
114
|
+
// NOTE in case the fullKey changed then the query was already destroyed in the useMemo above
|
|
115
|
+
if (cachedItem === undefined || cachedItem._tag === 'destroyed') return
|
|
116
|
+
|
|
117
|
+
// console.debug('rc--', cachedItem.query$.id, cachedItem.query$.label)
|
|
118
|
+
|
|
119
|
+
cachedItem.rc--
|
|
120
|
+
|
|
121
|
+
if (cachedItem.rc === 0) {
|
|
122
|
+
// console.debug('rc=0', cachedItem.query$.id, cachedItem.query$.label)
|
|
123
|
+
cachedItem.query$.destroy()
|
|
124
|
+
cachedItem.span.end()
|
|
125
|
+
cache.delete(fullKey)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}, [])
|
|
129
|
+
|
|
130
|
+
return { query$, otelContext }
|
|
131
|
+
}
|
package/src/useTemporaryQuery.ts
CHANGED
|
@@ -31,19 +31,19 @@ export type DepKey = string | number | ReadonlyArray<string | number>
|
|
|
31
31
|
*
|
|
32
32
|
* The `key` is used to determine whether the a new query should be created or if the existing one should be reused.
|
|
33
33
|
*/
|
|
34
|
-
export const
|
|
35
|
-
|
|
34
|
+
export const useScopedQuery = <TResult>(makeQuery: () => LiveQuery<TResult>, key: DepKey): TResult =>
|
|
35
|
+
useScopedQueryRef(makeQuery, key).current
|
|
36
36
|
|
|
37
|
-
export const
|
|
37
|
+
export const useScopedQueryRef = <TResult>(
|
|
38
38
|
makeQuery: () => LiveQuery<TResult>,
|
|
39
39
|
key: DepKey,
|
|
40
40
|
): React.MutableRefObject<TResult> => {
|
|
41
|
-
const { query$ } =
|
|
41
|
+
const { query$ } = useMakeScopedQuery(makeQuery, key)
|
|
42
42
|
|
|
43
43
|
return useQueryRef(query$)
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
export const
|
|
46
|
+
export const useMakeScopedQuery = <TResult, TQueryInfo extends QueryInfo>(
|
|
47
47
|
makeQuery: (otelContext: otel.Context) => LiveQuery<TResult, TQueryInfo>,
|
|
48
48
|
key: DepKey,
|
|
49
49
|
options?: {
|
|
@@ -87,7 +87,7 @@ export const useMakeTemporaryQuery = <TResult, TQueryInfo extends QueryInfo>(
|
|
|
87
87
|
return cachedItem
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
const spanName = options?.otel?.spanName ?? `LiveStore:
|
|
90
|
+
const spanName = options?.otel?.spanName ?? `LiveStore:useScopedQuery:${key}`
|
|
91
91
|
|
|
92
92
|
const span = store.otel.tracer.startSpan(
|
|
93
93
|
spanName,
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useTemporaryQuery.test.d.ts","sourceRoot":"","sources":["../src/useTemporaryQuery.test.tsx"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useTemporaryQuery.test.js","sourceRoot":"","sources":["../src/useTemporaryQuery.test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