@legendapp/state 3.0.0-beta.1 → 3.0.0-beta.12
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/index.d.mts +2 -0
- package/index.d.ts +2 -0
- package/index.js +9 -3
- package/index.mjs +9 -3
- package/package.json +7 -1
- package/persist-plugins/async-storage.js +17 -9
- package/persist-plugins/async-storage.mjs +17 -9
- package/react-reactive/enableReactComponents.d.mts +3 -2
- package/react-reactive/enableReactComponents.d.ts +3 -2
- package/react-reactive/enableReactComponents.js +7 -2
- package/react-reactive/enableReactComponents.mjs +7 -2
- package/react-reactive/enableReactNativeComponents.d.mts +3 -20
- package/react-reactive/enableReactNativeComponents.d.ts +3 -20
- package/react-reactive/enableReactNativeComponents.js +8 -3
- package/react-reactive/enableReactNativeComponents.mjs +8 -3
- package/react-reactive/enableReactive.js +7 -2
- package/react-reactive/enableReactive.mjs +7 -2
- package/react-reactive/enableReactive.native.js +8 -3
- package/react-reactive/enableReactive.native.mjs +8 -3
- package/react-reactive/enableReactive.web.js +8 -3
- package/react-reactive/enableReactive.web.mjs +8 -3
- package/react.d.mts +9 -7
- package/react.d.ts +9 -7
- package/sync-plugins/crud.d.mts +5 -2
- package/sync-plugins/crud.d.ts +5 -2
- package/sync-plugins/crud.js +22 -24
- package/sync-plugins/crud.mjs +22 -24
- package/sync-plugins/keel.js +1 -1
- package/sync-plugins/keel.mjs +1 -1
- package/sync-plugins/supabase.js +14 -6
- package/sync-plugins/supabase.mjs +14 -6
- package/sync-plugins/tanstack-query.d.mts +5 -5
- package/sync-plugins/tanstack-query.d.ts +5 -5
- package/sync-plugins/tanstack-query.js +17 -2
- package/sync-plugins/tanstack-query.mjs +17 -2
- package/sync-plugins/tanstack-react-query.d.mts +4 -2
- package/sync-plugins/tanstack-react-query.d.ts +4 -2
- package/sync.js +114 -91
- package/sync.mjs +114 -91
- package/trace.js +5 -6
- package/trace.mjs +5 -6
- package/types/reactive-native.d.ts +19 -0
- package/types/reactive-web.d.ts +7 -0
package/sync-plugins/crud.d.ts
CHANGED
|
@@ -5,11 +5,13 @@ type CrudAsOption = 'Map' | 'object' | 'value' | 'array';
|
|
|
5
5
|
type CrudResult<T> = T;
|
|
6
6
|
interface SyncedCrudPropsSingle<TRemote extends object, TLocal> {
|
|
7
7
|
get?: (params: SyncedGetParams<TRemote>) => Promise<CrudResult<TRemote | null>> | CrudResult<TRemote | null>;
|
|
8
|
+
list?: never | undefined;
|
|
8
9
|
initial?: InitialValue<TLocal, 'value'>;
|
|
9
10
|
as?: never | 'value';
|
|
10
11
|
}
|
|
11
12
|
interface SyncedCrudPropsMany<TRemote extends object, TLocal, TAsOption extends CrudAsOption> {
|
|
12
13
|
list?: (params: SyncedGetParams<TRemote>) => Promise<CrudResult<TRemote[] | null>> | CrudResult<TRemote[] | null>;
|
|
14
|
+
get?: never | undefined;
|
|
13
15
|
as?: TAsOption;
|
|
14
16
|
initial?: InitialValue<TLocal, TAsOption>;
|
|
15
17
|
}
|
|
@@ -45,7 +47,8 @@ type SyncedCrudReturnType<TLocal, TAsOption extends CrudAsOption> = TAsOption ex
|
|
|
45
47
|
} ? number : string, TLocal> : TAsOption extends 'object' ? Record<TLocal extends {
|
|
46
48
|
id: number;
|
|
47
49
|
} ? number : string, TLocal> : TAsOption extends 'value' ? TLocal : TLocal[];
|
|
48
|
-
declare function syncedCrud<TRemote extends object, TLocal = TRemote>(props:
|
|
49
|
-
declare function syncedCrud<TRemote extends object, TLocal = TRemote, TAsOption extends CrudAsOption = 'object'>(props:
|
|
50
|
+
declare function syncedCrud<TRemote extends object, TLocal = TRemote>(props: SyncedCrudPropsSingle<TRemote, TLocal> & SyncedCrudPropsBase<TRemote, TLocal>): SyncedCrudReturnType<TLocal, 'value'>;
|
|
51
|
+
declare function syncedCrud<TRemote extends object, TLocal = TRemote, TAsOption extends CrudAsOption = 'object'>(props: SyncedCrudPropsMany<TRemote, TLocal, TAsOption> & SyncedCrudPropsBase<TRemote, TLocal>): SyncedCrudReturnType<TLocal, Exclude<TAsOption, 'value'>>;
|
|
52
|
+
declare function syncedCrud<TRemote extends object, TLocal = TRemote, TAsOption extends CrudAsOption = 'object'>(props: (SyncedCrudPropsSingle<TRemote, TLocal> | SyncedCrudPropsMany<TRemote, TLocal, TAsOption>) & SyncedCrudPropsBase<TRemote, TLocal>): SyncedCrudReturnType<TLocal, TAsOption>;
|
|
50
53
|
|
|
51
54
|
export { type CrudAsOption, type CrudResult, type SyncedCrudOnSavedParams, type SyncedCrudPropsBase, type SyncedCrudPropsMany, type SyncedCrudPropsSingle, type SyncedCrudReturnType, type WaitForSetCrudFnParams, syncedCrud };
|
package/sync-plugins/crud.js
CHANGED
|
@@ -256,40 +256,38 @@ function syncedCrud(props) {
|
|
|
256
256
|
const saveResult = async (itemKey, input, data, isCreate) => {
|
|
257
257
|
var _a;
|
|
258
258
|
if (data) {
|
|
259
|
-
|
|
259
|
+
let saved = (transform == null ? void 0 : transform.load) ? await transform.load(data, "set") : data;
|
|
260
260
|
const isChild = itemKey !== "undefined" && asType !== "value";
|
|
261
261
|
const currentPeeked = state.getNodeValue(node);
|
|
262
262
|
const currentValue = isChild ? (_a = asType === "array" && state.isArray(currentPeeked) ? currentPeeked.find((v) => v[fieldId] === itemKey) : void 0) != null ? _a : currentPeeked[itemKey] : currentPeeked;
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
263
|
+
if (saved && !state.isNullOrUndefined(currentValue)) {
|
|
264
|
+
if (onSaved) {
|
|
265
|
+
const ret = onSaved({
|
|
266
|
+
saved,
|
|
267
|
+
input,
|
|
268
|
+
currentValue,
|
|
269
|
+
isCreate,
|
|
270
|
+
props
|
|
271
|
+
});
|
|
272
|
+
if (ret) {
|
|
273
|
+
saved = ret;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
saved = clone(saved);
|
|
277
|
+
Object.keys(saved).forEach((key) => {
|
|
274
278
|
const i = input[key];
|
|
275
279
|
const c = currentValue[key];
|
|
276
280
|
if (
|
|
277
281
|
// value is already the new value, can ignore
|
|
278
|
-
|
|
279
|
-
key !==
|
|
282
|
+
saved[key] === c || // user has changed local value
|
|
283
|
+
key !== fieldId && i !== c
|
|
280
284
|
) {
|
|
281
|
-
delete
|
|
285
|
+
delete saved[key];
|
|
282
286
|
}
|
|
283
287
|
});
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
savedOut = ret;
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
const createdAt = fieldCreatedAt ? savedOut[fieldCreatedAt] : void 0;
|
|
291
|
-
const updatedAt = fieldUpdatedAt ? savedOut[fieldUpdatedAt] : void 0;
|
|
292
|
-
const value2 = itemKey !== "undefined" && asType !== "value" ? { [itemKey]: savedOut } : savedOut;
|
|
288
|
+
const createdAt = fieldCreatedAt ? saved[fieldCreatedAt] : void 0;
|
|
289
|
+
const updatedAt = fieldUpdatedAt ? saved[fieldUpdatedAt] : void 0;
|
|
290
|
+
const value2 = itemKey !== "undefined" && asType !== "value" ? { [itemKey]: saved } : saved;
|
|
293
291
|
update({
|
|
294
292
|
value: value2,
|
|
295
293
|
lastSync: updatedAt || createdAt ? +new Date(updatedAt || createdAt) : void 0,
|
package/sync-plugins/crud.mjs
CHANGED
|
@@ -254,40 +254,38 @@ function syncedCrud(props) {
|
|
|
254
254
|
const saveResult = async (itemKey, input, data, isCreate) => {
|
|
255
255
|
var _a;
|
|
256
256
|
if (data) {
|
|
257
|
-
|
|
257
|
+
let saved = (transform == null ? void 0 : transform.load) ? await transform.load(data, "set") : data;
|
|
258
258
|
const isChild = itemKey !== "undefined" && asType !== "value";
|
|
259
259
|
const currentPeeked = getNodeValue(node);
|
|
260
260
|
const currentValue = isChild ? (_a = asType === "array" && isArray(currentPeeked) ? currentPeeked.find((v) => v[fieldId] === itemKey) : void 0) != null ? _a : currentPeeked[itemKey] : currentPeeked;
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
261
|
+
if (saved && !isNullOrUndefined(currentValue)) {
|
|
262
|
+
if (onSaved) {
|
|
263
|
+
const ret = onSaved({
|
|
264
|
+
saved,
|
|
265
|
+
input,
|
|
266
|
+
currentValue,
|
|
267
|
+
isCreate,
|
|
268
|
+
props
|
|
269
|
+
});
|
|
270
|
+
if (ret) {
|
|
271
|
+
saved = ret;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
saved = clone(saved);
|
|
275
|
+
Object.keys(saved).forEach((key) => {
|
|
272
276
|
const i = input[key];
|
|
273
277
|
const c = currentValue[key];
|
|
274
278
|
if (
|
|
275
279
|
// value is already the new value, can ignore
|
|
276
|
-
|
|
277
|
-
key !==
|
|
280
|
+
saved[key] === c || // user has changed local value
|
|
281
|
+
key !== fieldId && i !== c
|
|
278
282
|
) {
|
|
279
|
-
delete
|
|
283
|
+
delete saved[key];
|
|
280
284
|
}
|
|
281
285
|
});
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
savedOut = ret;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
const createdAt = fieldCreatedAt ? savedOut[fieldCreatedAt] : void 0;
|
|
289
|
-
const updatedAt = fieldUpdatedAt ? savedOut[fieldUpdatedAt] : void 0;
|
|
290
|
-
const value2 = itemKey !== "undefined" && asType !== "value" ? { [itemKey]: savedOut } : savedOut;
|
|
286
|
+
const createdAt = fieldCreatedAt ? saved[fieldCreatedAt] : void 0;
|
|
287
|
+
const updatedAt = fieldUpdatedAt ? saved[fieldUpdatedAt] : void 0;
|
|
288
|
+
const value2 = itemKey !== "undefined" && asType !== "value" ? { [itemKey]: saved } : saved;
|
|
291
289
|
update({
|
|
292
290
|
value: value2,
|
|
293
291
|
lastSync: updatedAt || createdAt ? +new Date(updatedAt || createdAt) : void 0,
|
package/sync-plugins/keel.js
CHANGED
|
@@ -274,6 +274,7 @@ function syncedKeel(props) {
|
|
|
274
274
|
} : subscribeParam;
|
|
275
275
|
return crud.syncedCrud({
|
|
276
276
|
...rest,
|
|
277
|
+
// Workaround for type errors
|
|
277
278
|
as: asType,
|
|
278
279
|
mode: mode || "merge",
|
|
279
280
|
list,
|
|
@@ -299,7 +300,6 @@ function syncedKeel(props) {
|
|
|
299
300
|
updatePartial: true,
|
|
300
301
|
subscribe,
|
|
301
302
|
generateId: generateKeelId,
|
|
302
|
-
// @ts-expect-error This errors because of the get/list union type
|
|
303
303
|
get
|
|
304
304
|
});
|
|
305
305
|
}
|
package/sync-plugins/keel.mjs
CHANGED
|
@@ -268,6 +268,7 @@ function syncedKeel(props) {
|
|
|
268
268
|
} : subscribeParam;
|
|
269
269
|
return syncedCrud({
|
|
270
270
|
...rest,
|
|
271
|
+
// Workaround for type errors
|
|
271
272
|
as: asType,
|
|
272
273
|
mode: mode || "merge",
|
|
273
274
|
list,
|
|
@@ -293,7 +294,6 @@ function syncedKeel(props) {
|
|
|
293
294
|
updatePartial: true,
|
|
294
295
|
subscribe,
|
|
295
296
|
generateId: generateKeelId,
|
|
296
|
-
// @ts-expect-error This errors because of the get/list union type
|
|
297
297
|
get
|
|
298
298
|
});
|
|
299
299
|
}
|
package/sync-plugins/supabase.js
CHANGED
|
@@ -36,9 +36,9 @@ function syncedSupabase(props) {
|
|
|
36
36
|
schema,
|
|
37
37
|
filter,
|
|
38
38
|
actions,
|
|
39
|
-
fieldCreatedAt
|
|
40
|
-
fieldUpdatedAt
|
|
41
|
-
fieldDeleted
|
|
39
|
+
fieldCreatedAt,
|
|
40
|
+
fieldUpdatedAt,
|
|
41
|
+
fieldDeleted,
|
|
42
42
|
realtime,
|
|
43
43
|
changesSince,
|
|
44
44
|
transform: transformParam,
|
|
@@ -54,9 +54,17 @@ function syncedSupabase(props) {
|
|
|
54
54
|
...rest
|
|
55
55
|
} = props;
|
|
56
56
|
const client = supabase;
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
57
|
+
if (process.env.NODE_ENV === "development" && changesSince === "last-sync") {
|
|
58
|
+
if (!fieldCreatedAt) {
|
|
59
|
+
console.warn("[legend-state] fieldCreatedAt is required when using last-sync mode");
|
|
60
|
+
}
|
|
61
|
+
if (!fieldUpdatedAt) {
|
|
62
|
+
console.warn("[legend-state] fieldUpdatedAt is required when using last-sync mode");
|
|
63
|
+
}
|
|
64
|
+
if (!fieldDeleted) {
|
|
65
|
+
console.warn("[legend-state] fieldDeleted is required when using last-sync mode");
|
|
66
|
+
}
|
|
67
|
+
}
|
|
60
68
|
const list = !actions || actions.includes("read") ? listParam ? wrapSupabaseFn(listParam) : async (params) => {
|
|
61
69
|
const { lastSync } = params;
|
|
62
70
|
const clientSchema = schema ? client.schema(schema) : client;
|
|
@@ -34,9 +34,9 @@ function syncedSupabase(props) {
|
|
|
34
34
|
schema,
|
|
35
35
|
filter,
|
|
36
36
|
actions,
|
|
37
|
-
fieldCreatedAt
|
|
38
|
-
fieldUpdatedAt
|
|
39
|
-
fieldDeleted
|
|
37
|
+
fieldCreatedAt,
|
|
38
|
+
fieldUpdatedAt,
|
|
39
|
+
fieldDeleted,
|
|
40
40
|
realtime,
|
|
41
41
|
changesSince,
|
|
42
42
|
transform: transformParam,
|
|
@@ -52,9 +52,17 @@ function syncedSupabase(props) {
|
|
|
52
52
|
...rest
|
|
53
53
|
} = props;
|
|
54
54
|
const client = supabase;
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
55
|
+
if (process.env.NODE_ENV === "development" && changesSince === "last-sync") {
|
|
56
|
+
if (!fieldCreatedAt) {
|
|
57
|
+
console.warn("[legend-state] fieldCreatedAt is required when using last-sync mode");
|
|
58
|
+
}
|
|
59
|
+
if (!fieldUpdatedAt) {
|
|
60
|
+
console.warn("[legend-state] fieldUpdatedAt is required when using last-sync mode");
|
|
61
|
+
}
|
|
62
|
+
if (!fieldDeleted) {
|
|
63
|
+
console.warn("[legend-state] fieldDeleted is required when using last-sync mode");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
58
66
|
const list = !actions || actions.includes("read") ? listParam ? wrapSupabaseFn(listParam) : async (params) => {
|
|
59
67
|
const { lastSync } = params;
|
|
60
68
|
const clientSchema = schema ? client.schema(schema) : client;
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { SyncedOptions } from '@legendapp/state/sync';
|
|
1
|
+
import { SyncedOptions, Synced } from '@legendapp/state/sync';
|
|
2
2
|
import { QueryKey, QueryObserverOptions, QueryClient, MutationObserverOptions, DefaultError } from '@tanstack/query-core';
|
|
3
3
|
|
|
4
|
-
interface ObservableQueryOptions<TQueryFnData, TError, TData, TQueryKey extends QueryKey> extends Omit<QueryObserverOptions<TQueryFnData, TError, TData, TQueryKey>, 'queryKey'> {
|
|
4
|
+
interface ObservableQueryOptions<TQueryFnData, TError, TData, TQueryKey extends QueryKey> extends Omit<QueryObserverOptions<TQueryFnData, TError, TData, TData, TQueryKey>, 'queryKey'> {
|
|
5
5
|
queryKey?: TQueryKey | (() => TQueryKey);
|
|
6
6
|
}
|
|
7
|
-
interface SyncedQueryParams<TQueryFnData, TError, TData, TQueryKey extends QueryKey> extends Omit<SyncedOptions<TData>, 'get' | 'set'> {
|
|
7
|
+
interface SyncedQueryParams<TQueryFnData, TError, TData, TQueryKey extends QueryKey> extends Omit<SyncedOptions<TData>, 'get' | 'set' | 'retry'> {
|
|
8
8
|
queryClient: QueryClient;
|
|
9
9
|
query: ObservableQueryOptions<TQueryFnData, TError, TData, TQueryKey>;
|
|
10
|
-
mutation?: MutationObserverOptions<
|
|
10
|
+
mutation?: MutationObserverOptions<TQueryFnData, TError, TData>;
|
|
11
11
|
}
|
|
12
|
-
declare function syncedQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(params: SyncedQueryParams<TQueryFnData, TError, TData, TQueryKey>): TData
|
|
12
|
+
declare function syncedQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(params: SyncedQueryParams<TQueryFnData, TError, TData, TQueryKey>): Synced<TData>;
|
|
13
13
|
|
|
14
14
|
export { type ObservableQueryOptions, type SyncedQueryParams, syncedQuery };
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { SyncedOptions } from '@legendapp/state/sync';
|
|
1
|
+
import { SyncedOptions, Synced } from '@legendapp/state/sync';
|
|
2
2
|
import { QueryKey, QueryObserverOptions, QueryClient, MutationObserverOptions, DefaultError } from '@tanstack/query-core';
|
|
3
3
|
|
|
4
|
-
interface ObservableQueryOptions<TQueryFnData, TError, TData, TQueryKey extends QueryKey> extends Omit<QueryObserverOptions<TQueryFnData, TError, TData, TQueryKey>, 'queryKey'> {
|
|
4
|
+
interface ObservableQueryOptions<TQueryFnData, TError, TData, TQueryKey extends QueryKey> extends Omit<QueryObserverOptions<TQueryFnData, TError, TData, TData, TQueryKey>, 'queryKey'> {
|
|
5
5
|
queryKey?: TQueryKey | (() => TQueryKey);
|
|
6
6
|
}
|
|
7
|
-
interface SyncedQueryParams<TQueryFnData, TError, TData, TQueryKey extends QueryKey> extends Omit<SyncedOptions<TData>, 'get' | 'set'> {
|
|
7
|
+
interface SyncedQueryParams<TQueryFnData, TError, TData, TQueryKey extends QueryKey> extends Omit<SyncedOptions<TData>, 'get' | 'set' | 'retry'> {
|
|
8
8
|
queryClient: QueryClient;
|
|
9
9
|
query: ObservableQueryOptions<TQueryFnData, TError, TData, TQueryKey>;
|
|
10
|
-
mutation?: MutationObserverOptions<
|
|
10
|
+
mutation?: MutationObserverOptions<TQueryFnData, TError, TData>;
|
|
11
11
|
}
|
|
12
|
-
declare function syncedQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(params: SyncedQueryParams<TQueryFnData, TError, TData, TQueryKey>): TData
|
|
12
|
+
declare function syncedQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(params: SyncedQueryParams<TQueryFnData, TError, TData, TQueryKey>): Synced<TData>;
|
|
13
13
|
|
|
14
14
|
export { type ObservableQueryOptions, type SyncedQueryParams, syncedQuery };
|
|
@@ -5,8 +5,14 @@ var sync = require('@legendapp/state/sync');
|
|
|
5
5
|
var queryCore = require('@tanstack/query-core');
|
|
6
6
|
|
|
7
7
|
// src/sync-plugins/tanstack-query.ts
|
|
8
|
+
var nextMutationKey = 0;
|
|
8
9
|
function syncedQuery(params) {
|
|
9
|
-
const { query: options, mutation: mutationOptions, queryClient, ...rest } = params;
|
|
10
|
+
const { query: options, mutation: mutationOptions, queryClient, initial: initialParam, ...rest } = params;
|
|
11
|
+
if (initialParam !== void 0) {
|
|
12
|
+
const initialValue = state.isFunction(initialParam) ? initialParam() : initialParam;
|
|
13
|
+
options.initialData = initialValue;
|
|
14
|
+
}
|
|
15
|
+
const initial = options.initialData;
|
|
10
16
|
const Observer = queryCore.QueryObserver;
|
|
11
17
|
const defaultedOptions = queryClient.defaultQueryOptions(
|
|
12
18
|
options
|
|
@@ -66,8 +72,16 @@ function syncedQuery(params) {
|
|
|
66
72
|
};
|
|
67
73
|
let set = void 0;
|
|
68
74
|
if (mutationOptions) {
|
|
69
|
-
const
|
|
75
|
+
const options2 = {
|
|
76
|
+
mutationKey: ["LS-mutation", nextMutationKey++],
|
|
77
|
+
...mutationOptions
|
|
78
|
+
};
|
|
79
|
+
const mutator = new queryCore.MutationObserver(queryClient, options2);
|
|
70
80
|
set = ({ value }) => {
|
|
81
|
+
const mutationCache = queryClient.getMutationCache();
|
|
82
|
+
mutationCache.findAll({ mutationKey: options2.mutationKey }).forEach((mutation) => {
|
|
83
|
+
mutationCache.remove(mutation);
|
|
84
|
+
});
|
|
71
85
|
return mutator.mutate(value);
|
|
72
86
|
};
|
|
73
87
|
}
|
|
@@ -75,6 +89,7 @@ function syncedQuery(params) {
|
|
|
75
89
|
get,
|
|
76
90
|
set,
|
|
77
91
|
subscribe,
|
|
92
|
+
initial,
|
|
78
93
|
...rest
|
|
79
94
|
});
|
|
80
95
|
}
|
|
@@ -3,8 +3,14 @@ import { synced } from '@legendapp/state/sync';
|
|
|
3
3
|
import { MutationObserver, QueryObserver, notifyManager } from '@tanstack/query-core';
|
|
4
4
|
|
|
5
5
|
// src/sync-plugins/tanstack-query.ts
|
|
6
|
+
var nextMutationKey = 0;
|
|
6
7
|
function syncedQuery(params) {
|
|
7
|
-
const { query: options, mutation: mutationOptions, queryClient, ...rest } = params;
|
|
8
|
+
const { query: options, mutation: mutationOptions, queryClient, initial: initialParam, ...rest } = params;
|
|
9
|
+
if (initialParam !== void 0) {
|
|
10
|
+
const initialValue = isFunction(initialParam) ? initialParam() : initialParam;
|
|
11
|
+
options.initialData = initialValue;
|
|
12
|
+
}
|
|
13
|
+
const initial = options.initialData;
|
|
8
14
|
const Observer = QueryObserver;
|
|
9
15
|
const defaultedOptions = queryClient.defaultQueryOptions(
|
|
10
16
|
options
|
|
@@ -64,8 +70,16 @@ function syncedQuery(params) {
|
|
|
64
70
|
};
|
|
65
71
|
let set = void 0;
|
|
66
72
|
if (mutationOptions) {
|
|
67
|
-
const
|
|
73
|
+
const options2 = {
|
|
74
|
+
mutationKey: ["LS-mutation", nextMutationKey++],
|
|
75
|
+
...mutationOptions
|
|
76
|
+
};
|
|
77
|
+
const mutator = new MutationObserver(queryClient, options2);
|
|
68
78
|
set = ({ value }) => {
|
|
79
|
+
const mutationCache = queryClient.getMutationCache();
|
|
80
|
+
mutationCache.findAll({ mutationKey: options2.mutationKey }).forEach((mutation) => {
|
|
81
|
+
mutationCache.remove(mutation);
|
|
82
|
+
});
|
|
69
83
|
return mutator.mutate(value);
|
|
70
84
|
};
|
|
71
85
|
}
|
|
@@ -73,6 +87,7 @@ function syncedQuery(params) {
|
|
|
73
87
|
get,
|
|
74
88
|
set,
|
|
75
89
|
subscribe,
|
|
90
|
+
initial,
|
|
76
91
|
...rest
|
|
77
92
|
});
|
|
78
93
|
}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { SyncedQueryParams } from '@legendapp/state/sync-plugins/tanstack-query';
|
|
2
|
-
import { DefaultError, QueryKey } from '@tanstack/query-core';
|
|
2
|
+
import { DefaultError, QueryKey, QueryClient } from '@tanstack/query-core';
|
|
3
3
|
import { Observable } from '@legendapp/state';
|
|
4
4
|
import { Synced } from '@legendapp/state/sync';
|
|
5
5
|
|
|
6
|
-
declare function useObservableSyncedQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(params: SyncedQueryParams<TQueryFnData, TError, TData, TQueryKey>
|
|
6
|
+
declare function useObservableSyncedQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(params: Omit<SyncedQueryParams<TQueryFnData, TError, TData, TQueryKey>, 'queryClient'> & {
|
|
7
|
+
queryClient?: QueryClient;
|
|
8
|
+
}): Observable<Synced<TData>>;
|
|
7
9
|
|
|
8
10
|
export { useObservableSyncedQuery };
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { SyncedQueryParams } from '@legendapp/state/sync-plugins/tanstack-query';
|
|
2
|
-
import { DefaultError, QueryKey } from '@tanstack/query-core';
|
|
2
|
+
import { DefaultError, QueryKey, QueryClient } from '@tanstack/query-core';
|
|
3
3
|
import { Observable } from '@legendapp/state';
|
|
4
4
|
import { Synced } from '@legendapp/state/sync';
|
|
5
5
|
|
|
6
|
-
declare function useObservableSyncedQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(params: SyncedQueryParams<TQueryFnData, TError, TData, TQueryKey>
|
|
6
|
+
declare function useObservableSyncedQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(params: Omit<SyncedQueryParams<TQueryFnData, TError, TData, TQueryKey>, 'queryClient'> & {
|
|
7
|
+
queryClient?: QueryClient;
|
|
8
|
+
}): Observable<Synced<TData>>;
|
|
7
9
|
|
|
8
10
|
export { useObservableSyncedQuery };
|