@legendapp/state 3.0.0-beta.13 → 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/index.d.mts +5 -1
- package/index.d.ts +5 -1
- package/index.js +6 -4
- package/index.mjs +6 -4
- package/package.json +1 -1
- package/react.d.mts +3 -3
- package/react.d.ts +3 -3
- 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/.DS_Store
CHANGED
|
Binary file
|
package/index.d.mts
CHANGED
|
@@ -249,7 +249,11 @@ interface UpdateFnParams<T = any> {
|
|
|
249
249
|
mode?: GetMode;
|
|
250
250
|
lastSync?: number | undefined;
|
|
251
251
|
}
|
|
252
|
+
interface UpdateSetFnParams<T = any> extends UpdateFnParams<T> {
|
|
253
|
+
lastSync?: never;
|
|
254
|
+
}
|
|
252
255
|
type UpdateFn<T = any> = (params: UpdateFnParams<T>) => void;
|
|
256
|
+
type UpdateSetFn<T = any> = (params: UpdateSetFnParams<T>) => void;
|
|
253
257
|
type Linked<T> = T;
|
|
254
258
|
interface ObserveOptions {
|
|
255
259
|
immediate?: boolean;
|
|
@@ -429,4 +433,4 @@ declare const internal: {
|
|
|
429
433
|
};
|
|
430
434
|
};
|
|
431
435
|
|
|
432
|
-
export { type ArrayValue, type Change, type ChildNodeInfo, type ClassConstructor, type GetMode, type GetOptions, type ImmutableObservableBase, type Linked, type LinkedOptions, type ListenerFn, type ListenerParams, type NodeInfo, type NodeListener, type NotPrimitive, type Observable, type ObservableBoolean, type ObservableEvent, ObservableHint, type ObservableListenerDispose, type ObservableMap, type ObservableObject, type ObservableObjectFns, type ObservableParam, type ObservablePrimitive, type ObservableRoot, type ObservableState, type ObservableSyncState, type ObservableSyncStateBase, type ObservableValue, type ObserveEvent, type ObserveEventCallback, type ObserveOptions, type OpaqueObject, type PlainObject, type Primitive, type RecordValue, type RecursiveValueOrFunction, type RemoveObservables, type RetryOptions, type RootNodeInfo, type Selector, type SetParams, type TrackingNode, type TrackingState, type TrackingType, type TypeAtPath, type UpdateFn, type UpdateFnParams, type WaitForSet, type WaitForSetFnParams, applyChange, applyChanges, batch, beginBatch, computeSelector, computed, constructObjectWithPath, deconstructObjectWithPath, endBatch, event, getObservableIndex, hasOwnProperty, internal, isArray, isBoolean, isDate, isEmpty, isFunction, isMap, isNullOrUndefined, isNumber, isObject, isObservable, isObservableValueReady, isObserved, isPlainObject, isPrimitive, isPromise, isSet, isString, isSymbol, linked, mergeIntoObservable, observable, observablePrimitive, observe, opaqueObject, proxy, setAtPath, setSilently, shouldIgnoreUnobserved, syncState, trackSelector, when, whenReady };
|
|
436
|
+
export { type ArrayValue, type Change, type ChildNodeInfo, type ClassConstructor, type GetMode, type GetOptions, type ImmutableObservableBase, type Linked, type LinkedOptions, type ListenerFn, type ListenerParams, type NodeInfo, type NodeListener, type NotPrimitive, type Observable, type ObservableBoolean, type ObservableEvent, ObservableHint, type ObservableListenerDispose, type ObservableMap, type ObservableObject, type ObservableObjectFns, type ObservableParam, type ObservablePrimitive, type ObservableRoot, type ObservableState, type ObservableSyncState, type ObservableSyncStateBase, type ObservableValue, type ObserveEvent, type ObserveEventCallback, type ObserveOptions, type OpaqueObject, type PlainObject, type Primitive, type RecordValue, type RecursiveValueOrFunction, type RemoveObservables, type RetryOptions, type RootNodeInfo, type Selector, type SetParams, type TrackingNode, type TrackingState, type TrackingType, type TypeAtPath, type UpdateFn, type UpdateFnParams, type UpdateSetFn, type UpdateSetFnParams, type WaitForSet, type WaitForSetFnParams, applyChange, applyChanges, batch, beginBatch, computeSelector, computed, constructObjectWithPath, deconstructObjectWithPath, endBatch, event, getObservableIndex, hasOwnProperty, internal, isArray, isBoolean, isDate, isEmpty, isFunction, isMap, isNullOrUndefined, isNumber, isObject, isObservable, isObservableValueReady, isObserved, isPlainObject, isPrimitive, isPromise, isSet, isString, isSymbol, linked, mergeIntoObservable, observable, observablePrimitive, observe, opaqueObject, proxy, setAtPath, setSilently, shouldIgnoreUnobserved, syncState, trackSelector, when, whenReady };
|
package/index.d.ts
CHANGED
|
@@ -249,7 +249,11 @@ interface UpdateFnParams<T = any> {
|
|
|
249
249
|
mode?: GetMode;
|
|
250
250
|
lastSync?: number | undefined;
|
|
251
251
|
}
|
|
252
|
+
interface UpdateSetFnParams<T = any> extends UpdateFnParams<T> {
|
|
253
|
+
lastSync?: never;
|
|
254
|
+
}
|
|
252
255
|
type UpdateFn<T = any> = (params: UpdateFnParams<T>) => void;
|
|
256
|
+
type UpdateSetFn<T = any> = (params: UpdateSetFnParams<T>) => void;
|
|
253
257
|
type Linked<T> = T;
|
|
254
258
|
interface ObserveOptions {
|
|
255
259
|
immediate?: boolean;
|
|
@@ -429,4 +433,4 @@ declare const internal: {
|
|
|
429
433
|
};
|
|
430
434
|
};
|
|
431
435
|
|
|
432
|
-
export { type ArrayValue, type Change, type ChildNodeInfo, type ClassConstructor, type GetMode, type GetOptions, type ImmutableObservableBase, type Linked, type LinkedOptions, type ListenerFn, type ListenerParams, type NodeInfo, type NodeListener, type NotPrimitive, type Observable, type ObservableBoolean, type ObservableEvent, ObservableHint, type ObservableListenerDispose, type ObservableMap, type ObservableObject, type ObservableObjectFns, type ObservableParam, type ObservablePrimitive, type ObservableRoot, type ObservableState, type ObservableSyncState, type ObservableSyncStateBase, type ObservableValue, type ObserveEvent, type ObserveEventCallback, type ObserveOptions, type OpaqueObject, type PlainObject, type Primitive, type RecordValue, type RecursiveValueOrFunction, type RemoveObservables, type RetryOptions, type RootNodeInfo, type Selector, type SetParams, type TrackingNode, type TrackingState, type TrackingType, type TypeAtPath, type UpdateFn, type UpdateFnParams, type WaitForSet, type WaitForSetFnParams, applyChange, applyChanges, batch, beginBatch, computeSelector, computed, constructObjectWithPath, deconstructObjectWithPath, endBatch, event, getObservableIndex, hasOwnProperty, internal, isArray, isBoolean, isDate, isEmpty, isFunction, isMap, isNullOrUndefined, isNumber, isObject, isObservable, isObservableValueReady, isObserved, isPlainObject, isPrimitive, isPromise, isSet, isString, isSymbol, linked, mergeIntoObservable, observable, observablePrimitive, observe, opaqueObject, proxy, setAtPath, setSilently, shouldIgnoreUnobserved, syncState, trackSelector, when, whenReady };
|
|
436
|
+
export { type ArrayValue, type Change, type ChildNodeInfo, type ClassConstructor, type GetMode, type GetOptions, type ImmutableObservableBase, type Linked, type LinkedOptions, type ListenerFn, type ListenerParams, type NodeInfo, type NodeListener, type NotPrimitive, type Observable, type ObservableBoolean, type ObservableEvent, ObservableHint, type ObservableListenerDispose, type ObservableMap, type ObservableObject, type ObservableObjectFns, type ObservableParam, type ObservablePrimitive, type ObservableRoot, type ObservableState, type ObservableSyncState, type ObservableSyncStateBase, type ObservableValue, type ObserveEvent, type ObserveEventCallback, type ObserveOptions, type OpaqueObject, type PlainObject, type Primitive, type RecordValue, type RecursiveValueOrFunction, type RemoveObservables, type RetryOptions, type RootNodeInfo, type Selector, type SetParams, type TrackingNode, type TrackingState, type TrackingType, type TypeAtPath, type UpdateFn, type UpdateFnParams, type UpdateSetFn, type UpdateSetFnParams, type WaitForSet, type WaitForSetFnParams, applyChange, applyChanges, batch, beginBatch, computeSelector, computed, constructObjectWithPath, deconstructObjectWithPath, endBatch, event, getObservableIndex, hasOwnProperty, internal, isArray, isBoolean, isDate, isEmpty, isFunction, isMap, isNullOrUndefined, isNumber, isObject, isObservable, isObservableValueReady, isObserved, isPlainObject, isPrimitive, isPromise, isSet, isString, isSymbol, linked, mergeIntoObservable, observable, observablePrimitive, observe, opaqueObject, proxy, setAtPath, setSilently, shouldIgnoreUnobserved, syncState, trackSelector, when, whenReady };
|
package/index.js
CHANGED
|
@@ -1173,8 +1173,8 @@ function collectionSetter(node, target, prop, ...args) {
|
|
|
1173
1173
|
return ret;
|
|
1174
1174
|
}
|
|
1175
1175
|
}
|
|
1176
|
-
function getKeys(obj, isArr, isMap2) {
|
|
1177
|
-
return isArr ? void 0 : obj ? isMap2 ? Array.from(obj.keys()) : Object.keys(obj) : [];
|
|
1176
|
+
function getKeys(obj, isArr, isMap2, isSet2) {
|
|
1177
|
+
return isArr ? void 0 : obj ? isSet2 ? Array.from(obj) : isMap2 ? Array.from(obj.keys()) : Object.keys(obj) : [];
|
|
1178
1178
|
}
|
|
1179
1179
|
function updateNodes(parent, obj, prevValue) {
|
|
1180
1180
|
var _a, _b, _c;
|
|
@@ -1204,9 +1204,11 @@ function updateNodes(parent, obj, prevValue) {
|
|
|
1204
1204
|
let prevChildrenById;
|
|
1205
1205
|
let moved;
|
|
1206
1206
|
const isCurMap = isMap(obj);
|
|
1207
|
+
const isCurSet = isSet(obj);
|
|
1207
1208
|
const isPrevMap = isMap(prevValue);
|
|
1208
|
-
const
|
|
1209
|
-
const
|
|
1209
|
+
const isPrevSet = isSet(prevValue);
|
|
1210
|
+
const keys = getKeys(obj, isArr, isCurMap, isCurSet);
|
|
1211
|
+
const keysPrev = getKeys(prevValue, isArr, isPrevMap, isPrevSet);
|
|
1210
1212
|
const length = ((_a = keys || obj) == null ? void 0 : _a.length) || 0;
|
|
1211
1213
|
const lengthPrev = ((_b = keysPrev || prevValue) == null ? void 0 : _b.length) || 0;
|
|
1212
1214
|
let idField;
|
package/index.mjs
CHANGED
|
@@ -1171,8 +1171,8 @@ function collectionSetter(node, target, prop, ...args) {
|
|
|
1171
1171
|
return ret;
|
|
1172
1172
|
}
|
|
1173
1173
|
}
|
|
1174
|
-
function getKeys(obj, isArr, isMap2) {
|
|
1175
|
-
return isArr ? void 0 : obj ? isMap2 ? Array.from(obj.keys()) : Object.keys(obj) : [];
|
|
1174
|
+
function getKeys(obj, isArr, isMap2, isSet2) {
|
|
1175
|
+
return isArr ? void 0 : obj ? isSet2 ? Array.from(obj) : isMap2 ? Array.from(obj.keys()) : Object.keys(obj) : [];
|
|
1176
1176
|
}
|
|
1177
1177
|
function updateNodes(parent, obj, prevValue) {
|
|
1178
1178
|
var _a, _b, _c;
|
|
@@ -1202,9 +1202,11 @@ function updateNodes(parent, obj, prevValue) {
|
|
|
1202
1202
|
let prevChildrenById;
|
|
1203
1203
|
let moved;
|
|
1204
1204
|
const isCurMap = isMap(obj);
|
|
1205
|
+
const isCurSet = isSet(obj);
|
|
1205
1206
|
const isPrevMap = isMap(prevValue);
|
|
1206
|
-
const
|
|
1207
|
-
const
|
|
1207
|
+
const isPrevSet = isSet(prevValue);
|
|
1208
|
+
const keys = getKeys(obj, isArr, isCurMap, isCurSet);
|
|
1209
|
+
const keysPrev = getKeys(prevValue, isArr, isPrevMap, isPrevSet);
|
|
1208
1210
|
const length = ((_a = keys || obj) == null ? void 0 : _a.length) || 0;
|
|
1209
1211
|
const lengthPrev = ((_b = keysPrev || prevValue) == null ? void 0 : _b.length) || 0;
|
|
1210
1212
|
let idField;
|
package/package.json
CHANGED
package/react.d.mts
CHANGED
|
@@ -54,11 +54,11 @@ declare function Switch<T extends object>({ value, children, }: {
|
|
|
54
54
|
children: Partial<Record<keyof T | 'null' | 'undefined' | 'default', () => ReactNode>>;
|
|
55
55
|
}): ReactElement | null;
|
|
56
56
|
declare function Switch<T extends string | number | symbol>({ value, children, }: {
|
|
57
|
-
value?: Selector<T>;
|
|
57
|
+
value?: Selector<T | undefined | null>;
|
|
58
58
|
children: Partial<Record<T | 'null' | 'undefined' | 'default', () => ReactNode>>;
|
|
59
59
|
}): ReactElement | null;
|
|
60
60
|
declare function Switch<T extends boolean>({ value, children, }: {
|
|
61
|
-
value?: Selector<T>;
|
|
61
|
+
value?: Selector<T | undefined | null>;
|
|
62
62
|
children: Partial<Record<'false' | 'true' | 'null' | 'undefined' | 'default', () => ReactNode>>;
|
|
63
63
|
}): ReactElement | null;
|
|
64
64
|
declare function Switch<T>({ value, children, }: {
|
|
@@ -116,7 +116,7 @@ declare function reactiveObserver<T extends object, K extends keyof T>(component
|
|
|
116
116
|
declare function reactiveObserver<T extends object, K extends keyof T>(component: React.ForwardRefExoticComponent<T>, keys: K[] | (keyof T)[], bindKeys?: BindKeys<T, K>): React.ForwardRefExoticComponent<ReactifyProps<T, K>>;
|
|
117
117
|
declare function reactiveObserver<T extends object>(component: React.FC<T>): React.FC<ShapeWith$<T>>;
|
|
118
118
|
declare function reactiveObserver<T extends object>(component: React.ForwardRefExoticComponent<T>): React.ForwardRefExoticComponent<ShapeWith$<T>>;
|
|
119
|
-
declare function reactiveComponents<P extends Record<string,
|
|
119
|
+
declare function reactiveComponents<P extends Record<string, any>>(components: P): ObjectShapeWith$<P>;
|
|
120
120
|
|
|
121
121
|
declare function useComputed<T>(get: () => T | Promise<T>): Observable<T>;
|
|
122
122
|
declare function useComputed<T>(get: () => T | Promise<T>, deps: any[]): Observable<T>;
|
package/react.d.ts
CHANGED
|
@@ -54,11 +54,11 @@ declare function Switch<T extends object>({ value, children, }: {
|
|
|
54
54
|
children: Partial<Record<keyof T | 'null' | 'undefined' | 'default', () => ReactNode>>;
|
|
55
55
|
}): ReactElement | null;
|
|
56
56
|
declare function Switch<T extends string | number | symbol>({ value, children, }: {
|
|
57
|
-
value?: Selector<T>;
|
|
57
|
+
value?: Selector<T | undefined | null>;
|
|
58
58
|
children: Partial<Record<T | 'null' | 'undefined' | 'default', () => ReactNode>>;
|
|
59
59
|
}): ReactElement | null;
|
|
60
60
|
declare function Switch<T extends boolean>({ value, children, }: {
|
|
61
|
-
value?: Selector<T>;
|
|
61
|
+
value?: Selector<T | undefined | null>;
|
|
62
62
|
children: Partial<Record<'false' | 'true' | 'null' | 'undefined' | 'default', () => ReactNode>>;
|
|
63
63
|
}): ReactElement | null;
|
|
64
64
|
declare function Switch<T>({ value, children, }: {
|
|
@@ -116,7 +116,7 @@ declare function reactiveObserver<T extends object, K extends keyof T>(component
|
|
|
116
116
|
declare function reactiveObserver<T extends object, K extends keyof T>(component: React.ForwardRefExoticComponent<T>, keys: K[] | (keyof T)[], bindKeys?: BindKeys<T, K>): React.ForwardRefExoticComponent<ReactifyProps<T, K>>;
|
|
117
117
|
declare function reactiveObserver<T extends object>(component: React.FC<T>): React.FC<ShapeWith$<T>>;
|
|
118
118
|
declare function reactiveObserver<T extends object>(component: React.ForwardRefExoticComponent<T>): React.ForwardRefExoticComponent<ShapeWith$<T>>;
|
|
119
|
-
declare function reactiveComponents<P extends Record<string,
|
|
119
|
+
declare function reactiveComponents<P extends Record<string, any>>(components: P): ObjectShapeWith$<P>;
|
|
120
120
|
|
|
121
121
|
declare function useComputed<T>(get: () => T | Promise<T>): Observable<T>;
|
|
122
122
|
declare function useComputed<T>(get: () => T | Promise<T>, deps: any[]): Observable<T>;
|
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;
|