@legendapp/state 3.0.0-beta.2 → 3.0.0-beta.20
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/.DS_Store +0 -0
- package/config/enableReactComponents.js +3 -1
- package/config/enableReactComponents.mjs +3 -1
- package/config/enableReactTracking.d.mts +2 -1
- package/config/enableReactTracking.d.ts +2 -1
- package/config/enableReactTracking.js +32 -13
- package/config/enableReactTracking.mjs +32 -13
- package/index.d.mts +13 -4
- package/index.d.ts +13 -4
- package/index.js +70 -22
- package/index.mjs +70 -22
- package/package.json +22 -1
- package/persist-plugins/async-storage.js +17 -9
- package/persist-plugins/async-storage.mjs +17 -9
- package/react-native.d.mts +4 -0
- package/react-native.d.ts +4 -0
- package/react-native.js +53 -0
- package/react-native.mjs +40 -0
- package/react-reactive/Components.d.mts +19 -0
- package/react-reactive/Components.d.ts +19 -0
- package/react-reactive/Components.js +53 -0
- package/react-reactive/Components.mjs +40 -0
- package/react-reactive/enableReactComponents.d.mts +3 -2
- package/react-reactive/enableReactComponents.d.ts +3 -2
- package/react-reactive/enableReactComponents.js +10 -3
- package/react-reactive/enableReactComponents.mjs +10 -3
- 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 +10 -3
- package/react-reactive/enableReactive.mjs +10 -3
- 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-web.d.mts +6 -0
- package/react-web.d.ts +6 -0
- package/react-web.js +39 -0
- package/react-web.mjs +37 -0
- package/react.d.mts +38 -20
- package/react.d.ts +38 -20
- package/react.js +36 -23
- package/react.mjs +37 -25
- package/sync-plugins/crud.d.mts +24 -9
- package/sync-plugins/crud.d.ts +24 -9
- package/sync-plugins/crud.js +199 -108
- package/sync-plugins/crud.mjs +200 -109
- package/sync-plugins/firebase.d.mts +7 -3
- package/sync-plugins/firebase.d.ts +7 -3
- package/sync-plugins/firebase.js +4 -2
- package/sync-plugins/firebase.mjs +4 -2
- package/sync-plugins/keel.d.mts +9 -13
- package/sync-plugins/keel.d.ts +9 -13
- package/sync-plugins/keel.js +52 -41
- package/sync-plugins/keel.mjs +53 -37
- package/sync-plugins/supabase.d.mts +7 -3
- package/sync-plugins/supabase.d.ts +7 -3
- package/sync-plugins/supabase.js +87 -31
- package/sync-plugins/supabase.mjs +88 -32
- package/sync-plugins/tanstack-query.d.mts +5 -5
- package/sync-plugins/tanstack-query.d.ts +5 -5
- package/sync-plugins/tanstack-query.js +10 -1
- package/sync-plugins/tanstack-query.mjs +10 -1
- package/sync-plugins/tanstack-react-query.d.mts +4 -2
- package/sync-plugins/tanstack-react-query.d.ts +4 -2
- package/sync.d.mts +16 -8
- package/sync.d.ts +16 -8
- package/sync.js +267 -174
- package/sync.mjs +266 -174
- 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/supabase.js
CHANGED
|
@@ -18,15 +18,29 @@ function configureSyncedSupabase(config) {
|
|
|
18
18
|
}
|
|
19
19
|
Object.assign(supabaseConfig, sync.removeNullUndefined(rest));
|
|
20
20
|
}
|
|
21
|
-
function wrapSupabaseFn(fn) {
|
|
22
|
-
return async (...args) => {
|
|
23
|
-
const {
|
|
21
|
+
function wrapSupabaseFn(fn, source) {
|
|
22
|
+
return async (params, ...args) => {
|
|
23
|
+
const { onError } = params;
|
|
24
|
+
const { data, error } = await fn(params, ...args);
|
|
24
25
|
if (error) {
|
|
25
|
-
|
|
26
|
+
onError(new Error(error.message), {
|
|
27
|
+
getParams: params,
|
|
28
|
+
source,
|
|
29
|
+
type: "get",
|
|
30
|
+
retry: params
|
|
31
|
+
});
|
|
26
32
|
}
|
|
27
33
|
return data;
|
|
28
34
|
};
|
|
29
35
|
}
|
|
36
|
+
function handleSupabaseError(error, onError, params) {
|
|
37
|
+
var _a;
|
|
38
|
+
if ((_a = error.message) == null ? void 0 : _a.includes("Failed to fetch")) {
|
|
39
|
+
throw error;
|
|
40
|
+
} else {
|
|
41
|
+
onError(new Error(error.message), params);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
30
44
|
function syncedSupabase(props) {
|
|
31
45
|
props = { ...supabaseConfig, ...props };
|
|
32
46
|
const {
|
|
@@ -36,9 +50,9 @@ function syncedSupabase(props) {
|
|
|
36
50
|
schema,
|
|
37
51
|
filter,
|
|
38
52
|
actions,
|
|
39
|
-
fieldCreatedAt
|
|
40
|
-
fieldUpdatedAt
|
|
41
|
-
fieldDeleted
|
|
53
|
+
fieldCreatedAt,
|
|
54
|
+
fieldUpdatedAt,
|
|
55
|
+
fieldDeleted,
|
|
42
56
|
realtime,
|
|
43
57
|
changesSince,
|
|
44
58
|
transform: transformParam,
|
|
@@ -54,11 +68,19 @@ function syncedSupabase(props) {
|
|
|
54
68
|
...rest
|
|
55
69
|
} = props;
|
|
56
70
|
const client = supabase;
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
71
|
+
if (process.env.NODE_ENV === "development" && changesSince === "last-sync") {
|
|
72
|
+
if (!fieldCreatedAt) {
|
|
73
|
+
console.warn("[legend-state] fieldCreatedAt is required when using last-sync mode");
|
|
74
|
+
}
|
|
75
|
+
if (!fieldUpdatedAt) {
|
|
76
|
+
console.warn("[legend-state] fieldUpdatedAt is required when using last-sync mode");
|
|
77
|
+
}
|
|
78
|
+
if (!fieldDeleted) {
|
|
79
|
+
console.warn("[legend-state] fieldDeleted is required when using last-sync mode");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const list = !actions || actions.includes("read") ? listParam ? wrapSupabaseFn(listParam, "list") : async (params) => {
|
|
83
|
+
const { lastSync, onError } = params;
|
|
62
84
|
const clientSchema = schema ? client.schema(schema) : client;
|
|
63
85
|
const from = clientSchema.from(collection);
|
|
64
86
|
let select = selectFn ? selectFn(from) : from.select();
|
|
@@ -70,42 +92,76 @@ function syncedSupabase(props) {
|
|
|
70
92
|
select = filter(select, params);
|
|
71
93
|
}
|
|
72
94
|
const { data, error } = await select;
|
|
73
|
-
if (
|
|
74
|
-
|
|
95
|
+
if (data) {
|
|
96
|
+
return data || [];
|
|
97
|
+
} else if (error) {
|
|
98
|
+
handleSupabaseError(error, onError, {
|
|
99
|
+
getParams: params,
|
|
100
|
+
source: "list",
|
|
101
|
+
type: "get",
|
|
102
|
+
retry: params
|
|
103
|
+
});
|
|
75
104
|
}
|
|
76
|
-
return
|
|
105
|
+
return null;
|
|
77
106
|
} : void 0;
|
|
78
|
-
const create = createParam ? wrapSupabaseFn(createParam) : !actions || actions.includes("create") ? async (input) => {
|
|
107
|
+
const create = createParam ? wrapSupabaseFn(createParam, "create") : !actions || actions.includes("create") ? async (input, params) => {
|
|
108
|
+
const { onError } = params;
|
|
79
109
|
const res = await client.from(collection).insert(input).select();
|
|
80
110
|
const { data, error } = res;
|
|
81
111
|
if (data) {
|
|
82
112
|
const created = data[0];
|
|
83
113
|
return created;
|
|
84
|
-
} else {
|
|
85
|
-
|
|
114
|
+
} else if (error) {
|
|
115
|
+
handleSupabaseError(error, onError, {
|
|
116
|
+
setParams: params,
|
|
117
|
+
source: "create",
|
|
118
|
+
type: "set",
|
|
119
|
+
retry: params,
|
|
120
|
+
input,
|
|
121
|
+
revert: sync.createRevertChanges(params.value$, params.changes)
|
|
122
|
+
});
|
|
86
123
|
}
|
|
87
124
|
} : void 0;
|
|
88
|
-
const update = !actions || actions.includes("update") ? updateParam ? wrapSupabaseFn(updateParam) : async (input) => {
|
|
125
|
+
const update = !actions || actions.includes("update") ? updateParam ? wrapSupabaseFn(updateParam, "update") : async (input, params) => {
|
|
126
|
+
const { onError } = params;
|
|
89
127
|
const res = await client.from(collection).update(input).eq("id", input.id).select();
|
|
90
128
|
const { data, error } = res;
|
|
91
129
|
if (data) {
|
|
92
130
|
const created = data[0];
|
|
93
131
|
return created;
|
|
94
|
-
} else {
|
|
95
|
-
|
|
132
|
+
} else if (error) {
|
|
133
|
+
handleSupabaseError(error, onError, {
|
|
134
|
+
setParams: params,
|
|
135
|
+
source: "update",
|
|
136
|
+
type: "set",
|
|
137
|
+
retry: params,
|
|
138
|
+
input,
|
|
139
|
+
revert: sync.createRevertChanges(params.value$, params.changes)
|
|
140
|
+
});
|
|
96
141
|
}
|
|
97
142
|
} : void 0;
|
|
98
|
-
const deleteFn = !fieldDeleted && (!actions || actions.includes("delete")) ?
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
143
|
+
const deleteFn = !fieldDeleted && (!actions || actions.includes("delete")) ? (
|
|
144
|
+
// prettier-ignore
|
|
145
|
+
deleteParam ? wrapSupabaseFn(deleteParam, "delete") : async (input, params) => {
|
|
146
|
+
const { onError } = params;
|
|
147
|
+
const id = input.id;
|
|
148
|
+
const res = await client.from(collection).delete().eq("id", id).select();
|
|
149
|
+
const { data, error } = res;
|
|
150
|
+
if (data) {
|
|
151
|
+
const created = data[0];
|
|
152
|
+
return created;
|
|
153
|
+
} else if (error) {
|
|
154
|
+
handleSupabaseError(error, onError, {
|
|
155
|
+
setParams: params,
|
|
156
|
+
source: "delete",
|
|
157
|
+
type: "set",
|
|
158
|
+
retry: params,
|
|
159
|
+
input,
|
|
160
|
+
revert: sync.createRevertChanges(params.value$, params.changes)
|
|
161
|
+
});
|
|
162
|
+
}
|
|
107
163
|
}
|
|
108
|
-
|
|
164
|
+
) : void 0;
|
|
109
165
|
const subscribe = realtime ? ({ node, value$, update: update2 }) => {
|
|
110
166
|
const { filter: filter2, schema: schema2 } = state.isObject(realtime) ? realtime : {};
|
|
111
167
|
const channel = client.channel(`LS_${node.key || ""}${channelNum++}`).on(
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { observable, computeSelector, isFunction, isObject, symbolDelete } from '@legendapp/state';
|
|
2
|
-
import { removeNullUndefined, transformStringifyDates, combineTransforms } from '@legendapp/state/sync';
|
|
2
|
+
import { removeNullUndefined, createRevertChanges, transformStringifyDates, combineTransforms } from '@legendapp/state/sync';
|
|
3
3
|
import { syncedCrud } from '@legendapp/state/sync-plugins/crud';
|
|
4
4
|
|
|
5
5
|
// src/sync-plugins/supabase.ts
|
|
@@ -16,15 +16,29 @@ function configureSyncedSupabase(config) {
|
|
|
16
16
|
}
|
|
17
17
|
Object.assign(supabaseConfig, removeNullUndefined(rest));
|
|
18
18
|
}
|
|
19
|
-
function wrapSupabaseFn(fn) {
|
|
20
|
-
return async (...args) => {
|
|
21
|
-
const {
|
|
19
|
+
function wrapSupabaseFn(fn, source) {
|
|
20
|
+
return async (params, ...args) => {
|
|
21
|
+
const { onError } = params;
|
|
22
|
+
const { data, error } = await fn(params, ...args);
|
|
22
23
|
if (error) {
|
|
23
|
-
|
|
24
|
+
onError(new Error(error.message), {
|
|
25
|
+
getParams: params,
|
|
26
|
+
source,
|
|
27
|
+
type: "get",
|
|
28
|
+
retry: params
|
|
29
|
+
});
|
|
24
30
|
}
|
|
25
31
|
return data;
|
|
26
32
|
};
|
|
27
33
|
}
|
|
34
|
+
function handleSupabaseError(error, onError, params) {
|
|
35
|
+
var _a;
|
|
36
|
+
if ((_a = error.message) == null ? void 0 : _a.includes("Failed to fetch")) {
|
|
37
|
+
throw error;
|
|
38
|
+
} else {
|
|
39
|
+
onError(new Error(error.message), params);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
28
42
|
function syncedSupabase(props) {
|
|
29
43
|
props = { ...supabaseConfig, ...props };
|
|
30
44
|
const {
|
|
@@ -34,9 +48,9 @@ function syncedSupabase(props) {
|
|
|
34
48
|
schema,
|
|
35
49
|
filter,
|
|
36
50
|
actions,
|
|
37
|
-
fieldCreatedAt
|
|
38
|
-
fieldUpdatedAt
|
|
39
|
-
fieldDeleted
|
|
51
|
+
fieldCreatedAt,
|
|
52
|
+
fieldUpdatedAt,
|
|
53
|
+
fieldDeleted,
|
|
40
54
|
realtime,
|
|
41
55
|
changesSince,
|
|
42
56
|
transform: transformParam,
|
|
@@ -52,11 +66,19 @@ function syncedSupabase(props) {
|
|
|
52
66
|
...rest
|
|
53
67
|
} = props;
|
|
54
68
|
const client = supabase;
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
69
|
+
if (process.env.NODE_ENV === "development" && changesSince === "last-sync") {
|
|
70
|
+
if (!fieldCreatedAt) {
|
|
71
|
+
console.warn("[legend-state] fieldCreatedAt is required when using last-sync mode");
|
|
72
|
+
}
|
|
73
|
+
if (!fieldUpdatedAt) {
|
|
74
|
+
console.warn("[legend-state] fieldUpdatedAt is required when using last-sync mode");
|
|
75
|
+
}
|
|
76
|
+
if (!fieldDeleted) {
|
|
77
|
+
console.warn("[legend-state] fieldDeleted is required when using last-sync mode");
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const list = !actions || actions.includes("read") ? listParam ? wrapSupabaseFn(listParam, "list") : async (params) => {
|
|
81
|
+
const { lastSync, onError } = params;
|
|
60
82
|
const clientSchema = schema ? client.schema(schema) : client;
|
|
61
83
|
const from = clientSchema.from(collection);
|
|
62
84
|
let select = selectFn ? selectFn(from) : from.select();
|
|
@@ -68,42 +90,76 @@ function syncedSupabase(props) {
|
|
|
68
90
|
select = filter(select, params);
|
|
69
91
|
}
|
|
70
92
|
const { data, error } = await select;
|
|
71
|
-
if (
|
|
72
|
-
|
|
93
|
+
if (data) {
|
|
94
|
+
return data || [];
|
|
95
|
+
} else if (error) {
|
|
96
|
+
handleSupabaseError(error, onError, {
|
|
97
|
+
getParams: params,
|
|
98
|
+
source: "list",
|
|
99
|
+
type: "get",
|
|
100
|
+
retry: params
|
|
101
|
+
});
|
|
73
102
|
}
|
|
74
|
-
return
|
|
103
|
+
return null;
|
|
75
104
|
} : void 0;
|
|
76
|
-
const create = createParam ? wrapSupabaseFn(createParam) : !actions || actions.includes("create") ? async (input) => {
|
|
105
|
+
const create = createParam ? wrapSupabaseFn(createParam, "create") : !actions || actions.includes("create") ? async (input, params) => {
|
|
106
|
+
const { onError } = params;
|
|
77
107
|
const res = await client.from(collection).insert(input).select();
|
|
78
108
|
const { data, error } = res;
|
|
79
109
|
if (data) {
|
|
80
110
|
const created = data[0];
|
|
81
111
|
return created;
|
|
82
|
-
} else {
|
|
83
|
-
|
|
112
|
+
} else if (error) {
|
|
113
|
+
handleSupabaseError(error, onError, {
|
|
114
|
+
setParams: params,
|
|
115
|
+
source: "create",
|
|
116
|
+
type: "set",
|
|
117
|
+
retry: params,
|
|
118
|
+
input,
|
|
119
|
+
revert: createRevertChanges(params.value$, params.changes)
|
|
120
|
+
});
|
|
84
121
|
}
|
|
85
122
|
} : void 0;
|
|
86
|
-
const update = !actions || actions.includes("update") ? updateParam ? wrapSupabaseFn(updateParam) : async (input) => {
|
|
123
|
+
const update = !actions || actions.includes("update") ? updateParam ? wrapSupabaseFn(updateParam, "update") : async (input, params) => {
|
|
124
|
+
const { onError } = params;
|
|
87
125
|
const res = await client.from(collection).update(input).eq("id", input.id).select();
|
|
88
126
|
const { data, error } = res;
|
|
89
127
|
if (data) {
|
|
90
128
|
const created = data[0];
|
|
91
129
|
return created;
|
|
92
|
-
} else {
|
|
93
|
-
|
|
130
|
+
} else if (error) {
|
|
131
|
+
handleSupabaseError(error, onError, {
|
|
132
|
+
setParams: params,
|
|
133
|
+
source: "update",
|
|
134
|
+
type: "set",
|
|
135
|
+
retry: params,
|
|
136
|
+
input,
|
|
137
|
+
revert: createRevertChanges(params.value$, params.changes)
|
|
138
|
+
});
|
|
94
139
|
}
|
|
95
140
|
} : void 0;
|
|
96
|
-
const deleteFn = !fieldDeleted && (!actions || actions.includes("delete")) ?
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
141
|
+
const deleteFn = !fieldDeleted && (!actions || actions.includes("delete")) ? (
|
|
142
|
+
// prettier-ignore
|
|
143
|
+
deleteParam ? wrapSupabaseFn(deleteParam, "delete") : async (input, params) => {
|
|
144
|
+
const { onError } = params;
|
|
145
|
+
const id = input.id;
|
|
146
|
+
const res = await client.from(collection).delete().eq("id", id).select();
|
|
147
|
+
const { data, error } = res;
|
|
148
|
+
if (data) {
|
|
149
|
+
const created = data[0];
|
|
150
|
+
return created;
|
|
151
|
+
} else if (error) {
|
|
152
|
+
handleSupabaseError(error, onError, {
|
|
153
|
+
setParams: params,
|
|
154
|
+
source: "delete",
|
|
155
|
+
type: "set",
|
|
156
|
+
retry: params,
|
|
157
|
+
input,
|
|
158
|
+
revert: createRevertChanges(params.value$, params.changes)
|
|
159
|
+
});
|
|
160
|
+
}
|
|
105
161
|
}
|
|
106
|
-
|
|
162
|
+
) : void 0;
|
|
107
163
|
const subscribe = realtime ? ({ node, value$, update: update2 }) => {
|
|
108
164
|
const { filter: filter2, schema: schema2 } = isObject(realtime) ? realtime : {};
|
|
109
165
|
const channel = client.channel(`LS_${node.key || ""}${channelNum++}`).on(
|
|
@@ -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,6 +5,7 @@ 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
10
|
const { query: options, mutation: mutationOptions, queryClient, initial: initialParam, ...rest } = params;
|
|
10
11
|
if (initialParam !== void 0) {
|
|
@@ -71,8 +72,16 @@ function syncedQuery(params) {
|
|
|
71
72
|
};
|
|
72
73
|
let set = void 0;
|
|
73
74
|
if (mutationOptions) {
|
|
74
|
-
const
|
|
75
|
+
const options2 = {
|
|
76
|
+
mutationKey: ["LS-mutation", nextMutationKey++],
|
|
77
|
+
...mutationOptions
|
|
78
|
+
};
|
|
79
|
+
const mutator = new queryCore.MutationObserver(queryClient, options2);
|
|
75
80
|
set = ({ value }) => {
|
|
81
|
+
const mutationCache = queryClient.getMutationCache();
|
|
82
|
+
mutationCache.findAll({ mutationKey: options2.mutationKey }).forEach((mutation) => {
|
|
83
|
+
mutationCache.remove(mutation);
|
|
84
|
+
});
|
|
76
85
|
return mutator.mutate(value);
|
|
77
86
|
};
|
|
78
87
|
}
|
|
@@ -3,6 +3,7 @@ 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
8
|
const { query: options, mutation: mutationOptions, queryClient, initial: initialParam, ...rest } = params;
|
|
8
9
|
if (initialParam !== void 0) {
|
|
@@ -69,8 +70,16 @@ function syncedQuery(params) {
|
|
|
69
70
|
};
|
|
70
71
|
let set = void 0;
|
|
71
72
|
if (mutationOptions) {
|
|
72
|
-
const
|
|
73
|
+
const options2 = {
|
|
74
|
+
mutationKey: ["LS-mutation", nextMutationKey++],
|
|
75
|
+
...mutationOptions
|
|
76
|
+
};
|
|
77
|
+
const mutator = new MutationObserver(queryClient, options2);
|
|
73
78
|
set = ({ value }) => {
|
|
79
|
+
const mutationCache = queryClient.getMutationCache();
|
|
80
|
+
mutationCache.findAll({ mutationKey: options2.mutationKey }).forEach((mutation) => {
|
|
81
|
+
mutationCache.remove(mutation);
|
|
82
|
+
});
|
|
74
83
|
return mutator.mutate(value);
|
|
75
84
|
};
|
|
76
85
|
}
|
|
@@ -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.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { MMKVConfiguration } from 'react-native-mmkv';
|
|
2
2
|
import { AsyncStorageStatic } from '@react-native-async-storage/async-storage';
|
|
3
|
-
import { ClassConstructor, NodeInfo, ObservableParam, GetMode, SetParams, UpdateFn, LinkedOptions, RetryOptions, Change, Observable, ObservableSyncState, TypeAtPath, RecordValue, ArrayValue, WaitForSet } from '@legendapp/state';
|
|
3
|
+
import { ClassConstructor, NodeInfo, ObservableParam, GetMode, SetParams, UpdateSetFn, UpdateFn, LinkedOptions, RetryOptions, Change, Observable, ObservableSyncState, TypeAtPath, RecordValue, ArrayValue, WaitForSet } from '@legendapp/state';
|
|
4
4
|
import { SyncedOptionsGlobal as SyncedOptionsGlobal$1 } from '@legendapp/state/sync';
|
|
5
5
|
|
|
6
6
|
interface PersistOptions<T = any> {
|
|
@@ -32,12 +32,12 @@ interface SyncedGetParams<T> extends SyncedGetSetBaseParams<T> {
|
|
|
32
32
|
lastSync: number | undefined;
|
|
33
33
|
updateLastSync: (lastSync: number) => void;
|
|
34
34
|
mode: GetMode;
|
|
35
|
-
onError: (error: Error) => void;
|
|
35
|
+
onError: (error: Error, params: SyncedErrorParams) => void;
|
|
36
36
|
options: SyncedOptions;
|
|
37
37
|
}
|
|
38
38
|
interface SyncedSetParams<T> extends Pick<SetParams<T>, 'changes' | 'value'>, SyncedGetSetBaseParams<T> {
|
|
39
|
-
update:
|
|
40
|
-
onError: (error: Error,
|
|
39
|
+
update: UpdateSetFn<T>;
|
|
40
|
+
onError: (error: Error, params: SyncedErrorParams) => void;
|
|
41
41
|
}
|
|
42
42
|
interface SyncedSubscribeParams<T = any> extends SyncedGetSetSubscribeBaseParams<T> {
|
|
43
43
|
lastSync: number | undefined;
|
|
@@ -45,12 +45,14 @@ interface SyncedSubscribeParams<T = any> extends SyncedGetSetSubscribeBaseParams
|
|
|
45
45
|
onError: (error: Error) => void;
|
|
46
46
|
}
|
|
47
47
|
interface SyncedErrorParams {
|
|
48
|
+
source: 'get' | 'set' | 'subscribe';
|
|
49
|
+
type: 'get' | 'set';
|
|
50
|
+
retry: OnErrorRetryParams;
|
|
48
51
|
getParams?: SyncedGetParams<any>;
|
|
49
52
|
setParams?: SyncedSetParams<any>;
|
|
50
53
|
subscribeParams?: SyncedSubscribeParams<any>;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
retryParams?: OnErrorRetryParams;
|
|
54
|
+
input?: any;
|
|
55
|
+
revert?: () => void;
|
|
54
56
|
}
|
|
55
57
|
interface SyncedOptions<TRemote = any, TLocal = TRemote> extends Omit<LinkedOptions<TRemote>, 'get' | 'set'> {
|
|
56
58
|
get?: (params: SyncedGetParams<TRemote>) => Promise<TRemote> | TRemote;
|
|
@@ -215,11 +217,17 @@ declare function synced<TRemote, TLocal = TRemote>(params: SyncedOptions<TRemote
|
|
|
215
217
|
declare function configureSynced<T extends (...args: any[]) => any>(fn: T, origOptions: Partial<Parameters<T>[0]>): T;
|
|
216
218
|
declare function configureSynced(origOptions: SyncedOptions): typeof synced;
|
|
217
219
|
|
|
220
|
+
declare function createRevertChanges(obs$: ObservableParam<any>, changes: Change[]): () => void;
|
|
221
|
+
|
|
218
222
|
declare function waitForSet(waitForSet: WaitForSet<any>, changes: Change[], value: any, params?: Record<string, any>): Promise<void>;
|
|
219
223
|
|
|
224
|
+
declare function runWithRetry<T>(state: SyncedGetSetBaseParams<any>, retryOptions: RetryOptions | undefined, retryId: any, fn: (params: OnErrorRetryParams) => Promise<T>): Promise<T>;
|
|
225
|
+
declare function runWithRetry<T>(state: SyncedGetSetBaseParams<any>, retryOptions: RetryOptions | undefined, retryId: any, fn: (params: OnErrorRetryParams) => T): T;
|
|
226
|
+
|
|
220
227
|
declare const internal: {
|
|
221
228
|
observableSyncConfiguration: SyncedOptionsGlobal;
|
|
222
229
|
waitForSet: typeof waitForSet;
|
|
230
|
+
runWithRetry: typeof runWithRetry;
|
|
223
231
|
};
|
|
224
232
|
|
|
225
|
-
export { type FieldTransforms, type FieldTransformsInner, type ObservablePersistAsyncStoragePluginOptions, type ObservablePersistIndexedDBPluginOptions, type ObservablePersistPlugin, type ObservablePersistPluginOptions, type ObservableSyncFunctions, type ObservableSyncSetParams, type OnErrorRetryParams, type PendingChanges, type PersistMetadata, type PersistOptions, type QueryByModified, type StringToDate, type SubscribeOptions, type SyncTransform, type SyncTransformMethod, type Synced, type SyncedErrorParams, type SyncedGetParams, type SyncedGetSetBaseParams, type SyncedGetSetSubscribeBaseParams, type SyncedOptions, type SyncedOptionsGlobal, type SyncedSetParams, type SyncedSubscribeParams, type TransformStringifyKeys, type TransformStringifyOptions, type TransformStringsToDates, combineTransforms, configureObservableSync, configureSynced, deepEqual, diffObjects, internal, mapSyncPlugins, onChangeRemote, removeNullUndefined, syncObservable, synced, transformStringifyDates, transformStringifyKeys };
|
|
233
|
+
export { type FieldTransforms, type FieldTransformsInner, type ObservablePersistAsyncStoragePluginOptions, type ObservablePersistIndexedDBPluginOptions, type ObservablePersistPlugin, type ObservablePersistPluginOptions, type ObservableSyncFunctions, type ObservableSyncSetParams, type OnErrorRetryParams, type PendingChanges, type PersistMetadata, type PersistOptions, type QueryByModified, type StringToDate, type SubscribeOptions, type SyncTransform, type SyncTransformMethod, type Synced, type SyncedErrorParams, type SyncedGetParams, type SyncedGetSetBaseParams, type SyncedGetSetSubscribeBaseParams, type SyncedOptions, type SyncedOptionsGlobal, type SyncedSetParams, type SyncedSubscribeParams, type TransformStringifyKeys, type TransformStringifyOptions, type TransformStringsToDates, combineTransforms, configureObservableSync, configureSynced, createRevertChanges, deepEqual, diffObjects, internal, mapSyncPlugins, onChangeRemote, removeNullUndefined, syncObservable, synced, transformStringifyDates, transformStringifyKeys };
|
package/sync.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { MMKVConfiguration } from 'react-native-mmkv';
|
|
2
2
|
import { AsyncStorageStatic } from '@react-native-async-storage/async-storage';
|
|
3
|
-
import { ClassConstructor, NodeInfo, ObservableParam, GetMode, SetParams, UpdateFn, LinkedOptions, RetryOptions, Change, Observable, ObservableSyncState, TypeAtPath, RecordValue, ArrayValue, WaitForSet } from '@legendapp/state';
|
|
3
|
+
import { ClassConstructor, NodeInfo, ObservableParam, GetMode, SetParams, UpdateSetFn, UpdateFn, LinkedOptions, RetryOptions, Change, Observable, ObservableSyncState, TypeAtPath, RecordValue, ArrayValue, WaitForSet } from '@legendapp/state';
|
|
4
4
|
import { SyncedOptionsGlobal as SyncedOptionsGlobal$1 } from '@legendapp/state/sync';
|
|
5
5
|
|
|
6
6
|
interface PersistOptions<T = any> {
|
|
@@ -32,12 +32,12 @@ interface SyncedGetParams<T> extends SyncedGetSetBaseParams<T> {
|
|
|
32
32
|
lastSync: number | undefined;
|
|
33
33
|
updateLastSync: (lastSync: number) => void;
|
|
34
34
|
mode: GetMode;
|
|
35
|
-
onError: (error: Error) => void;
|
|
35
|
+
onError: (error: Error, params: SyncedErrorParams) => void;
|
|
36
36
|
options: SyncedOptions;
|
|
37
37
|
}
|
|
38
38
|
interface SyncedSetParams<T> extends Pick<SetParams<T>, 'changes' | 'value'>, SyncedGetSetBaseParams<T> {
|
|
39
|
-
update:
|
|
40
|
-
onError: (error: Error,
|
|
39
|
+
update: UpdateSetFn<T>;
|
|
40
|
+
onError: (error: Error, params: SyncedErrorParams) => void;
|
|
41
41
|
}
|
|
42
42
|
interface SyncedSubscribeParams<T = any> extends SyncedGetSetSubscribeBaseParams<T> {
|
|
43
43
|
lastSync: number | undefined;
|
|
@@ -45,12 +45,14 @@ interface SyncedSubscribeParams<T = any> extends SyncedGetSetSubscribeBaseParams
|
|
|
45
45
|
onError: (error: Error) => void;
|
|
46
46
|
}
|
|
47
47
|
interface SyncedErrorParams {
|
|
48
|
+
source: 'get' | 'set' | 'subscribe';
|
|
49
|
+
type: 'get' | 'set';
|
|
50
|
+
retry: OnErrorRetryParams;
|
|
48
51
|
getParams?: SyncedGetParams<any>;
|
|
49
52
|
setParams?: SyncedSetParams<any>;
|
|
50
53
|
subscribeParams?: SyncedSubscribeParams<any>;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
retryParams?: OnErrorRetryParams;
|
|
54
|
+
input?: any;
|
|
55
|
+
revert?: () => void;
|
|
54
56
|
}
|
|
55
57
|
interface SyncedOptions<TRemote = any, TLocal = TRemote> extends Omit<LinkedOptions<TRemote>, 'get' | 'set'> {
|
|
56
58
|
get?: (params: SyncedGetParams<TRemote>) => Promise<TRemote> | TRemote;
|
|
@@ -215,11 +217,17 @@ declare function synced<TRemote, TLocal = TRemote>(params: SyncedOptions<TRemote
|
|
|
215
217
|
declare function configureSynced<T extends (...args: any[]) => any>(fn: T, origOptions: Partial<Parameters<T>[0]>): T;
|
|
216
218
|
declare function configureSynced(origOptions: SyncedOptions): typeof synced;
|
|
217
219
|
|
|
220
|
+
declare function createRevertChanges(obs$: ObservableParam<any>, changes: Change[]): () => void;
|
|
221
|
+
|
|
218
222
|
declare function waitForSet(waitForSet: WaitForSet<any>, changes: Change[], value: any, params?: Record<string, any>): Promise<void>;
|
|
219
223
|
|
|
224
|
+
declare function runWithRetry<T>(state: SyncedGetSetBaseParams<any>, retryOptions: RetryOptions | undefined, retryId: any, fn: (params: OnErrorRetryParams) => Promise<T>): Promise<T>;
|
|
225
|
+
declare function runWithRetry<T>(state: SyncedGetSetBaseParams<any>, retryOptions: RetryOptions | undefined, retryId: any, fn: (params: OnErrorRetryParams) => T): T;
|
|
226
|
+
|
|
220
227
|
declare const internal: {
|
|
221
228
|
observableSyncConfiguration: SyncedOptionsGlobal;
|
|
222
229
|
waitForSet: typeof waitForSet;
|
|
230
|
+
runWithRetry: typeof runWithRetry;
|
|
223
231
|
};
|
|
224
232
|
|
|
225
|
-
export { type FieldTransforms, type FieldTransformsInner, type ObservablePersistAsyncStoragePluginOptions, type ObservablePersistIndexedDBPluginOptions, type ObservablePersistPlugin, type ObservablePersistPluginOptions, type ObservableSyncFunctions, type ObservableSyncSetParams, type OnErrorRetryParams, type PendingChanges, type PersistMetadata, type PersistOptions, type QueryByModified, type StringToDate, type SubscribeOptions, type SyncTransform, type SyncTransformMethod, type Synced, type SyncedErrorParams, type SyncedGetParams, type SyncedGetSetBaseParams, type SyncedGetSetSubscribeBaseParams, type SyncedOptions, type SyncedOptionsGlobal, type SyncedSetParams, type SyncedSubscribeParams, type TransformStringifyKeys, type TransformStringifyOptions, type TransformStringsToDates, combineTransforms, configureObservableSync, configureSynced, deepEqual, diffObjects, internal, mapSyncPlugins, onChangeRemote, removeNullUndefined, syncObservable, synced, transformStringifyDates, transformStringifyKeys };
|
|
233
|
+
export { type FieldTransforms, type FieldTransformsInner, type ObservablePersistAsyncStoragePluginOptions, type ObservablePersistIndexedDBPluginOptions, type ObservablePersistPlugin, type ObservablePersistPluginOptions, type ObservableSyncFunctions, type ObservableSyncSetParams, type OnErrorRetryParams, type PendingChanges, type PersistMetadata, type PersistOptions, type QueryByModified, type StringToDate, type SubscribeOptions, type SyncTransform, type SyncTransformMethod, type Synced, type SyncedErrorParams, type SyncedGetParams, type SyncedGetSetBaseParams, type SyncedGetSetSubscribeBaseParams, type SyncedOptions, type SyncedOptionsGlobal, type SyncedSetParams, type SyncedSubscribeParams, type TransformStringifyKeys, type TransformStringifyOptions, type TransformStringsToDates, combineTransforms, configureObservableSync, configureSynced, createRevertChanges, deepEqual, diffObjects, internal, mapSyncPlugins, onChangeRemote, removeNullUndefined, syncObservable, synced, transformStringifyDates, transformStringifyKeys };
|