@legendapp/state 3.0.0-beta.1 → 3.0.0-beta.11
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.js +22 -24
- package/sync-plugins/crud.mjs +22 -24
- 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.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/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 };
|
package/sync.js
CHANGED
|
@@ -743,9 +743,20 @@ async function loadLocal(value$, syncOptions, syncState$, localState) {
|
|
|
743
743
|
await state.when(initialized$);
|
|
744
744
|
}
|
|
745
745
|
if (persistPlugin.loadTable) {
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
746
|
+
try {
|
|
747
|
+
const promise = persistPlugin.loadTable(table, config);
|
|
748
|
+
if (promise) {
|
|
749
|
+
await promise;
|
|
750
|
+
}
|
|
751
|
+
} catch (err) {
|
|
752
|
+
if (process.env.NODE_ENV === "development") {
|
|
753
|
+
console.error(
|
|
754
|
+
"[legend-state] Error loading local cache. This would be a crashing error in production.",
|
|
755
|
+
err
|
|
756
|
+
);
|
|
757
|
+
} else {
|
|
758
|
+
throw err;
|
|
759
|
+
}
|
|
749
760
|
}
|
|
750
761
|
}
|
|
751
762
|
const prevValue = getNodeValue(node);
|
|
@@ -848,7 +859,8 @@ function syncObservable(obs$, syncOptionsOrSynced) {
|
|
|
848
859
|
localState.isApplyingPending = false;
|
|
849
860
|
}
|
|
850
861
|
};
|
|
851
|
-
|
|
862
|
+
const { get, subscribe } = syncOptions;
|
|
863
|
+
if (get || subscribe) {
|
|
852
864
|
sync = async () => {
|
|
853
865
|
var _a;
|
|
854
866
|
if (isSynced && (!getNodeValue(getNode(syncState$)).isSyncEnabled || state.shouldIgnoreUnobserved(node, sync))) {
|
|
@@ -861,8 +873,7 @@ function syncObservable(obs$, syncOptionsOrSynced) {
|
|
|
861
873
|
}
|
|
862
874
|
const lastSync = (_a = metadatas.get(obs$)) == null ? void 0 : _a.lastSync;
|
|
863
875
|
const pending = localState.pendingChanges;
|
|
864
|
-
|
|
865
|
-
if (get) {
|
|
876
|
+
if (get || subscribe) {
|
|
866
877
|
const { waitFor } = syncOptions;
|
|
867
878
|
const runGet = () => {
|
|
868
879
|
var _a2;
|
|
@@ -955,7 +966,7 @@ function syncObservable(obs$, syncOptionsOrSynced) {
|
|
|
955
966
|
node.activationState.onChange = onChange;
|
|
956
967
|
}
|
|
957
968
|
if (!isSubscribed && syncOptions.subscribe) {
|
|
958
|
-
const
|
|
969
|
+
const subscribe2 = syncOptions.subscribe;
|
|
959
970
|
isSubscribed = true;
|
|
960
971
|
const doSubscribe = () => {
|
|
961
972
|
const subscribeParams = {
|
|
@@ -963,17 +974,27 @@ function syncObservable(obs$, syncOptionsOrSynced) {
|
|
|
963
974
|
value$: obs$,
|
|
964
975
|
lastSync,
|
|
965
976
|
update: (params) => {
|
|
966
|
-
state.when(
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
977
|
+
state.when(
|
|
978
|
+
() => !get || syncState$.isLoaded.get(),
|
|
979
|
+
() => {
|
|
980
|
+
state.when(waitFor || true, () => {
|
|
981
|
+
params.mode || (params.mode = syncOptions.mode || "merge");
|
|
982
|
+
onChange(params);
|
|
983
|
+
if (!syncState$.isLoaded.peek()) {
|
|
984
|
+
syncState$.assign({
|
|
985
|
+
isLoaded: syncStateValue.numPendingRemoteLoads < 1,
|
|
986
|
+
error: void 0,
|
|
987
|
+
isGetting: syncStateValue.numPendingGets > 0
|
|
988
|
+
});
|
|
989
|
+
}
|
|
990
|
+
});
|
|
991
|
+
}
|
|
992
|
+
);
|
|
972
993
|
},
|
|
973
994
|
refresh: () => state.when(syncState$.isLoaded, sync),
|
|
974
995
|
onError: (error) => onGetError(error, { source: "subscribe", subscribeParams })
|
|
975
996
|
};
|
|
976
|
-
unsubscribe =
|
|
997
|
+
unsubscribe = subscribe2(subscribeParams);
|
|
977
998
|
};
|
|
978
999
|
if (waitFor) {
|
|
979
1000
|
state.whenReady(waitFor, doSubscribe);
|
|
@@ -982,85 +1003,87 @@ function syncObservable(obs$, syncOptionsOrSynced) {
|
|
|
982
1003
|
}
|
|
983
1004
|
}
|
|
984
1005
|
const existingValue = getNodeValue(node);
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
},
|
|
1010
|
-
resetCache: () => {
|
|
1011
|
-
var _a3;
|
|
1012
|
-
modeBeforeReset = getParams.mode;
|
|
1013
|
-
getParams.mode = "set";
|
|
1014
|
-
return (_a3 = syncStateValue.resetPersistence) == null ? void 0 : _a3.call(syncStateValue);
|
|
1015
|
-
},
|
|
1016
|
-
cancel: false
|
|
1017
|
-
};
|
|
1018
|
-
(_a2 = syncOptions.onBeforeGet) == null ? void 0 : _a2.call(syncOptions, beforeGetParams);
|
|
1019
|
-
if (!beforeGetParams.cancel) {
|
|
1020
|
-
syncState$.assign({
|
|
1021
|
-
numPendingGets: (syncStateValue.numPendingGets || 0) + 1,
|
|
1022
|
-
isGetting: true
|
|
1023
|
-
});
|
|
1024
|
-
const got = runWithRetry(
|
|
1025
|
-
getParams,
|
|
1026
|
-
syncOptions.retry,
|
|
1027
|
-
(retryEvent) => {
|
|
1028
|
-
const params = getParams;
|
|
1029
|
-
params.cancelRetry = retryEvent.cancelRetry;
|
|
1030
|
-
params.retryNum = retryEvent.retryNum;
|
|
1031
|
-
return get(params);
|
|
1032
|
-
},
|
|
1033
|
-
onError
|
|
1034
|
-
);
|
|
1035
|
-
const numGets = node.numGets = (node.numGets || 0) + 1;
|
|
1036
|
-
const handle = (value) => {
|
|
1037
|
-
syncState$.numPendingGets.set((v) => v - 1);
|
|
1038
|
-
if (isWaitingForLoad) {
|
|
1039
|
-
isWaitingForLoad = false;
|
|
1040
|
-
syncStateValue.numPendingRemoteLoads--;
|
|
1041
|
-
}
|
|
1042
|
-
if (numGets >= (node.getNumResolved || 0)) {
|
|
1043
|
-
node.getNumResolved = node.numGets;
|
|
1044
|
-
onChange({
|
|
1045
|
-
value,
|
|
1046
|
-
lastSync: getParams.lastSync,
|
|
1047
|
-
mode: getParams.mode
|
|
1006
|
+
if (get) {
|
|
1007
|
+
const onError = (error) => onGetError(error, { getParams, source: "get" });
|
|
1008
|
+
const getParams = {
|
|
1009
|
+
node,
|
|
1010
|
+
value$: obs$,
|
|
1011
|
+
value: state.isFunction(existingValue) || (existingValue == null ? void 0 : existingValue[symbolLinked]) ? void 0 : existingValue,
|
|
1012
|
+
mode: syncOptions.mode,
|
|
1013
|
+
refresh: sync,
|
|
1014
|
+
options: syncOptions,
|
|
1015
|
+
lastSync,
|
|
1016
|
+
updateLastSync: (lastSync2) => getParams.lastSync = lastSync2,
|
|
1017
|
+
onError,
|
|
1018
|
+
retryNum: 0,
|
|
1019
|
+
cancelRetry: false
|
|
1020
|
+
};
|
|
1021
|
+
let modeBeforeReset = void 0;
|
|
1022
|
+
const beforeGetParams = {
|
|
1023
|
+
value: getParams.value,
|
|
1024
|
+
lastSync,
|
|
1025
|
+
pendingChanges: pending && !state.isEmpty(pending) ? pending : void 0,
|
|
1026
|
+
clearPendingChanges: async () => {
|
|
1027
|
+
localState.pendingChanges = {};
|
|
1028
|
+
await updateMetadataImmediate(obs$, localState, syncState$, syncOptions, {
|
|
1029
|
+
pending: localState.pendingChanges
|
|
1048
1030
|
});
|
|
1049
|
-
}
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
modeBeforeReset =
|
|
1053
|
-
|
|
1031
|
+
},
|
|
1032
|
+
resetCache: () => {
|
|
1033
|
+
var _a3;
|
|
1034
|
+
modeBeforeReset = getParams.mode;
|
|
1035
|
+
getParams.mode = "set";
|
|
1036
|
+
return (_a3 = syncStateValue.resetPersistence) == null ? void 0 : _a3.call(syncStateValue);
|
|
1037
|
+
},
|
|
1038
|
+
cancel: false
|
|
1039
|
+
};
|
|
1040
|
+
(_a2 = syncOptions.onBeforeGet) == null ? void 0 : _a2.call(syncOptions, beforeGetParams);
|
|
1041
|
+
if (!beforeGetParams.cancel) {
|
|
1054
1042
|
syncState$.assign({
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
isGetting: syncStateValue.numPendingGets > 0
|
|
1043
|
+
numPendingGets: (syncStateValue.numPendingGets || 0) + 1,
|
|
1044
|
+
isGetting: true
|
|
1058
1045
|
});
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1046
|
+
const got = runWithRetry(
|
|
1047
|
+
getParams,
|
|
1048
|
+
syncOptions.retry,
|
|
1049
|
+
(retryEvent) => {
|
|
1050
|
+
const params = getParams;
|
|
1051
|
+
params.cancelRetry = retryEvent.cancelRetry;
|
|
1052
|
+
params.retryNum = retryEvent.retryNum;
|
|
1053
|
+
return get(params);
|
|
1054
|
+
},
|
|
1055
|
+
onError
|
|
1056
|
+
);
|
|
1057
|
+
const numGets = node.numGets = (node.numGets || 0) + 1;
|
|
1058
|
+
const handle = (value) => {
|
|
1059
|
+
syncState$.numPendingGets.set((v) => v - 1);
|
|
1060
|
+
if (isWaitingForLoad) {
|
|
1061
|
+
isWaitingForLoad = false;
|
|
1062
|
+
syncStateValue.numPendingRemoteLoads--;
|
|
1063
|
+
}
|
|
1064
|
+
if (numGets >= (node.getNumResolved || 0)) {
|
|
1065
|
+
node.getNumResolved = node.numGets;
|
|
1066
|
+
onChange({
|
|
1067
|
+
value,
|
|
1068
|
+
lastSync: getParams.lastSync,
|
|
1069
|
+
mode: getParams.mode
|
|
1070
|
+
});
|
|
1071
|
+
}
|
|
1072
|
+
if (modeBeforeReset) {
|
|
1073
|
+
getParams.mode = modeBeforeReset;
|
|
1074
|
+
modeBeforeReset = void 0;
|
|
1075
|
+
}
|
|
1076
|
+
syncState$.assign({
|
|
1077
|
+
isLoaded: syncStateValue.numPendingRemoteLoads < 1,
|
|
1078
|
+
error: void 0,
|
|
1079
|
+
isGetting: syncStateValue.numPendingGets > 0
|
|
1080
|
+
});
|
|
1081
|
+
};
|
|
1082
|
+
if (state.isPromise(got)) {
|
|
1083
|
+
got.then(handle).catch(onError);
|
|
1084
|
+
} else {
|
|
1085
|
+
handle(got);
|
|
1086
|
+
}
|
|
1064
1087
|
}
|
|
1065
1088
|
}
|
|
1066
1089
|
};
|
|
@@ -1132,7 +1155,7 @@ function syncObservable(obs$, syncOptionsOrSynced) {
|
|
|
1132
1155
|
return true;
|
|
1133
1156
|
};
|
|
1134
1157
|
state.when(onAllPersistLoaded, function() {
|
|
1135
|
-
if (syncOptions.get && syncOptions.syncMode === "auto") {
|
|
1158
|
+
if ((syncOptions.get || syncOptions.subscribe) && syncOptions.syncMode === "auto") {
|
|
1136
1159
|
sync();
|
|
1137
1160
|
}
|
|
1138
1161
|
if ((syncOptions == null ? void 0 : syncOptions.set) || (syncOptions == null ? void 0 : syncOptions.persist)) {
|