@instantdb/react-common 0.22.155 → 0.22.156
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/commonjs/index.d.ts +2 -1
- package/dist/commonjs/index.d.ts.map +1 -1
- package/dist/commonjs/index.js +3 -1
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/useQuery.d.ts +2 -1
- package/dist/commonjs/useQuery.d.ts.map +1 -1
- package/dist/commonjs/useQuery.js +15 -3
- package/dist/commonjs/useQuery.js.map +1 -1
- package/dist/esm/index.d.ts +2 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +2 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/useQuery.d.ts +2 -1
- package/dist/esm/useQuery.d.ts.map +1 -1
- package/dist/esm/useQuery.js +15 -3
- package/dist/esm/useQuery.js.map +1 -1
- package/dist/standalone/index.js +636 -619
- package/dist/standalone/index.umd.cjs +11 -11
- package/package.json +3 -3
- package/src/index.ts +6 -1
- package/src/useQuery.ts +22 -2
package/dist/commonjs/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import InstantReactAbstractDatabase from './InstantReactAbstractDatabase.tsx';
|
|
2
2
|
import type { InstantReactRoom } from './InstantReactRoom.ts';
|
|
3
|
-
|
|
3
|
+
import { useQueryInternal } from './useQuery.ts';
|
|
4
|
+
export { InstantReactAbstractDatabase, type InstantReactRoom, useQueryInternal, };
|
|
4
5
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,4BAA4B,MAAM,oCAAoC,CAAC;AAC9E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,4BAA4B,MAAM,oCAAoC,CAAC;AAC9E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEjD,OAAO,EACL,4BAA4B,EAC5B,KAAK,gBAAgB,EACrB,gBAAgB,GACjB,CAAC"}
|
package/dist/commonjs/index.js
CHANGED
|
@@ -3,7 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.InstantReactAbstractDatabase = void 0;
|
|
6
|
+
exports.useQueryInternal = exports.InstantReactAbstractDatabase = void 0;
|
|
7
7
|
const InstantReactAbstractDatabase_tsx_1 = __importDefault(require("./InstantReactAbstractDatabase.js"));
|
|
8
8
|
exports.InstantReactAbstractDatabase = InstantReactAbstractDatabase_tsx_1.default;
|
|
9
|
+
const useQuery_ts_1 = require("./useQuery.js");
|
|
10
|
+
Object.defineProperty(exports, "useQueryInternal", { enumerable: true, get: function () { return useQuery_ts_1.useQueryInternal; } });
|
|
9
11
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,yGAA8E;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,yGAA8E;AAK5E,uCALK,0CAA4B,CAKL;AAH9B,+CAAiD;AAK/C,iGALO,8BAAgB,OAKP","sourcesContent":["import InstantReactAbstractDatabase from './InstantReactAbstractDatabase.tsx';\nimport type { InstantReactRoom } from './InstantReactRoom.ts';\nimport { useQueryInternal } from './useQuery.ts';\n\nexport {\n InstantReactAbstractDatabase,\n type InstantReactRoom,\n useQueryInternal,\n};\n"]}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type InstaQLOptions, InstantCoreDatabase, InstaQLLifecycleState, InstantSchemaDef, ValidQuery } from '@instantdb/core';
|
|
2
|
-
export declare function useQueryInternal<Q extends ValidQuery<Q, Schema>, Schema extends InstantSchemaDef<any, any, any>, UseDates extends boolean>(_core: InstantCoreDatabase<Schema, UseDates>, _query: null | Q, _opts?: InstaQLOptions): {
|
|
2
|
+
export declare function useQueryInternal<Q extends ValidQuery<Q, Schema>, Schema extends InstantSchemaDef<any, any, any>, UseDates extends boolean>(_core: InstantCoreDatabase<Schema, UseDates>, _query: null | Q, _opts?: InstaQLOptions, serverSnapshot?: () => InstaQLLifecycleState<Schema, Q, UseDates> | undefined): {
|
|
3
3
|
state: InstaQLLifecycleState<Schema, Q, UseDates>;
|
|
4
4
|
query: any;
|
|
5
|
+
queryHash: string;
|
|
5
6
|
};
|
|
6
7
|
//# sourceMappingURL=useQuery.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQuery.d.ts","sourceRoot":"","sources":["../../src/useQuery.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useQuery.d.ts","sourceRoot":"","sources":["../../src/useQuery.ts"],"names":[],"mappings":"AAEA,OAAO,EAGL,KAAK,cAAc,EACnB,mBAAmB,EACnB,qBAAqB,EACrB,gBAAgB,EAChB,UAAU,EACX,MAAM,iBAAiB,CAAC;AAoBzB,wBAAgB,gBAAgB,CAC9B,CAAC,SAAS,UAAU,CAAC,CAAC,EAAE,MAAM,CAAC,EAC/B,MAAM,SAAS,gBAAgB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EAC9C,QAAQ,SAAS,OAAO,EAExB,KAAK,EAAE,mBAAmB,CAAC,MAAM,EAAE,QAAQ,CAAC,EAC5C,MAAM,EAAE,IAAI,GAAG,CAAC,EAChB,KAAK,CAAC,EAAE,cAAc,EACtB,cAAc,CAAC,EAAE,MAAM,qBAAqB,CAAC,MAAM,EAAE,CAAC,EAAE,QAAQ,CAAC,GAAG,SAAS,GAC5E;IACD,KAAK,EAAE,qBAAqB,CAAC,MAAM,EAAE,CAAC,EAAE,QAAQ,CAAC,CAAC;IAClD,KAAK,EAAE,GAAG,CAAC;IACX,SAAS,EAAE,MAAM,CAAC;CACnB,CAgFA"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
'use client';
|
|
1
2
|
"use strict";
|
|
2
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
4
|
exports.useQueryInternal = useQueryInternal;
|
|
@@ -18,7 +19,7 @@ function stateForResult(result) {
|
|
|
18
19
|
...(result ? result : {}),
|
|
19
20
|
};
|
|
20
21
|
}
|
|
21
|
-
function useQueryInternal(_core, _query, _opts) {
|
|
22
|
+
function useQueryInternal(_core, _query, _opts, serverSnapshot) {
|
|
22
23
|
if (_query && _opts && 'ruleParams' in _opts) {
|
|
23
24
|
_query = { $$ruleParams: _opts['ruleParams'], ..._query };
|
|
24
25
|
}
|
|
@@ -30,6 +31,9 @@ function useQueryInternal(_core, _query, _opts) {
|
|
|
30
31
|
// If we don't use a ref, the state will always be considered different, so
|
|
31
32
|
// the component will always re-render.
|
|
32
33
|
const resultCacheRef = (0, react_1.useRef)(stateForResult(_core._reactor.getPreviousResult(query)));
|
|
34
|
+
// Cache the server snapshot to satisfy useSyncExternalStore's requirement
|
|
35
|
+
// that getServerSnapshot returns the same reference across calls.
|
|
36
|
+
const serverSnapshotCacheRef = (0, react_1.useRef)(defaultState);
|
|
33
37
|
// Similar to `resultCacheRef`, `useSyncExternalStore` will unsubscribe
|
|
34
38
|
// if `subscribe` changes, so we use `useCallback` to memoize the function.
|
|
35
39
|
const subscribe = (0, react_1.useCallback)((cb) => {
|
|
@@ -61,7 +65,15 @@ function useQueryInternal(_core, _query, _opts) {
|
|
|
61
65
|
},
|
|
62
66
|
// Build a new subscribe function if the query changes
|
|
63
67
|
[queryHash]);
|
|
64
|
-
const state = (0, react_1.useSyncExternalStore)(subscribe, () => resultCacheRef.current, () =>
|
|
65
|
-
|
|
68
|
+
const state = (0, react_1.useSyncExternalStore)(subscribe, () => resultCacheRef.current, () => {
|
|
69
|
+
if (serverSnapshot && serverSnapshotCacheRef.current === defaultState) {
|
|
70
|
+
const result = serverSnapshot();
|
|
71
|
+
if (result) {
|
|
72
|
+
serverSnapshotCacheRef.current = result;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return serverSnapshotCacheRef.current;
|
|
76
|
+
});
|
|
77
|
+
return { state, query, queryHash };
|
|
66
78
|
}
|
|
67
79
|
//# sourceMappingURL=useQuery.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQuery.js","sourceRoot":"","sources":["../../src/useQuery.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useQuery.js","sourceRoot":"","sources":["../../src/useQuery.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;AA8Bb,4CA6FC;AAzHD,0CAQyB;AACzB,iCAAkE;AAElE,MAAM,YAAY,GAAG;IACnB,SAAS,EAAE,IAAI;IACf,IAAI,EAAE,SAAS;IACf,QAAQ,EAAE,SAAS;IACnB,KAAK,EAAE,SAAS;CACR,CAAC;AAEX,SAAS,cAAc,CAAC,MAAW;IACjC,OAAO;QACL,SAAS,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC;QAC3B,IAAI,EAAE,SAAS;QACf,QAAQ,EAAE,SAAS;QACnB,KAAK,EAAE,SAAS;QAChB,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC;KAC1B,CAAC;AACJ,CAAC;AAED,SAAgB,gBAAgB,CAK9B,KAA4C,EAC5C,MAAgB,EAChB,KAAsB,EACtB,cAA6E;IAM7E,IAAI,MAAM,IAAI,KAAK,IAAI,YAAY,IAAI,KAAK,EAAE,CAAC;QAC7C,MAAM,GAAG,EAAE,YAAY,EAAE,KAAK,CAAC,YAAY,CAAC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,GAAG,MAAM;aACV,CAAC;YAEF,EAAE,EAAE,CAAC;QACP,CAAC,CAAC,CAAC;QAEH,OAAO,WAAW,CAAC;IACrB,CAAC;IACD,sDAAsD;IACtD,CAAC,SAAS,CAAC,CACZ,CAAC;IAEF,MAAM,KAAK,GAAG,IAAA,4BAAoB,EAGhC,SAAS,EACT,GAAG,EAAE,CAAC,cAAc,CAAC,OAAO,EAC5B,GAAG,EAAE;QACH,IAAI,cAAc,IAAI,sBAAsB,CAAC,OAAO,KAAK,YAAY,EAAE,CAAC;YACtE,MAAM,MAAM,GAAG,cAAc,EAAE,CAAC;YAChC,IAAI,MAAM,EAAE,CAAC;gBACX,sBAAsB,CAAC,OAAO,GAAG,MAAM,CAAC;YAC1C,CAAC;QACH,CAAC;QAED,OAAO,sBAAsB,CAAC,OAAO,CAAC;IACxC,CAAC,CACF,CAAC;IAEF,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;AACrC,CAAC","sourcesContent":["'use client';\n\nimport {\n weakHash,\n coerceQuery,\n type InstaQLOptions,\n InstantCoreDatabase,\n InstaQLLifecycleState,\n InstantSchemaDef,\n ValidQuery,\n} from '@instantdb/core';\nimport { useCallback, useRef, useSyncExternalStore } from 'react';\n\nconst defaultState = {\n isLoading: true,\n data: undefined,\n pageInfo: undefined,\n error: undefined,\n} as const;\n\nfunction stateForResult(result: any) {\n return {\n isLoading: !Boolean(result),\n data: undefined,\n pageInfo: undefined,\n error: undefined,\n ...(result ? result : {}),\n };\n}\n\nexport function useQueryInternal<\n Q extends ValidQuery<Q, Schema>,\n Schema extends InstantSchemaDef<any, any, any>,\n UseDates extends boolean,\n>(\n _core: InstantCoreDatabase<Schema, UseDates>,\n _query: null | Q,\n _opts?: InstaQLOptions,\n serverSnapshot?: () => InstaQLLifecycleState<Schema, Q, UseDates> | undefined,\n): {\n state: InstaQLLifecycleState<Schema, Q, UseDates>;\n query: any;\n queryHash: string;\n} {\n if (_query && _opts && 'ruleParams' in _opts) {\n _query = { $$ruleParams: _opts['ruleParams'], ..._query };\n }\n const query = _query ? coerceQuery(_query) : null;\n const queryHash = weakHash(query);\n\n // We use a ref to store the result of the query.\n // This is becuase `useSyncExternalStore` uses `Object.is`\n // to compare the previous and next state.\n // If we don't use a ref, the state will always be considered different, so\n // the component will always re-render.\n const resultCacheRef = useRef<InstaQLLifecycleState<Schema, Q, UseDates>>(\n stateForResult(_core._reactor.getPreviousResult(query)),\n );\n\n // Cache the server snapshot to satisfy useSyncExternalStore's requirement\n // that getServerSnapshot returns the same reference across calls.\n const serverSnapshotCacheRef = useRef<\n InstaQLLifecycleState<Schema, Q, UseDates>\n >(defaultState as any);\n\n // Similar to `resultCacheRef`, `useSyncExternalStore` will unsubscribe\n // if `subscribe` changes, so we use `useCallback` to memoize the function.\n const subscribe = useCallback(\n (cb) => {\n // Update the ref when the query changes to avoid showing stale data\n resultCacheRef.current = stateForResult(\n _core._reactor.getPreviousResult(query),\n );\n // before the subscribeQuery is connected and calls it's own callback,\n // we might have data in the store via SSR, we need to notify useSyncExternalStore that\n // the data has changed.\n cb();\n\n // Don't subscribe if query is null\n if (!query) {\n const unsubscribe = () => {};\n return unsubscribe;\n }\n\n const unsubscribe = _core.subscribeQuery<Q, UseDates>(query, (result) => {\n resultCacheRef.current = {\n isLoading: !Boolean(result),\n // @ts-expect-error: ts thinks this will always be overwritten\n data: undefined,\n // @ts-expect-error: ts thinks this will always be overwritten\n pageInfo: undefined,\n // @ts-expect-error: ts thinks this will always be overwritten\n error: undefined,\n ...result,\n };\n\n cb();\n });\n\n return unsubscribe;\n },\n // Build a new subscribe function if the query changes\n [queryHash],\n );\n\n const state = useSyncExternalStore<\n InstaQLLifecycleState<Schema, Q, UseDates>\n >(\n subscribe,\n () => resultCacheRef.current,\n () => {\n if (serverSnapshot && serverSnapshotCacheRef.current === defaultState) {\n const result = serverSnapshot();\n if (result) {\n serverSnapshotCacheRef.current = result;\n }\n }\n\n return serverSnapshotCacheRef.current;\n },\n );\n\n return { state, query, queryHash };\n}\n"]}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import InstantReactAbstractDatabase from './InstantReactAbstractDatabase.tsx';
|
|
2
2
|
import type { InstantReactRoom } from './InstantReactRoom.ts';
|
|
3
|
-
|
|
3
|
+
import { useQueryInternal } from './useQuery.ts';
|
|
4
|
+
export { InstantReactAbstractDatabase, type InstantReactRoom, useQueryInternal, };
|
|
4
5
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/esm/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,4BAA4B,MAAM,oCAAoC,CAAC;AAC9E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,4BAA4B,MAAM,oCAAoC,CAAC;AAC9E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEjD,OAAO,EACL,4BAA4B,EAC5B,KAAK,gBAAgB,EACrB,gBAAgB,GACjB,CAAC"}
|
package/dist/esm/index.js
CHANGED
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,4BAA4B,MAAM,mCAAoC,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,4BAA4B,MAAM,mCAAoC,CAAC;AAE9E,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEjD,OAAO,EACL,4BAA4B,EAE5B,gBAAgB,GACjB,CAAC","sourcesContent":["import InstantReactAbstractDatabase from './InstantReactAbstractDatabase.tsx';\nimport type { InstantReactRoom } from './InstantReactRoom.ts';\nimport { useQueryInternal } from './useQuery.ts';\n\nexport {\n InstantReactAbstractDatabase,\n type InstantReactRoom,\n useQueryInternal,\n};\n"]}
|
package/dist/esm/useQuery.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type InstaQLOptions, InstantCoreDatabase, InstaQLLifecycleState, InstantSchemaDef, ValidQuery } from '@instantdb/core';
|
|
2
|
-
export declare function useQueryInternal<Q extends ValidQuery<Q, Schema>, Schema extends InstantSchemaDef<any, any, any>, UseDates extends boolean>(_core: InstantCoreDatabase<Schema, UseDates>, _query: null | Q, _opts?: InstaQLOptions): {
|
|
2
|
+
export declare function useQueryInternal<Q extends ValidQuery<Q, Schema>, Schema extends InstantSchemaDef<any, any, any>, UseDates extends boolean>(_core: InstantCoreDatabase<Schema, UseDates>, _query: null | Q, _opts?: InstaQLOptions, serverSnapshot?: () => InstaQLLifecycleState<Schema, Q, UseDates> | undefined): {
|
|
3
3
|
state: InstaQLLifecycleState<Schema, Q, UseDates>;
|
|
4
4
|
query: any;
|
|
5
|
+
queryHash: string;
|
|
5
6
|
};
|
|
6
7
|
//# sourceMappingURL=useQuery.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQuery.d.ts","sourceRoot":"","sources":["../../src/useQuery.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useQuery.d.ts","sourceRoot":"","sources":["../../src/useQuery.ts"],"names":[],"mappings":"AAEA,OAAO,EAGL,KAAK,cAAc,EACnB,mBAAmB,EACnB,qBAAqB,EACrB,gBAAgB,EAChB,UAAU,EACX,MAAM,iBAAiB,CAAC;AAoBzB,wBAAgB,gBAAgB,CAC9B,CAAC,SAAS,UAAU,CAAC,CAAC,EAAE,MAAM,CAAC,EAC/B,MAAM,SAAS,gBAAgB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EAC9C,QAAQ,SAAS,OAAO,EAExB,KAAK,EAAE,mBAAmB,CAAC,MAAM,EAAE,QAAQ,CAAC,EAC5C,MAAM,EAAE,IAAI,GAAG,CAAC,EAChB,KAAK,CAAC,EAAE,cAAc,EACtB,cAAc,CAAC,EAAE,MAAM,qBAAqB,CAAC,MAAM,EAAE,CAAC,EAAE,QAAQ,CAAC,GAAG,SAAS,GAC5E;IACD,KAAK,EAAE,qBAAqB,CAAC,MAAM,EAAE,CAAC,EAAE,QAAQ,CAAC,CAAC;IAClD,KAAK,EAAE,GAAG,CAAC;IACX,SAAS,EAAE,MAAM,CAAC;CACnB,CAgFA"}
|
package/dist/esm/useQuery.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
'use client';
|
|
1
2
|
import { weakHash, coerceQuery, } from '@instantdb/core';
|
|
2
3
|
import { useCallback, useRef, useSyncExternalStore } from 'react';
|
|
3
4
|
const defaultState = {
|
|
@@ -15,7 +16,7 @@ function stateForResult(result) {
|
|
|
15
16
|
...(result ? result : {}),
|
|
16
17
|
};
|
|
17
18
|
}
|
|
18
|
-
export function useQueryInternal(_core, _query, _opts) {
|
|
19
|
+
export function useQueryInternal(_core, _query, _opts, serverSnapshot) {
|
|
19
20
|
if (_query && _opts && 'ruleParams' in _opts) {
|
|
20
21
|
_query = { $$ruleParams: _opts['ruleParams'], ..._query };
|
|
21
22
|
}
|
|
@@ -27,6 +28,9 @@ export function useQueryInternal(_core, _query, _opts) {
|
|
|
27
28
|
// If we don't use a ref, the state will always be considered different, so
|
|
28
29
|
// the component will always re-render.
|
|
29
30
|
const resultCacheRef = useRef(stateForResult(_core._reactor.getPreviousResult(query)));
|
|
31
|
+
// Cache the server snapshot to satisfy useSyncExternalStore's requirement
|
|
32
|
+
// that getServerSnapshot returns the same reference across calls.
|
|
33
|
+
const serverSnapshotCacheRef = useRef(defaultState);
|
|
30
34
|
// Similar to `resultCacheRef`, `useSyncExternalStore` will unsubscribe
|
|
31
35
|
// if `subscribe` changes, so we use `useCallback` to memoize the function.
|
|
32
36
|
const subscribe = useCallback((cb) => {
|
|
@@ -58,7 +62,15 @@ export function useQueryInternal(_core, _query, _opts) {
|
|
|
58
62
|
},
|
|
59
63
|
// Build a new subscribe function if the query changes
|
|
60
64
|
[queryHash]);
|
|
61
|
-
const state = useSyncExternalStore(subscribe, () => resultCacheRef.current, () =>
|
|
62
|
-
|
|
65
|
+
const state = useSyncExternalStore(subscribe, () => resultCacheRef.current, () => {
|
|
66
|
+
if (serverSnapshot && serverSnapshotCacheRef.current === defaultState) {
|
|
67
|
+
const result = serverSnapshot();
|
|
68
|
+
if (result) {
|
|
69
|
+
serverSnapshotCacheRef.current = result;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return serverSnapshotCacheRef.current;
|
|
73
|
+
});
|
|
74
|
+
return { state, query, queryHash };
|
|
63
75
|
}
|
|
64
76
|
//# sourceMappingURL=useQuery.js.map
|
package/dist/esm/useQuery.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQuery.js","sourceRoot":"","sources":["../../src/useQuery.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,WAAW,GAMZ,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AAElE,MAAM,YAAY,GAAG;IACnB,SAAS,EAAE,IAAI;IACf,IAAI,EAAE,SAAS;IACf,QAAQ,EAAE,SAAS;IACnB,KAAK,EAAE,SAAS;CACR,CAAC;AAEX,SAAS,cAAc,CAAC,MAAW;IACjC,OAAO;QACL,SAAS,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC;QAC3B,IAAI,EAAE,SAAS;QACf,QAAQ,EAAE,SAAS;QACnB,KAAK,EAAE,SAAS;QAChB,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,E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CAAC;YACtE,MAAM,MAAM,GAAG,cAAc,EAAE,CAAC;YAChC,IAAI,MAAM,EAAE,CAAC;gBACX,sBAAsB,CAAC,OAAO,GAAG,MAAM,CAAC;YAC1C,CAAC;QACH,CAAC;QAED,OAAO,sBAAsB,CAAC,OAAO,CAAC;IACxC,CAAC,CACF,CAAC;IAEF,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;AACrC,CAAC","sourcesContent":["'use client';\n\nimport {\n weakHash,\n coerceQuery,\n type InstaQLOptions,\n InstantCoreDatabase,\n InstaQLLifecycleState,\n InstantSchemaDef,\n ValidQuery,\n} from '@instantdb/core';\nimport { useCallback, useRef, useSyncExternalStore } from 'react';\n\nconst defaultState = {\n isLoading: true,\n data: undefined,\n pageInfo: undefined,\n error: undefined,\n} as const;\n\nfunction stateForResult(result: any) {\n return {\n isLoading: !Boolean(result),\n data: undefined,\n pageInfo: undefined,\n error: undefined,\n ...(result ? result : {}),\n };\n}\n\nexport function useQueryInternal<\n Q extends ValidQuery<Q, Schema>,\n Schema extends InstantSchemaDef<any, any, any>,\n UseDates extends boolean,\n>(\n _core: InstantCoreDatabase<Schema, UseDates>,\n _query: null | Q,\n _opts?: InstaQLOptions,\n serverSnapshot?: () => InstaQLLifecycleState<Schema, Q, UseDates> | undefined,\n): {\n state: InstaQLLifecycleState<Schema, Q, UseDates>;\n query: any;\n queryHash: string;\n} {\n if (_query && _opts && 'ruleParams' in _opts) {\n _query = { $$ruleParams: _opts['ruleParams'], ..._query };\n }\n const query = _query ? coerceQuery(_query) : null;\n const queryHash = weakHash(query);\n\n // We use a ref to store the result of the query.\n // This is becuase `useSyncExternalStore` uses `Object.is`\n // to compare the previous and next state.\n // If we don't use a ref, the state will always be considered different, so\n // the component will always re-render.\n const resultCacheRef = useRef<InstaQLLifecycleState<Schema, Q, UseDates>>(\n stateForResult(_core._reactor.getPreviousResult(query)),\n );\n\n // Cache the server snapshot to satisfy useSyncExternalStore's requirement\n // that getServerSnapshot returns the same reference across calls.\n const serverSnapshotCacheRef = useRef<\n InstaQLLifecycleState<Schema, Q, UseDates>\n >(defaultState as any);\n\n // Similar to `resultCacheRef`, `useSyncExternalStore` will unsubscribe\n // if `subscribe` changes, so we use `useCallback` to memoize the function.\n const subscribe = useCallback(\n (cb) => {\n // Update the ref when the query changes to avoid showing stale data\n resultCacheRef.current = stateForResult(\n _core._reactor.getPreviousResult(query),\n );\n // before the subscribeQuery is connected and calls it's own callback,\n // we might have data in the store via SSR, we need to notify useSyncExternalStore that\n // the data has changed.\n cb();\n\n // Don't subscribe if query is null\n if (!query) {\n const unsubscribe = () => {};\n return unsubscribe;\n }\n\n const unsubscribe = _core.subscribeQuery<Q, UseDates>(query, (result) => {\n resultCacheRef.current = {\n isLoading: !Boolean(result),\n // @ts-expect-error: ts thinks this will always be overwritten\n data: undefined,\n // @ts-expect-error: ts thinks this will always be overwritten\n pageInfo: undefined,\n // @ts-expect-error: ts thinks this will always be overwritten\n error: undefined,\n ...result,\n };\n\n cb();\n });\n\n return unsubscribe;\n },\n // Build a new subscribe function if the query changes\n [queryHash],\n );\n\n const state = useSyncExternalStore<\n InstaQLLifecycleState<Schema, Q, UseDates>\n >(\n subscribe,\n () => resultCacheRef.current,\n () => {\n if (serverSnapshot && serverSnapshotCacheRef.current === defaultState) {\n const result = serverSnapshot();\n if (result) {\n serverSnapshotCacheRef.current = result;\n }\n }\n\n return serverSnapshotCacheRef.current;\n },\n );\n\n return { state, query, queryHash };\n}\n"]}
|