@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
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"fixture.d.ts","sourceRoot":"","sources":["../../src/__tests__/fixture.tsx"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"fixture.d.ts","sourceRoot":"","sources":["../../src/__tests__/fixture.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAsB,KAAK,EAAE,MAAM,0BAA0B,CAAA;AACpE,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAA;AAEjD,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAA;AACxD,OAAO,KAAK,KAAK,IAAI,MAAM,oBAAoB,CAAA;AAG/C,OAAO,KAAK,cAAc,MAAM,WAAW,CAAA;AAE3C,MAAM,MAAM,IAAI,GAAG;IACjB,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,MAAM,CAAA;IACZ,SAAS,EAAE,OAAO,CAAA;CACnB,CAAA;AAED,MAAM,MAAM,MAAM,GAAG,KAAK,GAAG,QAAQ,GAAG,WAAW,CAAA;AAEnD,MAAM,MAAM,QAAQ,GAAG;IACrB,WAAW,EAAE,MAAM,CAAA;IACnB,MAAM,EAAE,MAAM,CAAA;CACf,CAAA;AAwCD,eAAO,MAAM,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAelB,CAAA;AAOD,eAAO,MAAM,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAA4C,CAAA;AAG/D,eAAO,MAAM,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAAgC,CAAA;AAEnD,eAAO,MAAM,gBAAgB,GAAI,2CAI9B;IACD,UAAU,CAAC,EAAE,IAAI,CAAC,MAAM,CAAA;IACxB,WAAW,CAAC,EAAE,IAAI,CAAC,OAAO,CAAA;IAC1B,UAAU,CAAC,EAAE,OAAO,CAAA;CAChB;4BAkC6B,GAAG;;;;;;6EAWgC,CAAA"}
|
@@ -1,30 +1,69 @@
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
2
|
+
import { makeInMemoryAdapter } from '@livestore/adapter-web';
|
1
3
|
import { provideOtel } from '@livestore/common';
|
2
|
-
import {
|
3
|
-
import { createStore
|
4
|
-
import { Effect } from '@livestore/utils/effect';
|
5
|
-
import { makeInMemoryAdapter } from '@livestore/web';
|
4
|
+
import { Events, makeSchema, State } from '@livestore/common/schema';
|
5
|
+
import { createStore } from '@livestore/livestore';
|
6
|
+
import { Effect, Schema } from '@livestore/utils/effect';
|
6
7
|
import React from 'react';
|
7
8
|
import * as LiveStoreReact from '../mod.js';
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
9
|
+
const todos = State.SQLite.table({
|
10
|
+
name: 'todos',
|
11
|
+
columns: {
|
12
|
+
id: State.SQLite.text({ primaryKey: true }),
|
13
|
+
text: State.SQLite.text({ default: '', nullable: false }),
|
14
|
+
completed: State.SQLite.boolean({ default: false, nullable: false }),
|
15
|
+
},
|
16
|
+
});
|
17
|
+
const app = State.SQLite.table({
|
18
|
+
name: 'app',
|
19
|
+
columns: {
|
20
|
+
id: State.SQLite.text({ primaryKey: true, default: 'static' }),
|
21
|
+
newTodoText: State.SQLite.text({ default: '', nullable: true }),
|
22
|
+
filter: State.SQLite.text({ default: 'all', nullable: false }),
|
23
|
+
},
|
24
|
+
});
|
25
|
+
const userInfo = State.SQLite.clientDocument({
|
26
|
+
name: 'UserInfo',
|
27
|
+
schema: Schema.Struct({
|
28
|
+
username: Schema.String,
|
29
|
+
text: Schema.String,
|
30
|
+
}),
|
31
|
+
default: { value: { username: '', text: '' } },
|
32
|
+
});
|
33
|
+
const AppRouterSchema = State.SQLite.clientDocument({
|
34
|
+
name: 'AppRouter',
|
35
|
+
schema: Schema.Struct({
|
36
|
+
currentTaskId: Schema.String.pipe(Schema.NullOr),
|
37
|
+
}),
|
38
|
+
default: {
|
39
|
+
value: { currentTaskId: null },
|
40
|
+
id: 'singleton',
|
41
|
+
},
|
42
|
+
});
|
43
|
+
export const events = {
|
44
|
+
todoCreated: Events.synced({
|
45
|
+
name: 'todoCreated',
|
46
|
+
schema: Schema.Struct({ id: Schema.String, text: Schema.String, completed: Schema.Boolean }),
|
47
|
+
}),
|
48
|
+
todoUpdated: Events.synced({
|
49
|
+
name: 'todoUpdated',
|
50
|
+
schema: Schema.Struct({
|
51
|
+
id: Schema.String,
|
52
|
+
text: Schema.String.pipe(Schema.optional),
|
53
|
+
completed: Schema.Boolean.pipe(Schema.optional),
|
54
|
+
}),
|
55
|
+
}),
|
56
|
+
AppRouterSet: AppRouterSchema.set,
|
57
|
+
UserInfoSet: userInfo.set,
|
58
|
+
};
|
59
|
+
const materializers = State.SQLite.materializers(events, {
|
60
|
+
todoCreated: ({ id, text, completed }) => todos.insert({ id, text, completed }),
|
61
|
+
todoUpdated: ({ id, text, completed }) => todos.update({ completed, text }).where({ id }),
|
62
|
+
});
|
63
|
+
export const tables = { todos, app, userInfo, AppRouterSchema };
|
64
|
+
const state = State.SQLite.makeState({ tables, materializers });
|
65
|
+
export const schema = makeSchema({ state, events });
|
66
|
+
export const makeTodoMvcReact = ({ otelTracer, otelContext, strictMode, } = {}) => Effect.gen(function* () {
|
28
67
|
const makeRenderCount = () => {
|
29
68
|
let val = 0;
|
30
69
|
const inc = () => {
|
@@ -37,19 +76,21 @@ export const makeTodoMvcReact = ({ otelTracer, otelContext, useGlobalReactivityG
|
|
37
76
|
inc,
|
38
77
|
};
|
39
78
|
};
|
40
|
-
const reactivityGraph = useGlobalReactivityGraph ? globalReactivityGraph : makeReactivityGraph();
|
41
79
|
const store = yield* createStore({
|
42
80
|
schema,
|
43
81
|
storeId: 'default',
|
44
82
|
adapter: makeInMemoryAdapter(),
|
45
|
-
reactivityGraph,
|
46
83
|
debug: { instanceId: 'test' },
|
47
84
|
});
|
85
|
+
const storeWithReactApi = LiveStoreReact.withReactApi(store);
|
48
86
|
// TODO improve typing of `LiveStoreContext`
|
49
|
-
const storeContext = {
|
87
|
+
const storeContext = {
|
88
|
+
stage: 'running',
|
89
|
+
store: storeWithReactApi,
|
90
|
+
};
|
50
91
|
const MaybeStrictMode = strictMode ? React.StrictMode : React.Fragment;
|
51
|
-
const wrapper = ({ children }) => (
|
52
|
-
|
53
|
-
return { wrapper, store
|
92
|
+
const wrapper = ({ children }) => (_jsx(MaybeStrictMode, { children: _jsx(LiveStoreReact.LiveStoreContext.Provider, { value: storeContext, children: children }) }));
|
93
|
+
const renderCount = makeRenderCount();
|
94
|
+
return { wrapper, store: storeWithReactApi, renderCount };
|
54
95
|
}).pipe(provideOtel({ parentSpanContext: otelContext, otelTracer }));
|
55
96
|
//# sourceMappingURL=fixture.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"fixture.js","sourceRoot":"","sources":["../../src/__tests__/fixture.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
1
|
+
{"version":3,"file":"fixture.js","sourceRoot":"","sources":["../../src/__tests__/fixture.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAA;AAC5D,OAAO,EAAE,WAAW,EAA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,CAAC;YACD,GAAG;SACJ,CAAA;IACH,CAAC,CAAA;IAED,MAAM,KAAK,GAAe,KAAK,CAAC,CAAC,WAAW,CAAC;QAC3C,MAAM;QACN,OAAO,EAAE,SAAS;QAClB,OAAO,EAAE,mBAAmB,EAAE;QAC9B,KAAK,EAAE,EAAE,UAAU,EAAE,MAAM,EAAE;KAC9B,CAAC,CAAA;IAEF,MAAM,iBAAiB,GAAG,cAAc,CAAC,YAAY,CAAC,KAAK,CAAC,CAAA;IAE5D,4CAA4C;IAC5C,MAAM,YAAY,GAAG;QACnB,KAAK,EAAE,SAAkB;QACzB,KAAK,EAAE,iBAAiB;KACzB,CAAA;IAED,MAAM,eAAe,GAAG,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAA;IAEtE,MAAM,OAAO,GAAG,CAAC,EAAE,QAAQ,EAAO,EAAE,EAAE,CAAC,CACrC,KAAC,eAAe,cACd,KAAC,cAAc,CAAC,gBAAgB,CAAC,QAAQ,IAAC,KAAK,EAAE,YAAY,YAC1D,QAAQ,GACgC,GAC3B,CACnB,CAAA;IAED,MAAM,WAAW,GAAG,eAAe,EAAE,CAAA;IAErC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,iBAAiB,EAAE,WAAW,EAAE,CAAA;AAC3D,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE,iBAAiB,EAAE,WAAW,EAAE,UAAU,EAAE,CAAC,CAAC,CAAA"}
|
@@ -1,7 +1,7 @@
|
|
1
|
-
import type {
|
1
|
+
import type { LiveQueryDef } from '@livestore/livestore';
|
2
2
|
import React from 'react';
|
3
3
|
export type LiveListProps<TItem> = {
|
4
|
-
items$:
|
4
|
+
items$: LiveQueryDef<ReadonlyArray<TItem>>;
|
5
5
|
renderItem: (item: TItem, opts: {
|
6
6
|
index: number;
|
7
7
|
isInitialListRender: boolean;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"LiveList.d.ts","sourceRoot":"","sources":["../../../src/experimental/components/LiveList.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,
|
1
|
+
{"version":3,"file":"LiveList.d.ts","sourceRoot":"","sources":["../../../src/experimental/components/LiveList.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAA;AAExD,OAAO,KAAK,MAAM,OAAO,CAAA;AAUzB,MAAM,MAAM,aAAa,CAAC,KAAK,IAAI;IACjC,MAAM,EAAE,YAAY,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC,CAAA;IAE1C,UAAU,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,mBAAmB,EAAE,OAAO,CAAA;KAAE,KAAK,KAAK,CAAC,SAAS,CAAA;IACnG,+CAA+C;IAC/C,MAAM,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,GAAG,MAAM,CAAA;CACxD,CAAA;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,QAAQ,GAAI,KAAK,EAAG,gCAAgC,aAAa,CAAC,KAAK,CAAC,KAAG,KAAK,CAAC,SAkC7F,CAAA"}
|
@@ -1,7 +1,7 @@
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
|
1
2
|
import { computed } from '@livestore/livestore';
|
2
3
|
import React from 'react';
|
3
4
|
import { useQuery } from '../../useQuery.js';
|
4
|
-
import { useScopedQuery } from '../../useScopedQuery.js';
|
5
5
|
/**
|
6
6
|
* This component is a helper component for rendering a list of items for a LiveQuery of an array of items.
|
7
7
|
* The idea is that instead of letting React handle the rendering of the items array directly,
|
@@ -13,16 +13,20 @@ import { useScopedQuery } from '../../useScopedQuery.js';
|
|
13
13
|
export const LiveList = ({ items$, renderItem, getKey }) => {
|
14
14
|
const [hasMounted, setHasMounted] = React.useState(false);
|
15
15
|
React.useEffect(() => setHasMounted(true), []);
|
16
|
-
const
|
17
|
-
const keys = useScopedQuery(keysCb, 'fixed');
|
16
|
+
const keys = useQuery(computed((get) => get(items$).map(getKey)));
|
18
17
|
const arr = React.useMemo(() => keys.map((key) =>
|
19
18
|
// TODO figure out a way so that `item$` returns an ordered lookup map to more efficiently find the item by key
|
20
|
-
[
|
21
|
-
|
19
|
+
[
|
20
|
+
key,
|
21
|
+
computed((get) => get(items$).find((item) => getKey(item, 0) === key), {
|
22
|
+
deps: [key],
|
23
|
+
}),
|
24
|
+
]), [getKey, items$, keys]);
|
25
|
+
return (_jsx(_Fragment, { children: arr.map(([key, item$], index) => (_jsx(ItemWrapperMemo, { itemKey: key, "item$": item$, opts: { isInitialListRender: !hasMounted, index }, renderItem: renderItem }, key))) }));
|
22
26
|
};
|
23
27
|
const ItemWrapper = ({ item$, opts, renderItem, }) => {
|
24
28
|
const item = useQuery(item$);
|
25
|
-
return
|
29
|
+
return _jsx(_Fragment, { children: renderItem(item, opts) });
|
26
30
|
};
|
27
31
|
const ItemWrapperMemo = React.memo(ItemWrapper, (prev, next) => prev.itemKey === next.itemKey &&
|
28
32
|
prev.renderItem === prev.renderItem &&
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"LiveList.js","sourceRoot":"","sources":["../../../src/experimental/components/LiveList.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAA;AAC/C,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;
|
1
|
+
{"version":3,"file":"LiveList.js","sourceRoot":"","sources":["../../../src/experimental/components/LiveList.tsx"],"names":[],"mappings":";AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAA;AAC/C,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAgB5C;;;;;;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AG,CAAS,EAC3B,KAAK,EACL,IAAI,EACJ,UAAU,GAMX,EAAE,EAAE;IACH,MAAM,IAAI,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;IAE5B,OAAO,4BAAG,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,GAAI,CAAA;AACtC,CAAC,CAAA;AAED,MAAM,eAAe,GAAG,KAAK,CAAC,IAAI,CAChC,WAAW,EACX,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,CACb,IAAI,CAAC,OAAO,KAAK,IAAI,CAAC,OAAO;IAC7B,IAAI,CAAC,UAAU,KAAK,IAAI,CAAC,UAAU;IACnC,IAAI,CAAC,IAAI,CAAC,KAAK,KAAK,IAAI,CAAC,IAAI,CAAC,KAAK;IACnC,IAAI,CAAC,IAAI,CAAC,mBAAmB,KAAK,IAAI,CAAC,IAAI,CAAC,mBAAmB,CAC5C,CAAA"}
|
package/dist/mod.d.ts
CHANGED
@@ -1,8 +1,7 @@
|
|
1
|
-
export { LiveStoreContext,
|
1
|
+
export { LiveStoreContext, type ReactApi } from './LiveStoreContext.js';
|
2
|
+
export { useStore, withReactApi } from './useStore.js';
|
2
3
|
export { LiveStoreProvider } from './LiveStoreProvider.js';
|
3
|
-
export { useQuery } from './useQuery.js';
|
4
|
-
export { useScopedQuery } from './useScopedQuery.js';
|
5
4
|
export { useStackInfo } from './utils/stack-info.js';
|
6
|
-
export {
|
7
|
-
export {
|
5
|
+
export { useQuery, useQueryRef } from './useQuery.js';
|
6
|
+
export { useClientDocument, type StateSetters, type SetStateAction, type Dispatch, type UseRowResult as UseStateResult, } from './useClientDocument.js';
|
8
7
|
//# sourceMappingURL=mod.d.ts.map
|
package/dist/mod.d.ts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAA;
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,KAAK,QAAQ,EAAE,MAAM,uBAAuB,CAAA;AACvE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AACtD,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAA;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAA;AACpD,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AACrD,OAAO,EACL,iBAAiB,EACjB,KAAK,YAAY,EACjB,KAAK,cAAc,EACnB,KAAK,QAAQ,EACb,KAAK,YAAY,IAAI,cAAc,GACpC,MAAM,wBAAwB,CAAA"}
|
package/dist/mod.js
CHANGED
@@ -1,8 +1,7 @@
|
|
1
|
-
export { LiveStoreContext
|
1
|
+
export { LiveStoreContext } from './LiveStoreContext.js';
|
2
|
+
export { useStore, withReactApi } from './useStore.js';
|
2
3
|
export { LiveStoreProvider } from './LiveStoreProvider.js';
|
3
|
-
export { useQuery } from './useQuery.js';
|
4
|
-
export { useScopedQuery } from './useScopedQuery.js';
|
5
4
|
export { useStackInfo } from './utils/stack-info.js';
|
6
|
-
export {
|
7
|
-
export {
|
5
|
+
export { useQuery, useQueryRef } from './useQuery.js';
|
6
|
+
export { useClientDocument, } from './useClientDocument.js';
|
8
7
|
//# sourceMappingURL=mod.js.map
|
package/dist/mod.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"mod.js","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,
|
1
|
+
{"version":3,"file":"mod.js","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAiB,MAAM,uBAAuB,CAAA;AACvE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AACtD,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAA;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAA;AACpD,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AACrD,OAAO,EACL,iBAAiB,GAKlB,MAAM,wBAAwB,CAAA"}
|
@@ -0,0 +1,61 @@
|
|
1
|
+
import type { RowQuery } from '@livestore/common';
|
2
|
+
import { SessionIdSymbol } from '@livestore/common';
|
3
|
+
import { State } from '@livestore/common/schema';
|
4
|
+
import type { LiveQuery } from '@livestore/livestore';
|
5
|
+
export type UseRowResult<TTableDef extends State.SQLite.ClientDocumentTableDef.TraitAny> = [
|
6
|
+
row: TTableDef['Value'],
|
7
|
+
setRow: StateSetters<TTableDef>,
|
8
|
+
id: string,
|
9
|
+
query$: LiveQuery<TTableDef['Value']>
|
10
|
+
];
|
11
|
+
/**
|
12
|
+
* Similar to `React.useState` but returns a tuple of `[state, setState, id, query$]` for a given table where ...
|
13
|
+
*
|
14
|
+
* - `state` is the current value of the row (fully decoded according to the table schema)
|
15
|
+
* - `setState` is a function that can be used to update the document
|
16
|
+
* - `id` is the id of the document
|
17
|
+
* - `query$` is a `LiveQuery` that e.g. can be used to subscribe to changes to the document
|
18
|
+
*
|
19
|
+
* `useClientDocument` only works for client-document tables:
|
20
|
+
*
|
21
|
+
* ```tsx
|
22
|
+
* const MyState = State.SQLite.clientDocument({
|
23
|
+
* name: 'MyState',
|
24
|
+
* schema: Schema.Struct({
|
25
|
+
* showSidebar: Schema.Boolean,
|
26
|
+
* }),
|
27
|
+
* default: { id: SessionIdSymbol, value: { showSidebar: true } },
|
28
|
+
* })
|
29
|
+
*
|
30
|
+
* const MyComponent = () => {
|
31
|
+
* const [{ showSidebar }, setState] = useClientDocument(MyState)
|
32
|
+
* return (
|
33
|
+
* <div onClick={() => setState({ showSidebar: !showSidebar })}>
|
34
|
+
* {showSidebar ? 'Sidebar is open' : 'Sidebar is closed'}
|
35
|
+
* </div>
|
36
|
+
* )
|
37
|
+
* }
|
38
|
+
* ```
|
39
|
+
*
|
40
|
+
* If the table has a default id, `useClientDocument` can be called without an `id` argument. Otherwise, the `id` argument is required.
|
41
|
+
*/
|
42
|
+
export declare const useClientDocument: {
|
43
|
+
<TTableDef extends State.SQLite.ClientDocumentTableDef.Trait<any, any, any, {
|
44
|
+
partialSet: boolean;
|
45
|
+
default: {
|
46
|
+
id: string | SessionIdSymbol;
|
47
|
+
value: any;
|
48
|
+
};
|
49
|
+
}>>(table: TTableDef, id?: State.SQLite.ClientDocumentTableDef.DefaultIdType<TTableDef> | SessionIdSymbol, options?: Partial<RowQuery.GetOrCreateOptions<TTableDef>>): UseRowResult<TTableDef>;
|
50
|
+
<TTableDef extends State.SQLite.ClientDocumentTableDef.Trait<any, any, any, {
|
51
|
+
partialSet: boolean;
|
52
|
+
default: {
|
53
|
+
id: string | SessionIdSymbol | undefined;
|
54
|
+
value: any;
|
55
|
+
};
|
56
|
+
}>>(table: TTableDef, id: State.SQLite.ClientDocumentTableDef.DefaultIdType<TTableDef> | string | SessionIdSymbol, options?: Partial<RowQuery.GetOrCreateOptions<TTableDef>>): UseRowResult<TTableDef>;
|
57
|
+
};
|
58
|
+
export type Dispatch<A> = (action: A) => void;
|
59
|
+
export type SetStateAction<S> = Partial<S> | ((previousValue: S) => Partial<S>);
|
60
|
+
export type StateSetters<TTableDef extends State.SQLite.ClientDocumentTableDef.TraitAny> = Dispatch<SetStateAction<TTableDef['Value']>>;
|
61
|
+
//# sourceMappingURL=useClientDocument.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"useClientDocument.d.ts","sourceRoot":"","sources":["../src/useClientDocument.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,EAAE,KAAK,EAA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E,KAAK,CAAC,MAAM,CAAC,sBAAsB,CAAC,aAAa,CAAC,SAAS,CAAC,GAAG,MAAM,GAAG,eAAe,EAC3F,OAAO,CAAC,EAAE,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAC,SAAS,CAAC,CAAC,GACxD,YAAY,CAAC,SAAS,CAAC,CAAA;CAwD3B,CAAA;AAED,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,KAAK,IAAI,CAAA;AAC7C,MAAM,MAAM,cAAc,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,aAAa,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC,CAAA;AAE/E,MAAM,MAAM,YAAY,CAAC,SAAS,SAAS,KAAK,CAAC,MAAM,CAAC,sBAAsB,CAAC,QAAQ,IAAI,QAAQ,CACjG,cAAc,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CACnC,CAAA"}
|
@@ -0,0 +1,79 @@
|
|
1
|
+
import { SessionIdSymbol } from '@livestore/common';
|
2
|
+
import { State } from '@livestore/common/schema';
|
3
|
+
import { queryDb } from '@livestore/livestore';
|
4
|
+
import { shouldNeverHappen } from '@livestore/utils';
|
5
|
+
import React from 'react';
|
6
|
+
import { LiveStoreContext } from './LiveStoreContext.js';
|
7
|
+
import { useQueryRef } from './useQuery.js';
|
8
|
+
/**
|
9
|
+
* Similar to `React.useState` but returns a tuple of `[state, setState, id, query$]` for a given table where ...
|
10
|
+
*
|
11
|
+
* - `state` is the current value of the row (fully decoded according to the table schema)
|
12
|
+
* - `setState` is a function that can be used to update the document
|
13
|
+
* - `id` is the id of the document
|
14
|
+
* - `query$` is a `LiveQuery` that e.g. can be used to subscribe to changes to the document
|
15
|
+
*
|
16
|
+
* `useClientDocument` only works for client-document tables:
|
17
|
+
*
|
18
|
+
* ```tsx
|
19
|
+
* const MyState = State.SQLite.clientDocument({
|
20
|
+
* name: 'MyState',
|
21
|
+
* schema: Schema.Struct({
|
22
|
+
* showSidebar: Schema.Boolean,
|
23
|
+
* }),
|
24
|
+
* default: { id: SessionIdSymbol, value: { showSidebar: true } },
|
25
|
+
* })
|
26
|
+
*
|
27
|
+
* const MyComponent = () => {
|
28
|
+
* const [{ showSidebar }, setState] = useClientDocument(MyState)
|
29
|
+
* return (
|
30
|
+
* <div onClick={() => setState({ showSidebar: !showSidebar })}>
|
31
|
+
* {showSidebar ? 'Sidebar is open' : 'Sidebar is closed'}
|
32
|
+
* </div>
|
33
|
+
* )
|
34
|
+
* }
|
35
|
+
* ```
|
36
|
+
*
|
37
|
+
* If the table has a default id, `useClientDocument` can be called without an `id` argument. Otherwise, the `id` argument is required.
|
38
|
+
*/
|
39
|
+
export const useClientDocument = (table, idOrOptions, options_, storeArg) => {
|
40
|
+
const id = typeof idOrOptions === 'string' || idOrOptions === SessionIdSymbol
|
41
|
+
? idOrOptions
|
42
|
+
: table[State.SQLite.ClientDocumentTableDefSymbol].options.default.id;
|
43
|
+
const options = typeof idOrOptions === 'string' || idOrOptions === SessionIdSymbol ? options_ : idOrOptions;
|
44
|
+
const { default: defaultValues } = options ?? {};
|
45
|
+
React.useMemo(() => validateTableOptions(table), [table]);
|
46
|
+
const tableName = table.sqliteDef.name;
|
47
|
+
const store = storeArg?.store ??
|
48
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
49
|
+
React.useContext(LiveStoreContext)?.store ??
|
50
|
+
shouldNeverHappen(`No store provided to useClientDocument`);
|
51
|
+
// console.debug('useClientDocument', tableName, id)
|
52
|
+
const idStr = id === SessionIdSymbol ? store.clientSession.sessionId : id;
|
53
|
+
const queryDef = React.useMemo(() => queryDb(table.get(id, { default: defaultValues }), {
|
54
|
+
deps: [idStr, table.sqliteDef.name, JSON.stringify(defaultValues)],
|
55
|
+
}), [table, id, defaultValues, idStr]);
|
56
|
+
const queryRef = useQueryRef(queryDef, {
|
57
|
+
otelSpanName: `LiveStore:useClientDocument:${tableName}:${idStr}`,
|
58
|
+
store: storeArg?.store,
|
59
|
+
});
|
60
|
+
const setState = React.useMemo(() => (newValueOrFn) => {
|
61
|
+
const newValue = typeof newValueOrFn === 'function' ? newValueOrFn(queryRef.valueRef.current) : newValueOrFn;
|
62
|
+
if (queryRef.valueRef.current === newValue)
|
63
|
+
return;
|
64
|
+
store.commit(table.set(removeUndefinedValues(newValue), id));
|
65
|
+
}, [id, queryRef.valueRef, store, table]);
|
66
|
+
return [queryRef.valueRef.current, setState, idStr, queryRef.queryRcRef.value];
|
67
|
+
};
|
68
|
+
const validateTableOptions = (table) => {
|
69
|
+
if (State.SQLite.tableIsClientDocumentTable(table) === false) {
|
70
|
+
return shouldNeverHappen(`useClientDocument called on table "${table.sqliteDef.name}" which is not a client document table`);
|
71
|
+
}
|
72
|
+
};
|
73
|
+
const removeUndefinedValues = (value) => {
|
74
|
+
if (typeof value === 'object' && value !== null) {
|
75
|
+
return Object.fromEntries(Object.entries(value).filter(([_, v]) => v !== undefined));
|
76
|
+
}
|
77
|
+
return value;
|
78
|
+
};
|
79
|
+
//# sourceMappingURL=useClientDocument.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"useClientDocument.js","sourceRoot":"","sources":["../src/useClientDocument.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,EAAE,KAAK,EAAE,MAAM,0BAA0B,CAAA;AAEhD,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAA;AAC9C,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AACpD,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAS3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,CAAC,MAAM,iBAAiB,GA6B1B,CACF,KA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AAI,EAAE,CAAC;QAChD,OAAO,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,SAAS,CAAC,CAAC,CAAA;IACtF,CAAC;IAED,OAAO,KAAK,CAAA;AACd,CAAC,CAAA"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"useClientDocument.test.d.ts","sourceRoot":"","sources":["../src/useClientDocument.test.tsx"],"names":[],"mappings":""}
|
@@ -0,0 +1,175 @@
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
2
|
+
import * as LiveStore from '@livestore/livestore';
|
3
|
+
import { getSimplifiedRootSpan } from '@livestore/livestore/internal/testing-utils';
|
4
|
+
import { Effect, ReadonlyRecord, Schema } from '@livestore/utils/effect';
|
5
|
+
import { Vitest } from '@livestore/utils-dev/node-vitest';
|
6
|
+
import * as otel from '@opentelemetry/api';
|
7
|
+
import { BasicTracerProvider, InMemorySpanExporter, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
|
8
|
+
import * as ReactTesting from '@testing-library/react';
|
9
|
+
import { beforeEach, expect, it } from 'vitest';
|
10
|
+
import { events, makeTodoMvcReact, tables } from './__tests__/fixture.js';
|
11
|
+
import { __resetUseRcResourceCache } from './useRcResource.js';
|
12
|
+
// const strictMode = process.env.REACT_STRICT_MODE !== undefined
|
13
|
+
// NOTE running tests concurrently doesn't work with the default global db graph
|
14
|
+
Vitest.describe('useClientDocument', () => {
|
15
|
+
beforeEach(() => {
|
16
|
+
__resetUseRcResourceCache();
|
17
|
+
});
|
18
|
+
Vitest.scopedLive('should update the data based on component key', () => Effect.gen(function* () {
|
19
|
+
const { wrapper, store, renderCount } = yield* makeTodoMvcReact({});
|
20
|
+
const { result, rerender } = ReactTesting.renderHook((userId) => {
|
21
|
+
renderCount.inc();
|
22
|
+
const [state, setState, id] = store.useClientDocument(tables.userInfo, userId);
|
23
|
+
return { state, setState, id };
|
24
|
+
}, { wrapper, initialProps: 'u1' });
|
25
|
+
expect(result.current.id).toBe('u1');
|
26
|
+
expect(result.current.state.username).toBe('');
|
27
|
+
expect(renderCount.val).toBe(1);
|
28
|
+
expect(store.reactivityGraph.getSnapshot({ includeResults: true })).toMatchSnapshot();
|
29
|
+
store.commit(tables.userInfo.set({ username: 'username_u2' }, 'u2'));
|
30
|
+
rerender('u2');
|
31
|
+
expect(store.reactivityGraph.getSnapshot({ includeResults: true })).toMatchSnapshot();
|
32
|
+
expect(result.current.id).toBe('u2');
|
33
|
+
expect(result.current.state.username).toBe('username_u2');
|
34
|
+
expect(renderCount.val).toBe(2);
|
35
|
+
}));
|
36
|
+
// TODO add a test that makes sure React doesn't re-render when a setter is used to set the same value
|
37
|
+
Vitest.scopedLive('should update the data reactively - via setState', () => Effect.gen(function* () {
|
38
|
+
const { wrapper, store, renderCount } = yield* makeTodoMvcReact({});
|
39
|
+
const { result } = ReactTesting.renderHook((userId) => {
|
40
|
+
renderCount.inc();
|
41
|
+
const [state, setState, id] = store.useClientDocument(tables.userInfo, userId);
|
42
|
+
return { state, setState, id };
|
43
|
+
}, { wrapper, initialProps: 'u1' });
|
44
|
+
expect(result.current.id).toBe('u1');
|
45
|
+
expect(result.current.state.username).toBe('');
|
46
|
+
expect(renderCount.val).toBe(1);
|
47
|
+
ReactTesting.act(() => result.current.setState({ username: 'username_u1_hello' }));
|
48
|
+
expect(result.current.id).toBe('u1');
|
49
|
+
expect(result.current.state.username).toBe('username_u1_hello');
|
50
|
+
expect(renderCount.val).toBe(2);
|
51
|
+
}));
|
52
|
+
Vitest.scopedLive('should update the data reactively - via raw store commit', () => Effect.gen(function* () {
|
53
|
+
const { wrapper, store, renderCount } = yield* makeTodoMvcReact({});
|
54
|
+
const { result } = ReactTesting.renderHook((userId) => {
|
55
|
+
renderCount.inc();
|
56
|
+
const [state, setState, id] = store.useClientDocument(tables.userInfo, userId);
|
57
|
+
return { state, setState, id };
|
58
|
+
}, { wrapper, initialProps: 'u1' });
|
59
|
+
expect(result.current.id).toBe('u1');
|
60
|
+
expect(result.current.state.username).toBe('');
|
61
|
+
expect(renderCount.val).toBe(1);
|
62
|
+
ReactTesting.act(() => store.commit(events.UserInfoSet({ username: 'username_u1_hello' }, 'u1')));
|
63
|
+
expect(result.current.id).toBe('u1');
|
64
|
+
expect(result.current.state.username).toBe('username_u1_hello');
|
65
|
+
expect(renderCount.val).toBe(2);
|
66
|
+
}));
|
67
|
+
Vitest.scopedLive('should work for a larger app', () => Effect.gen(function* () {
|
68
|
+
const { wrapper, store, renderCount } = yield* makeTodoMvcReact({});
|
69
|
+
const allTodos$ = LiveStore.queryDb({ query: `select * from todos`, schema: Schema.Array(tables.todos.rowSchema) }, { label: 'allTodos' });
|
70
|
+
let globalSetState;
|
71
|
+
const AppRouter = () => {
|
72
|
+
renderCount.inc();
|
73
|
+
const [state, setState] = store.useClientDocument(tables.AppRouterSchema, 'singleton');
|
74
|
+
globalSetState = setState;
|
75
|
+
return (_jsxs("div", { children: [_jsx(TasksList, { setTaskId: (taskId) => setState({ currentTaskId: taskId }) }), _jsxs("div", { role: "current-id", children: ["Current Task Id: ", state.currentTaskId ?? '-'] }), state.currentTaskId ? _jsx(TaskDetails, { id: state.currentTaskId }) : _jsx("div", { children: "Click on a task to see details" })] }));
|
76
|
+
};
|
77
|
+
const TasksList = ({ setTaskId }) => {
|
78
|
+
const allTodos = store.useQuery(allTodos$);
|
79
|
+
return (_jsx("div", { children: allTodos.map((_) => (_jsx("div", { onClick: () => setTaskId(_.id), children: _.id }, _.id))) }));
|
80
|
+
};
|
81
|
+
const TaskDetails = ({ id }) => {
|
82
|
+
const todo = store.useQuery(LiveStore.queryDb(tables.todos.where({ id }).first(), { deps: id }));
|
83
|
+
return _jsx("div", { role: "content", children: JSON.stringify(todo) });
|
84
|
+
};
|
85
|
+
const renderResult = ReactTesting.render(_jsx(AppRouter, {}), { wrapper });
|
86
|
+
expect(renderCount.val).toBe(1);
|
87
|
+
ReactTesting.act(() => store.commit(LiveStore.rawSqlEvent({
|
88
|
+
sql: LiveStore.sql `INSERT INTO todos (id, text, completed) VALUES ('t1', 'buy milk', 0)`,
|
89
|
+
})));
|
90
|
+
expect(renderCount.val).toBe(1);
|
91
|
+
expect(renderResult.getByRole('current-id').innerHTML).toMatchInlineSnapshot('"Current Task Id: -"');
|
92
|
+
ReactTesting.act(() => globalSetState({ currentTaskId: 't1' }));
|
93
|
+
expect(renderCount.val).toBe(2);
|
94
|
+
expect(renderResult.getByRole('content').innerHTML).toMatchInlineSnapshot(`"{"id":"t1","text":"buy milk","completed":false}"`);
|
95
|
+
expect(renderResult.getByRole('current-id').innerHTML).toMatchInlineSnapshot('"Current Task Id: t1"');
|
96
|
+
ReactTesting.act(() => store.commit(events.todoCreated({ id: 't2', text: 'buy eggs', completed: false }), events.AppRouterSet({ currentTaskId: 't2' }), events.todoCreated({ id: 't3', text: 'buy bread', completed: false })));
|
97
|
+
expect(renderCount.val).toBe(3);
|
98
|
+
expect(renderResult.getByRole('current-id').innerHTML).toMatchInlineSnapshot('"Current Task Id: t2"');
|
99
|
+
}));
|
100
|
+
Vitest.scopedLive('should work for a useClientDocument query chained with a useTemporary query', () => Effect.gen(function* () {
|
101
|
+
const { store, wrapper, renderCount } = yield* makeTodoMvcReact({});
|
102
|
+
store.commit(events.todoCreated({ id: 't1', text: 'buy milk', completed: false }), events.todoCreated({ id: 't2', text: 'buy bread', completed: false }));
|
103
|
+
const { result, unmount, rerender } = ReactTesting.renderHook((userId) => {
|
104
|
+
renderCount.inc();
|
105
|
+
const [_row, _setRow, _id, rowState$] = store.useClientDocument(tables.userInfo, userId);
|
106
|
+
const todos = store.useQuery(LiveStore.queryDb((get) => tables.todos.where('text', 'LIKE', `%${get(rowState$).text}%`),
|
107
|
+
// TODO find a way where explicit `userId` is not needed here
|
108
|
+
// possibly by automatically understanding the `get(rowState$)` dependency
|
109
|
+
{ label: 'todosFiltered', deps: userId }));
|
110
|
+
return { todos };
|
111
|
+
}, { wrapper, initialProps: 'u1' });
|
112
|
+
ReactTesting.act(() => store.commit(events.UserInfoSet({ username: 'username_u2', text: 'milk' }, 'u2')));
|
113
|
+
expect(result.current.todos.length).toBe(2);
|
114
|
+
expect(renderCount.val).toBe(1);
|
115
|
+
rerender('u2');
|
116
|
+
expect(result.current.todos.length).toBe(1);
|
117
|
+
expect(renderCount.val).toBe(2);
|
118
|
+
unmount();
|
119
|
+
}));
|
120
|
+
Vitest.describe('otel', () => {
|
121
|
+
it.each([{ strictMode: true }, { strictMode: false }])('should update the data based on component key strictMode=%s', async ({ strictMode }) => {
|
122
|
+
const exporter = new InMemorySpanExporter();
|
123
|
+
const provider = new BasicTracerProvider({
|
124
|
+
spanProcessors: [new SimpleSpanProcessor(exporter)],
|
125
|
+
});
|
126
|
+
const otelTracer = provider.getTracer(`testing-${strictMode ? 'strict' : 'non-strict'}`);
|
127
|
+
const span = otelTracer.startSpan('test-root');
|
128
|
+
const otelContext = otel.trace.setSpan(otel.context.active(), span);
|
129
|
+
await Effect.gen(function* () {
|
130
|
+
const { wrapper, store, renderCount } = yield* makeTodoMvcReact({
|
131
|
+
otelContext,
|
132
|
+
otelTracer,
|
133
|
+
strictMode,
|
134
|
+
});
|
135
|
+
const { result, rerender, unmount } = ReactTesting.renderHook((userId) => {
|
136
|
+
renderCount.inc();
|
137
|
+
const [state, setState, id] = store.useClientDocument(tables.userInfo, userId);
|
138
|
+
return { state, setState, id };
|
139
|
+
}, { wrapper, initialProps: 'u1' });
|
140
|
+
expect(result.current.id).toBe('u1');
|
141
|
+
expect(result.current.state.username).toBe('');
|
142
|
+
expect(renderCount.val).toBe(1);
|
143
|
+
// For u2 we'll make sure that the row already exists,
|
144
|
+
// so the lazy `insert` will be skipped
|
145
|
+
ReactTesting.act(() => store.commit(events.UserInfoSet({ username: 'username_u2' }, 'u2')));
|
146
|
+
rerender('u2');
|
147
|
+
expect(result.current.id).toBe('u2');
|
148
|
+
expect(result.current.state.username).toBe('username_u2');
|
149
|
+
expect(renderCount.val).toBe(2);
|
150
|
+
unmount();
|
151
|
+
span.end();
|
152
|
+
}).pipe(Effect.scoped, Effect.tapCauseLogPretty, Effect.runPromise);
|
153
|
+
await provider.forceFlush();
|
154
|
+
const mapAttributes = (attributes) => {
|
155
|
+
return ReadonlyRecord.map(attributes, (val, key) => {
|
156
|
+
if (key === 'firstStackInfo') {
|
157
|
+
const stackInfo = JSON.parse(val);
|
158
|
+
// stackInfo.frames.shift() // Removes `renderHook.wrapper` from the stack
|
159
|
+
stackInfo.frames.forEach((_) => {
|
160
|
+
if (_.name.includes('renderHook.wrapper')) {
|
161
|
+
_.name = 'renderHook.wrapper';
|
162
|
+
}
|
163
|
+
_.filePath = '__REPLACED_FOR_SNAPSHOT__';
|
164
|
+
});
|
165
|
+
return JSON.stringify(stackInfo);
|
166
|
+
}
|
167
|
+
return val;
|
168
|
+
});
|
169
|
+
};
|
170
|
+
expect(getSimplifiedRootSpan(exporter, mapAttributes)).toMatchSnapshot();
|
171
|
+
await provider.shutdown();
|
172
|
+
});
|
173
|
+
});
|
174
|
+
});
|
175
|
+
//# sourceMappingURL=useClientDocument.test.js.map
|