@legendapp/state 3.0.0-beta.12 → 3.0.0-beta.14
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/index.d.mts +6 -1
- package/index.d.ts +6 -1
- package/index.js +35 -9
- package/index.mjs +35 -9
- package/package.json +1 -1
- package/react-reactive/enableReactComponents.js +3 -1
- package/react-reactive/enableReactComponents.mjs +3 -1
- package/react-reactive/enableReactive.js +3 -1
- package/react-reactive/enableReactive.mjs +3 -1
- package/react.d.mts +26 -15
- package/react.d.ts +26 -15
- package/react.js +13 -9
- package/react.mjs +13 -9
- package/sync-plugins/crud.d.mts +8 -3
- package/sync-plugins/crud.d.ts +8 -3
- package/sync-plugins/crud.js +144 -80
- package/sync-plugins/crud.mjs +144 -80
- 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 +5 -9
- package/sync-plugins/keel.d.ts +5 -9
- package/sync-plugins/keel.js +48 -30
- package/sync-plugins/keel.mjs +48 -30
- package/sync-plugins/supabase.d.mts +7 -3
- package/sync-plugins/supabase.d.ts +7 -3
- package/sync-plugins/supabase.js +55 -18
- package/sync-plugins/supabase.mjs +56 -19
- package/sync.d.mts +16 -8
- package/sync.d.ts +16 -8
- package/sync.js +95 -67
- package/sync.mjs +94 -67
package/react.mjs
CHANGED
|
@@ -134,7 +134,7 @@ function Computed({ children }) {
|
|
|
134
134
|
}
|
|
135
135
|
var hasSymbol = typeof Symbol === "function" && Symbol.for;
|
|
136
136
|
var didWarnProps = false;
|
|
137
|
-
function createReactiveComponent(component, observe3, reactive2, bindKeys) {
|
|
137
|
+
function createReactiveComponent(component, observe3, reactive2, keysReactive, bindKeys) {
|
|
138
138
|
const ReactForwardRefSymbol = hasSymbol ? Symbol.for("react.forward_ref") : (
|
|
139
139
|
// eslint-disable-next-line react/display-name, @typescript-eslint/no-unused-vars
|
|
140
140
|
typeof forwardRef === "function" && forwardRef((props) => null)["$$typeof"]
|
|
@@ -159,6 +159,7 @@ function createReactiveComponent(component, observe3, reactive2, bindKeys) {
|
|
|
159
159
|
throw new Error(`[legend-state] \`render\` property of ForwardRef was not a function`);
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
|
+
const keysReactiveSet = keysReactive ? new Set(keysReactive) : void 0;
|
|
162
163
|
const proxyHandler = {
|
|
163
164
|
apply(target, thisArg, argArray) {
|
|
164
165
|
if (reactive2) {
|
|
@@ -168,9 +169,10 @@ function createReactiveComponent(component, observe3, reactive2, bindKeys) {
|
|
|
168
169
|
for (let i = 0; i < keys.length; i++) {
|
|
169
170
|
const key = keys[i];
|
|
170
171
|
const p = props[key];
|
|
172
|
+
const isReactiveKey = keysReactiveSet && keysReactiveSet.has(key);
|
|
171
173
|
if (key === "children" && (isFunction(p) || isObservable(p))) {
|
|
172
174
|
props[key] = useSelector(p, { skipCheck: true });
|
|
173
|
-
} else if (key.startsWith("$") || key.endsWith("$")) {
|
|
175
|
+
} else if (isReactiveKey || key.startsWith("$") || key.endsWith("$")) {
|
|
174
176
|
if (process.env.NODE_ENV === "development" && !didWarnProps && key.endsWith("$")) {
|
|
175
177
|
didWarnProps = true;
|
|
176
178
|
console.warn(
|
|
@@ -180,7 +182,7 @@ function createReactiveComponent(component, observe3, reactive2, bindKeys) {
|
|
|
180
182
|
)}. See https://legendapp.com/open-source/state/migrating for more details.`
|
|
181
183
|
);
|
|
182
184
|
}
|
|
183
|
-
const k = key.endsWith("$") ? key.slice(0, -1) : key.slice(1);
|
|
185
|
+
const k = isReactiveKey ? key : key.endsWith("$") ? key.slice(0, -1) : key.slice(1);
|
|
184
186
|
const bind = bindKeys == null ? void 0 : bindKeys[k];
|
|
185
187
|
const shouldBind = bind && isObservable(p);
|
|
186
188
|
propsOut[k] = shouldBind && (bind == null ? void 0 : bind.selector) ? bind.selector(propsOut, p) : useSelector(p);
|
|
@@ -198,7 +200,9 @@ function createReactiveComponent(component, observe3, reactive2, bindKeys) {
|
|
|
198
200
|
process.env.NODE_ENV === "development" ? handlerFn : useCallback(handlerFn, [props[bind.handler], bindKeys]);
|
|
199
201
|
}
|
|
200
202
|
}
|
|
201
|
-
|
|
203
|
+
if (!isReactiveKey) {
|
|
204
|
+
delete propsOut[key];
|
|
205
|
+
}
|
|
202
206
|
} else if (propsOut[key] === void 0) {
|
|
203
207
|
propsOut[key] = p;
|
|
204
208
|
}
|
|
@@ -235,11 +239,11 @@ function createReactiveComponent(component, observe3, reactive2, bindKeys) {
|
|
|
235
239
|
function observer(component) {
|
|
236
240
|
return createReactiveComponent(component, true);
|
|
237
241
|
}
|
|
238
|
-
function reactive(component, bindKeys) {
|
|
239
|
-
return createReactiveComponent(component, false, true, bindKeys);
|
|
242
|
+
function reactive(component, keys, bindKeys) {
|
|
243
|
+
return createReactiveComponent(component, false, true, keys, bindKeys);
|
|
240
244
|
}
|
|
241
|
-
function reactiveObserver(component, bindKeys) {
|
|
242
|
-
return createReactiveComponent(component, true, true, bindKeys);
|
|
245
|
+
function reactiveObserver(component, keys, bindKeys) {
|
|
246
|
+
return createReactiveComponent(component, true, true, keys, bindKeys);
|
|
243
247
|
}
|
|
244
248
|
function reactiveComponents(components) {
|
|
245
249
|
return new Proxy(
|
|
@@ -365,7 +369,7 @@ var Reactive = new Proxy(
|
|
|
365
369
|
}
|
|
366
370
|
return createElement(Component, propsOut);
|
|
367
371
|
});
|
|
368
|
-
target[p] = reactive(render, ReactiveFnBinders.get(p));
|
|
372
|
+
target[p] = reactive(render, [], ReactiveFnBinders.get(p));
|
|
369
373
|
}
|
|
370
374
|
return target[p];
|
|
371
375
|
}
|
package/sync-plugins/crud.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { WaitForSetFnParams, ObservableParam, ObservableEvent } from '@legendapp/state';
|
|
2
|
-
import { SyncedGetParams, SyncedOptions, SyncedSetParams, SyncedSubscribeParams } from '@legendapp/state/sync';
|
|
2
|
+
import { SyncedGetParams, SyncedErrorParams, SyncedOptions, SyncedSetParams, SyncedSubscribeParams } from '@legendapp/state/sync';
|
|
3
3
|
|
|
4
4
|
type CrudAsOption = 'Map' | 'object' | 'value' | 'array';
|
|
5
5
|
type CrudResult<T> = T;
|
|
@@ -25,7 +25,11 @@ interface SyncedCrudOnSavedParams<TRemote extends object, TLocal> {
|
|
|
25
25
|
interface WaitForSetCrudFnParams<T> extends WaitForSetFnParams<T> {
|
|
26
26
|
type: 'create' | 'update' | 'delete';
|
|
27
27
|
}
|
|
28
|
-
interface
|
|
28
|
+
interface CrudErrorParams extends Omit<SyncedErrorParams, 'source'> {
|
|
29
|
+
source: 'list' | 'get' | 'create' | 'update' | 'delete' | 'unknown';
|
|
30
|
+
}
|
|
31
|
+
type CrudOnErrorFn = (error: Error, params: CrudErrorParams) => void;
|
|
32
|
+
interface SyncedCrudPropsBase<TRemote extends object, TLocal = TRemote> extends Omit<SyncedOptions<TRemote, TLocal>, 'get' | 'set' | 'initial' | 'subscribe' | 'waitForSet' | 'onError'> {
|
|
29
33
|
create?(input: TRemote, params: SyncedSetParams<TRemote>): Promise<CrudResult<TRemote> | null | undefined | void>;
|
|
30
34
|
update?(input: Partial<TRemote>, params: SyncedSetParams<TRemote>): Promise<CrudResult<Partial<TRemote> | null | undefined | void>>;
|
|
31
35
|
delete?(input: TRemote, params: SyncedSetParams<TRemote>): Promise<any>;
|
|
@@ -40,6 +44,7 @@ interface SyncedCrudPropsBase<TRemote extends object, TLocal = TRemote> extends
|
|
|
40
44
|
generateId?: () => string | number;
|
|
41
45
|
subscribe?: (params: SyncedSubscribeParams<TRemote[]>) => (() => void) | void;
|
|
42
46
|
waitForSet?: ((params: WaitForSetCrudFnParams<TLocal>) => any) | Promise<any> | ObservableParam<any> | ObservableEvent;
|
|
47
|
+
onError?: (error: Error, params: CrudErrorParams) => void;
|
|
43
48
|
}
|
|
44
49
|
type InitialValue<TLocal, TAsOption extends CrudAsOption> = TAsOption extends 'Map' ? Map<string | number, TLocal> : TAsOption extends 'object' ? Record<string | number, TLocal> : TAsOption extends 'value' ? TLocal : TLocal[];
|
|
45
50
|
type SyncedCrudReturnType<TLocal, TAsOption extends CrudAsOption> = TAsOption extends 'Map' ? Map<TLocal extends {
|
|
@@ -51,4 +56,4 @@ declare function syncedCrud<TRemote extends object, TLocal = TRemote>(props: Syn
|
|
|
51
56
|
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
57
|
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>;
|
|
53
58
|
|
|
54
|
-
export { type CrudAsOption, type CrudResult, type SyncedCrudOnSavedParams, type SyncedCrudPropsBase, type SyncedCrudPropsMany, type SyncedCrudPropsSingle, type SyncedCrudReturnType, type WaitForSetCrudFnParams, syncedCrud };
|
|
59
|
+
export { type CrudAsOption, type CrudErrorParams, type CrudOnErrorFn, type CrudResult, type SyncedCrudOnSavedParams, type SyncedCrudPropsBase, type SyncedCrudPropsMany, type SyncedCrudPropsSingle, type SyncedCrudReturnType, type WaitForSetCrudFnParams, syncedCrud };
|
package/sync-plugins/crud.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { WaitForSetFnParams, ObservableParam, ObservableEvent } from '@legendapp/state';
|
|
2
|
-
import { SyncedGetParams, SyncedOptions, SyncedSetParams, SyncedSubscribeParams } from '@legendapp/state/sync';
|
|
2
|
+
import { SyncedGetParams, SyncedErrorParams, SyncedOptions, SyncedSetParams, SyncedSubscribeParams } from '@legendapp/state/sync';
|
|
3
3
|
|
|
4
4
|
type CrudAsOption = 'Map' | 'object' | 'value' | 'array';
|
|
5
5
|
type CrudResult<T> = T;
|
|
@@ -25,7 +25,11 @@ interface SyncedCrudOnSavedParams<TRemote extends object, TLocal> {
|
|
|
25
25
|
interface WaitForSetCrudFnParams<T> extends WaitForSetFnParams<T> {
|
|
26
26
|
type: 'create' | 'update' | 'delete';
|
|
27
27
|
}
|
|
28
|
-
interface
|
|
28
|
+
interface CrudErrorParams extends Omit<SyncedErrorParams, 'source'> {
|
|
29
|
+
source: 'list' | 'get' | 'create' | 'update' | 'delete' | 'unknown';
|
|
30
|
+
}
|
|
31
|
+
type CrudOnErrorFn = (error: Error, params: CrudErrorParams) => void;
|
|
32
|
+
interface SyncedCrudPropsBase<TRemote extends object, TLocal = TRemote> extends Omit<SyncedOptions<TRemote, TLocal>, 'get' | 'set' | 'initial' | 'subscribe' | 'waitForSet' | 'onError'> {
|
|
29
33
|
create?(input: TRemote, params: SyncedSetParams<TRemote>): Promise<CrudResult<TRemote> | null | undefined | void>;
|
|
30
34
|
update?(input: Partial<TRemote>, params: SyncedSetParams<TRemote>): Promise<CrudResult<Partial<TRemote> | null | undefined | void>>;
|
|
31
35
|
delete?(input: TRemote, params: SyncedSetParams<TRemote>): Promise<any>;
|
|
@@ -40,6 +44,7 @@ interface SyncedCrudPropsBase<TRemote extends object, TLocal = TRemote> extends
|
|
|
40
44
|
generateId?: () => string | number;
|
|
41
45
|
subscribe?: (params: SyncedSubscribeParams<TRemote[]>) => (() => void) | void;
|
|
42
46
|
waitForSet?: ((params: WaitForSetCrudFnParams<TLocal>) => any) | Promise<any> | ObservableParam<any> | ObservableEvent;
|
|
47
|
+
onError?: (error: Error, params: CrudErrorParams) => void;
|
|
43
48
|
}
|
|
44
49
|
type InitialValue<TLocal, TAsOption extends CrudAsOption> = TAsOption extends 'Map' ? Map<string | number, TLocal> : TAsOption extends 'object' ? Record<string | number, TLocal> : TAsOption extends 'value' ? TLocal : TLocal[];
|
|
45
50
|
type SyncedCrudReturnType<TLocal, TAsOption extends CrudAsOption> = TAsOption extends 'Map' ? Map<TLocal extends {
|
|
@@ -51,4 +56,4 @@ declare function syncedCrud<TRemote extends object, TLocal = TRemote>(props: Syn
|
|
|
51
56
|
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
57
|
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>;
|
|
53
58
|
|
|
54
|
-
export { type CrudAsOption, type CrudResult, type SyncedCrudOnSavedParams, type SyncedCrudPropsBase, type SyncedCrudPropsMany, type SyncedCrudPropsSingle, type SyncedCrudReturnType, type WaitForSetCrudFnParams, syncedCrud };
|
|
59
|
+
export { type CrudAsOption, type CrudErrorParams, type CrudOnErrorFn, type CrudResult, type SyncedCrudOnSavedParams, type SyncedCrudPropsBase, type SyncedCrudPropsMany, type SyncedCrudPropsSingle, type SyncedCrudReturnType, type WaitForSetCrudFnParams, syncedCrud };
|
package/sync-plugins/crud.js
CHANGED
|
@@ -5,7 +5,7 @@ var sync = require('@legendapp/state/sync');
|
|
|
5
5
|
|
|
6
6
|
// src/sync-plugins/crud.ts
|
|
7
7
|
var { clone } = state.internal;
|
|
8
|
-
var { waitForSet } = sync.internal;
|
|
8
|
+
var { waitForSet, runWithRetry } = sync.internal;
|
|
9
9
|
function transformOut(data, transform) {
|
|
10
10
|
return transform ? transform(clone(data)) : data;
|
|
11
11
|
}
|
|
@@ -25,6 +25,39 @@ function computeLastSync(data, fieldUpdatedAt, fieldCreatedAt) {
|
|
|
25
25
|
}
|
|
26
26
|
return newLastSync;
|
|
27
27
|
}
|
|
28
|
+
var queuedRetries = {
|
|
29
|
+
create: /* @__PURE__ */ new Map(),
|
|
30
|
+
update: /* @__PURE__ */ new Map(),
|
|
31
|
+
delete: /* @__PURE__ */ new Map()
|
|
32
|
+
};
|
|
33
|
+
function retrySet(params, retry, action, itemKey, itemValue, actionFn, saveResult) {
|
|
34
|
+
if (action === "delete") {
|
|
35
|
+
if (queuedRetries.create.has(itemKey)) {
|
|
36
|
+
queuedRetries.create.delete(itemKey);
|
|
37
|
+
}
|
|
38
|
+
if (queuedRetries.update.has(itemKey)) {
|
|
39
|
+
queuedRetries.update.delete(itemKey);
|
|
40
|
+
}
|
|
41
|
+
} else {
|
|
42
|
+
if (queuedRetries.delete.has(itemKey)) {
|
|
43
|
+
queuedRetries.delete.delete(itemKey);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const queuedRetry = queuedRetries[action].get(itemKey);
|
|
47
|
+
if (queuedRetry) {
|
|
48
|
+
itemValue = Object.assign(queuedRetry, itemValue);
|
|
49
|
+
}
|
|
50
|
+
queuedRetries[action].set(itemKey, itemValue);
|
|
51
|
+
return runWithRetry(
|
|
52
|
+
params,
|
|
53
|
+
retry,
|
|
54
|
+
"create_" + itemKey,
|
|
55
|
+
() => actionFn(itemValue, params).then((result) => {
|
|
56
|
+
queuedRetries[action].delete(itemKey);
|
|
57
|
+
return saveResult(itemKey, itemValue, result, true);
|
|
58
|
+
})
|
|
59
|
+
);
|
|
60
|
+
}
|
|
28
61
|
function syncedCrud(props) {
|
|
29
62
|
const {
|
|
30
63
|
get: getFn,
|
|
@@ -45,6 +78,7 @@ function syncedCrud(props) {
|
|
|
45
78
|
changesSince,
|
|
46
79
|
generateId,
|
|
47
80
|
waitForSet: waitForSetParam,
|
|
81
|
+
retry,
|
|
48
82
|
...rest
|
|
49
83
|
} = props;
|
|
50
84
|
const fieldId = fieldIdProp || "id";
|
|
@@ -77,63 +111,68 @@ function syncedCrud(props) {
|
|
|
77
111
|
return out;
|
|
78
112
|
};
|
|
79
113
|
const transformRows = (data) => {
|
|
80
|
-
return Promise.all(
|
|
114
|
+
return data.length ? Promise.all(
|
|
81
115
|
data.map(
|
|
82
116
|
(value) => (
|
|
83
117
|
// Skip transforming any children with symbolDelete or fieldDeleted because they'll get deleted by resultsToOutType
|
|
84
118
|
value[state.symbolDelete] || fieldDeleted && value[fieldDeleted] || fieldDeletedList && value[fieldDeletedList] ? value : transform.load(value, "get")
|
|
85
119
|
)
|
|
86
120
|
)
|
|
87
|
-
);
|
|
121
|
+
) : [];
|
|
88
122
|
};
|
|
89
123
|
const get = getFn || listFn ? (getParams) => {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const listPromise = listFn(getParams);
|
|
97
|
-
const toOut = (transformed) => {
|
|
98
|
-
var _a;
|
|
99
|
-
if (asType === "value") {
|
|
100
|
-
return transformed.length > 0 ? transformed[0] : (_a = (isLastSyncMode && lastSync || fieldDeleted) && value) != null ? _a : null;
|
|
101
|
-
} else {
|
|
102
|
-
return resultsToOutType(transformed);
|
|
124
|
+
return runWithRetry(getParams, retry, getFn || listFn, () => {
|
|
125
|
+
const { updateLastSync, lastSync, value } = getParams;
|
|
126
|
+
if (listFn) {
|
|
127
|
+
const isLastSyncMode = changesSince === "last-sync";
|
|
128
|
+
if (isLastSyncMode && lastSync) {
|
|
129
|
+
getParams.mode = modeParam || (asType === "array" ? "append" : asType === "value" ? "set" : "assign");
|
|
103
130
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
131
|
+
const listPromise = listFn(getParams);
|
|
132
|
+
const toOut = (transformed) => {
|
|
133
|
+
if (asType === "value") {
|
|
134
|
+
if (transformed.length > 0) {
|
|
135
|
+
return transformed[0];
|
|
136
|
+
} else {
|
|
137
|
+
return value ? void 0 : null;
|
|
138
|
+
}
|
|
139
|
+
} else {
|
|
140
|
+
return resultsToOutType(transformed);
|
|
111
141
|
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
} else if (getFn) {
|
|
121
|
-
const dataPromise = getFn(getParams);
|
|
122
|
-
const processData = (data) => {
|
|
123
|
-
let transformed = data;
|
|
124
|
-
if (data) {
|
|
125
|
-
const newLastSync = data[fieldUpdatedAt] || data[fieldCreatedAt];
|
|
126
|
-
if (newLastSync && newLastSync !== lastSync) {
|
|
127
|
-
updateLastSync(newLastSync);
|
|
142
|
+
};
|
|
143
|
+
const processResults = (data) => {
|
|
144
|
+
data || (data = []);
|
|
145
|
+
if (fieldUpdatedAt) {
|
|
146
|
+
const newLastSync = computeLastSync(data, fieldUpdatedAt, fieldCreatedAt);
|
|
147
|
+
if (newLastSync && newLastSync !== lastSync) {
|
|
148
|
+
updateLastSync(newLastSync);
|
|
149
|
+
}
|
|
128
150
|
}
|
|
151
|
+
let transformed = data;
|
|
129
152
|
if (transform == null ? void 0 : transform.load) {
|
|
130
|
-
transformed =
|
|
153
|
+
transformed = transformRows(data);
|
|
131
154
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
155
|
+
return state.isPromise(transformed) ? transformed.then(toOut) : toOut(transformed);
|
|
156
|
+
};
|
|
157
|
+
return state.isPromise(listPromise) ? listPromise.then(processResults) : processResults(listPromise);
|
|
158
|
+
} else if (getFn) {
|
|
159
|
+
const dataPromise = getFn(getParams);
|
|
160
|
+
const processData = (data) => {
|
|
161
|
+
let transformed = data;
|
|
162
|
+
if (data) {
|
|
163
|
+
const newLastSync = data[fieldUpdatedAt] || data[fieldCreatedAt];
|
|
164
|
+
if (newLastSync && newLastSync !== lastSync) {
|
|
165
|
+
updateLastSync(newLastSync);
|
|
166
|
+
}
|
|
167
|
+
if (transform == null ? void 0 : transform.load) {
|
|
168
|
+
transformed = transform.load(data, "get");
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return transformed;
|
|
172
|
+
};
|
|
173
|
+
return state.isPromise(dataPromise) ? dataPromise.then(processData) : processData(dataPromise);
|
|
174
|
+
}
|
|
175
|
+
});
|
|
137
176
|
} : void 0;
|
|
138
177
|
const set = createFn || updateFn || deleteFn ? async (params) => {
|
|
139
178
|
const { value, changes, update, retryAsCreate, node } = params;
|
|
@@ -285,59 +324,84 @@ function syncedCrud(props) {
|
|
|
285
324
|
delete saved[key];
|
|
286
325
|
}
|
|
287
326
|
});
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
327
|
+
let value2;
|
|
328
|
+
if (asType === "array") {
|
|
329
|
+
const index = currentPeeked.findIndex(
|
|
330
|
+
(cur) => cur[fieldId] === itemKey
|
|
331
|
+
);
|
|
332
|
+
if (index < 0) {
|
|
333
|
+
console.warn("[legend-state] Item saved that does not exist in array", saved);
|
|
334
|
+
} else {
|
|
335
|
+
value2 = { [index < 0 ? 0 : index]: saved };
|
|
336
|
+
}
|
|
337
|
+
} else {
|
|
338
|
+
value2 = itemKey !== "undefined" && asType !== "value" ? { [itemKey]: saved } : saved;
|
|
339
|
+
}
|
|
340
|
+
if (value2 !== void 0) {
|
|
341
|
+
update({
|
|
342
|
+
value: value2,
|
|
343
|
+
mode: "merge"
|
|
344
|
+
});
|
|
345
|
+
}
|
|
296
346
|
}
|
|
297
347
|
}
|
|
298
348
|
};
|
|
299
349
|
return Promise.all([
|
|
350
|
+
// Handle creates
|
|
300
351
|
...Array.from(creates).map(async ([itemKey, itemValue]) => {
|
|
301
352
|
if (waitForSetParam) {
|
|
302
353
|
await waitForSet(waitForSetParam, changes, itemValue, { type: "create" });
|
|
303
354
|
}
|
|
304
355
|
const createObj = await transformOut(itemValue, transform == null ? void 0 : transform.save);
|
|
305
|
-
return
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
356
|
+
return retrySet(params, retry, "create", itemKey, createObj, createFn, saveResult).then(
|
|
357
|
+
() => {
|
|
358
|
+
pendingCreates.delete(itemKey);
|
|
359
|
+
}
|
|
360
|
+
);
|
|
310
361
|
}),
|
|
362
|
+
// Handle updates
|
|
311
363
|
...Array.from(updates).map(async ([itemKey, itemValue]) => {
|
|
312
364
|
if (waitForSetParam) {
|
|
313
365
|
await waitForSet(waitForSetParam, changes, itemValue, { type: "update" });
|
|
314
366
|
}
|
|
315
|
-
const
|
|
316
|
-
const changed = await transformOut(toSave, transform == null ? void 0 : transform.save);
|
|
367
|
+
const changed = await transformOut(itemValue, transform == null ? void 0 : transform.save);
|
|
317
368
|
if (Object.keys(changed).length > 0) {
|
|
318
|
-
return
|
|
319
|
-
(result) => result && saveResult(itemKey, changed, result, false)
|
|
320
|
-
);
|
|
369
|
+
return retrySet(params, retry, "update", itemKey, changed, updateFn, saveResult);
|
|
321
370
|
}
|
|
322
371
|
}),
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
372
|
+
// Handle deletes
|
|
373
|
+
...Array.from(deletes).filter((val) => val !== state.symbolDelete).map(async (valuePrevious) => {
|
|
374
|
+
if (waitForSetParam) {
|
|
375
|
+
await waitForSet(waitForSetParam, changes, valuePrevious, { type: "delete" });
|
|
376
|
+
}
|
|
377
|
+
const valueId = valuePrevious[fieldId];
|
|
378
|
+
if (!valueId) {
|
|
379
|
+
console.error("[legend-state]: deleting item without an id");
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
if (deleteFn) {
|
|
383
|
+
return retrySet(
|
|
384
|
+
params,
|
|
385
|
+
retry,
|
|
386
|
+
"delete",
|
|
387
|
+
valueId,
|
|
388
|
+
valuePrevious,
|
|
389
|
+
deleteFn,
|
|
390
|
+
saveResult
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
if (fieldDeleted && updateFn) {
|
|
394
|
+
return retrySet(
|
|
395
|
+
params,
|
|
396
|
+
retry,
|
|
397
|
+
"delete",
|
|
398
|
+
valueId,
|
|
399
|
+
{ [fieldId]: valueId, [fieldDeleted]: true },
|
|
400
|
+
updateFn,
|
|
401
|
+
saveResult
|
|
402
|
+
);
|
|
340
403
|
}
|
|
404
|
+
console.warn("[legend-state] missing delete function");
|
|
341
405
|
})
|
|
342
406
|
]);
|
|
343
407
|
} : void 0;
|