@legendapp/state 3.0.0-alpha.1 → 3.0.0-alpha.3
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/CHANGELOG.md +1 -831
- package/LICENSE +1 -21
- package/README.md +1 -141
- package/as/arrayAsRecord.d.mts +5 -0
- package/as/arrayAsRecord.d.ts +5 -0
- package/as/arrayAsRecord.js +28 -0
- package/as/arrayAsRecord.mjs +26 -0
- package/as/arrayAsSet.d.mts +5 -0
- package/as/arrayAsSet.d.ts +5 -0
- package/as/arrayAsSet.js +13 -0
- package/as/arrayAsSet.mjs +11 -0
- package/as/arrayAsString.d.mts +5 -0
- package/as/arrayAsString.d.ts +5 -0
- package/as/arrayAsString.js +13 -0
- package/as/arrayAsString.mjs +11 -0
- package/as/numberAsString.d.mts +5 -0
- package/as/numberAsString.d.ts +5 -0
- package/as/numberAsString.js +13 -0
- package/as/numberAsString.mjs +11 -0
- package/as/recordAsArray.d.mts +5 -0
- package/as/recordAsArray.d.ts +5 -0
- package/as/recordAsArray.js +25 -0
- package/as/recordAsArray.mjs +23 -0
- package/as/recordAsString.d.mts +5 -0
- package/as/recordAsString.d.ts +5 -0
- package/as/recordAsString.js +13 -0
- package/as/recordAsString.mjs +11 -0
- package/as/setAsArray.d.mts +5 -0
- package/as/setAsArray.d.ts +5 -0
- package/as/setAsArray.js +13 -0
- package/as/setAsArray.mjs +11 -0
- package/as/setAsString.d.mts +5 -0
- package/as/setAsString.d.ts +5 -0
- package/as/setAsString.js +13 -0
- package/as/setAsString.mjs +11 -0
- package/as/stringAsArray.d.mts +5 -0
- package/as/stringAsArray.d.ts +5 -0
- package/as/stringAsArray.js +13 -0
- package/as/stringAsArray.mjs +11 -0
- package/as/stringAsNumber.d.mts +5 -0
- package/as/stringAsNumber.d.ts +5 -0
- package/as/stringAsNumber.js +16 -0
- package/as/stringAsNumber.mjs +14 -0
- package/as/stringAsRecord.d.mts +5 -0
- package/as/stringAsRecord.d.ts +5 -0
- package/as/stringAsRecord.js +15 -0
- package/as/stringAsRecord.mjs +13 -0
- package/as/stringAsSet.d.mts +5 -0
- package/as/stringAsSet.d.ts +5 -0
- package/as/stringAsSet.js +13 -0
- package/as/stringAsSet.mjs +11 -0
- package/babel.d.mts +21 -0
- package/babel.d.ts +21 -2
- package/babel.js +57 -53
- package/babel.mjs +65 -0
- package/config/enable$GetSet.js +13 -14
- package/config/enable$GetSet.mjs +13 -14
- package/config/enableReactComponents.d.mts +9 -0
- package/config/enableReactComponents.d.ts +4 -2
- package/config/enableReactComponents.js +13 -10
- package/config/enableReactComponents.mjs +13 -10
- package/config/enableReactNativeComponents.d.mts +22 -0
- package/config/enableReactNativeComponents.d.ts +6 -4
- package/config/enableReactNativeComponents.js +43 -47
- package/config/enableReactNativeComponents.mjs +43 -47
- package/config/enableReactTracking.d.mts +7 -0
- package/config/enableReactTracking.d.ts +3 -2
- package/config/enableReactTracking.js +33 -38
- package/config/enableReactTracking.mjs +33 -38
- package/config/enableReactUse.d.mts +10 -0
- package/config/enableReactUse.d.ts +4 -1
- package/config/enableReactUse.js +15 -14
- package/config/enableReactUse.mjs +15 -14
- package/config/{enable$GetSet.d.ts → enable_GetSet.d.mts} +4 -2
- package/config/enable_GetSet.d.ts +10 -0
- package/config/enable_PeekAssign.d.mts +10 -0
- package/config/enable_PeekAssign.d.ts +4 -2
- package/config/enable_PeekAssign.js +13 -14
- package/config/enable_PeekAssign.mjs +13 -14
- package/helpers/pageHash.d.mts +9 -0
- package/helpers/pageHash.d.ts +2 -0
- package/helpers/pageHash.js +25 -30
- package/helpers/pageHash.mjs +25 -30
- package/helpers/pageHashParams.d.mts +9 -0
- package/helpers/pageHashParams.d.ts +2 -0
- package/helpers/pageHashParams.js +34 -37
- package/helpers/pageHashParams.mjs +34 -37
- package/helpers/time.d.mts +6 -0
- package/helpers/time.d.ts +6 -3
- package/helpers/time.js +17 -17
- package/helpers/time.mjs +17 -17
- package/helpers/trackHistory.d.mts +6 -0
- package/helpers/trackHistory.d.ts +4 -2
- package/helpers/trackHistory.js +13 -16
- package/helpers/trackHistory.mjs +13 -16
- package/helpers/undoRedo.d.mts +37 -0
- package/helpers/undoRedo.d.ts +5 -3
- package/helpers/undoRedo.js +59 -94
- package/helpers/undoRedo.mjs +59 -94
- package/index.d.mts +404 -0
- package/index.d.ts +371 -28
- package/index.js +2015 -2166
- package/index.mjs +2015 -2166
- package/package.json +254 -195
- package/persist-plugins/async-storage.d.mts +18 -0
- package/persist-plugins/async-storage.d.ts +6 -3
- package/persist-plugins/async-storage.js +79 -86
- package/persist-plugins/async-storage.mjs +79 -86
- package/persist-plugins/indexeddb.d.mts +29 -0
- package/persist-plugins/indexeddb.d.ts +6 -3
- package/persist-plugins/indexeddb.js +331 -352
- package/persist-plugins/indexeddb.mjs +331 -352
- package/persist-plugins/local-storage.d.mts +23 -0
- package/persist-plugins/local-storage.d.ts +8 -5
- package/persist-plugins/local-storage.js +74 -76
- package/persist-plugins/local-storage.mjs +74 -76
- package/persist-plugins/mmkv.d.mts +18 -0
- package/persist-plugins/mmkv.d.ts +6 -3
- package/persist-plugins/mmkv.js +82 -86
- package/persist-plugins/mmkv.mjs +82 -86
- package/react-hooks/createObservableHook.d.mts +5 -0
- package/react-hooks/createObservableHook.d.ts +4 -1
- package/react-hooks/createObservableHook.js +29 -30
- package/react-hooks/createObservableHook.mjs +25 -30
- package/react-hooks/useHover.d.mts +5 -0
- package/react-hooks/useHover.d.ts +5 -3
- package/react-hooks/useHover.js +29 -29
- package/react-hooks/useHover.mjs +29 -29
- package/react-hooks/useMeasure.d.mts +9 -0
- package/react-hooks/useMeasure.d.ts +5 -2
- package/react-hooks/useMeasure.js +30 -32
- package/react-hooks/useMeasure.mjs +30 -32
- package/react-hooks/useObservableNextRouter.d.mts +35 -0
- package/react-hooks/useObservableNextRouter.d.ts +9 -7
- package/react-hooks/useObservableNextRouter.js +64 -77
- package/react-hooks/useObservableNextRouter.mjs +60 -77
- package/react.d.mts +157 -0
- package/react.d.ts +157 -21
- package/react.js +458 -749
- package/react.mjs +457 -752
- package/sync-plugins/crud.d.mts +54 -0
- package/sync-plugins/crud.d.ts +12 -10
- package/sync-plugins/crud.js +253 -270
- package/sync-plugins/crud.mjs +253 -270
- package/sync-plugins/fetch.d.mts +21 -0
- package/sync-plugins/fetch.d.ts +7 -4
- package/sync-plugins/fetch.js +50 -37
- package/sync-plugins/fetch.mjs +50 -37
- package/sync-plugins/keel.d.mts +108 -0
- package/sync-plugins/keel.d.ts +17 -15
- package/sync-plugins/keel.js +229 -462
- package/sync-plugins/keel.mjs +227 -464
- package/sync-plugins/supabase.d.mts +39 -0
- package/sync-plugins/supabase.d.ts +16 -14
- package/sync-plugins/supabase.js +128 -128
- package/sync-plugins/supabase.mjs +128 -128
- package/sync-plugins/tanstack-query.d.mts +14 -0
- package/sync-plugins/tanstack-query.d.ts +7 -4
- package/sync-plugins/tanstack-query.js +51 -57
- package/sync-plugins/tanstack-query.mjs +51 -57
- package/sync-plugins/tanstack-react-query.d.mts +8 -0
- package/sync-plugins/tanstack-react-query.d.ts +6 -1
- package/sync-plugins/tanstack-react-query.js +2 -2
- package/sync-plugins/tanstack-react-query.mjs +2 -2
- package/sync.d.mts +351 -0
- package/sync.d.ts +349 -9
- package/sync.js +910 -964
- package/sync.mjs +920 -974
- package/trace.d.mts +9 -0
- package/trace.d.ts +9 -4
- package/trace.js +72 -62
- package/trace.mjs +72 -62
- package/types/babel.d.ts +1 -12
- package/babel.js.map +0 -1
- package/config/enable$GetSet.js.map +0 -1
- package/config/enable$GetSet.mjs.map +0 -1
- package/config/enableReactComponents.js.map +0 -1
- package/config/enableReactComponents.mjs.map +0 -1
- package/config/enableReactNativeComponents.js.map +0 -1
- package/config/enableReactNativeComponents.mjs.map +0 -1
- package/config/enableReactTracking.js.map +0 -1
- package/config/enableReactTracking.mjs.map +0 -1
- package/config/enableReactUse.js.map +0 -1
- package/config/enableReactUse.mjs.map +0 -1
- package/config/enable_PeekAssign.js.map +0 -1
- package/config/enable_PeekAssign.mjs.map +0 -1
- package/helpers/pageHash.js.map +0 -1
- package/helpers/pageHash.mjs.map +0 -1
- package/helpers/pageHashParams.js.map +0 -1
- package/helpers/pageHashParams.mjs.map +0 -1
- package/helpers/time.js.map +0 -1
- package/helpers/time.mjs.map +0 -1
- package/helpers/trackHistory.js.map +0 -1
- package/helpers/trackHistory.mjs.map +0 -1
- package/helpers/undoRedo.js.map +0 -1
- package/helpers/undoRedo.mjs.map +0 -1
- package/history.d.ts +0 -1
- package/history.js +0 -24
- package/history.js.map +0 -1
- package/history.mjs +0 -22
- package/history.mjs.map +0 -1
- package/index.js.map +0 -1
- package/index.mjs.map +0 -1
- package/persist-plugins/async-storage.js.map +0 -1
- package/persist-plugins/async-storage.mjs.map +0 -1
- package/persist-plugins/indexeddb.js.map +0 -1
- package/persist-plugins/indexeddb.mjs.map +0 -1
- package/persist-plugins/local-storage.js.map +0 -1
- package/persist-plugins/local-storage.mjs.map +0 -1
- package/persist-plugins/mmkv.js.map +0 -1
- package/persist-plugins/mmkv.mjs.map +0 -1
- package/react-hooks/createObservableHook.js.map +0 -1
- package/react-hooks/createObservableHook.mjs.map +0 -1
- package/react-hooks/useHover.js.map +0 -1
- package/react-hooks/useHover.mjs.map +0 -1
- package/react-hooks/useMeasure.js.map +0 -1
- package/react-hooks/useMeasure.mjs.map +0 -1
- package/react-hooks/useObservableNextRouter.js.map +0 -1
- package/react-hooks/useObservableNextRouter.mjs.map +0 -1
- package/react.js.map +0 -1
- package/react.mjs.map +0 -1
- package/src/ObservableObject.ts +0 -1350
- package/src/ObservablePrimitive.ts +0 -62
- package/src/babel/index.ts +0 -83
- package/src/batching.ts +0 -357
- package/src/computed.ts +0 -18
- package/src/config/enable$GetSet.ts +0 -30
- package/src/config/enableReactComponents.ts +0 -26
- package/src/config/enableReactNativeComponents.ts +0 -102
- package/src/config/enableReactTracking.ts +0 -62
- package/src/config/enableReactUse.ts +0 -32
- package/src/config/enable_PeekAssign.ts +0 -31
- package/src/config.ts +0 -47
- package/src/createObservable.ts +0 -47
- package/src/event.ts +0 -26
- package/src/globals.ts +0 -235
- package/src/helpers/pageHash.ts +0 -41
- package/src/helpers/pageHashParams.ts +0 -55
- package/src/helpers/time.ts +0 -30
- package/src/helpers/trackHistory.ts +0 -29
- package/src/helpers/undoRedo.ts +0 -111
- package/src/helpers.ts +0 -231
- package/src/is.ts +0 -63
- package/src/linked.ts +0 -17
- package/src/observable.ts +0 -32
- package/src/observableInterfaces.ts +0 -151
- package/src/observableTypes.ts +0 -232
- package/src/observe.ts +0 -89
- package/src/old-plugins/firebase.ts +0 -1053
- package/src/onChange.ts +0 -146
- package/src/persist/configureObservablePersistence.ts +0 -7
- package/src/persist/fieldTransformer.ts +0 -149
- package/src/persist/observablePersistRemoteFunctionsAdapter.ts +0 -39
- package/src/persist/persistObservable.ts +0 -1034
- package/src/persist-plugins/async-storage.ts +0 -99
- package/src/persist-plugins/indexeddb.ts +0 -439
- package/src/persist-plugins/local-storage.ts +0 -86
- package/src/persist-plugins/mmkv.ts +0 -91
- package/src/proxy.ts +0 -28
- package/src/react/Computed.tsx +0 -8
- package/src/react/For.tsx +0 -116
- package/src/react/Memo.tsx +0 -4
- package/src/react/Reactive.tsx +0 -53
- package/src/react/Show.tsx +0 -33
- package/src/react/Switch.tsx +0 -43
- package/src/react/react-globals.ts +0 -3
- package/src/react/reactInterfaces.ts +0 -32
- package/src/react/reactive-observer.tsx +0 -210
- package/src/react/useComputed.ts +0 -36
- package/src/react/useEffectOnce.ts +0 -41
- package/src/react/useIsMounted.ts +0 -16
- package/src/react/useMount.ts +0 -15
- package/src/react/useObservable.ts +0 -24
- package/src/react/useObservableReducer.ts +0 -52
- package/src/react/useObservableState.ts +0 -30
- package/src/react/useObserve.ts +0 -54
- package/src/react/useObserveEffect.ts +0 -40
- package/src/react/usePauseProvider.tsx +0 -16
- package/src/react/useSelector.ts +0 -167
- package/src/react/useUnmount.ts +0 -8
- package/src/react/useWhen.ts +0 -9
- package/src/react-hooks/createObservableHook.ts +0 -53
- package/src/react-hooks/useHover.ts +0 -40
- package/src/react-hooks/useMeasure.ts +0 -48
- package/src/react-hooks/useObservableNextRouter.ts +0 -137
- package/src/retry.ts +0 -71
- package/src/setupTracking.ts +0 -26
- package/src/sync/activateSyncedNode.ts +0 -128
- package/src/sync/configureObservableSync.ts +0 -7
- package/src/sync/persistTypes.ts +0 -216
- package/src/sync/syncHelpers.ts +0 -180
- package/src/sync/syncObservable.ts +0 -1056
- package/src/sync/syncObservableAdapter.ts +0 -31
- package/src/sync/syncTypes.ts +0 -189
- package/src/sync/synced.ts +0 -21
- package/src/sync-plugins/crud.ts +0 -412
- package/src/sync-plugins/fetch.ts +0 -80
- package/src/sync-plugins/keel.ts +0 -495
- package/src/sync-plugins/supabase.ts +0 -249
- package/src/sync-plugins/tanstack-query.ts +0 -113
- package/src/sync-plugins/tanstack-react-query.ts +0 -12
- package/src/trace/traceHelpers.ts +0 -11
- package/src/trace/useTraceListeners.ts +0 -34
- package/src/trace/useTraceUpdates.ts +0 -24
- package/src/trace/useVerifyNotTracking.ts +0 -33
- package/src/trace/useVerifyOneRender.ts +0 -10
- package/src/trackSelector.ts +0 -52
- package/src/tracking.ts +0 -43
- package/src/types/babel.d.ts +0 -12
- package/src/when.ts +0 -75
- package/sync-plugins/crud.js.map +0 -1
- package/sync-plugins/crud.mjs.map +0 -1
- package/sync-plugins/fetch.js.map +0 -1
- package/sync-plugins/fetch.mjs.map +0 -1
- package/sync-plugins/keel.js.map +0 -1
- package/sync-plugins/keel.mjs.map +0 -1
- package/sync-plugins/supabase.js.map +0 -1
- package/sync-plugins/supabase.mjs.map +0 -1
- package/sync-plugins/tanstack-query.js.map +0 -1
- package/sync-plugins/tanstack-query.mjs.map +0 -1
- package/sync-plugins/tanstack-react-query.js.map +0 -1
- package/sync-plugins/tanstack-react-query.mjs.map +0 -1
- package/sync.js.map +0 -1
- package/sync.mjs.map +0 -1
- package/trace.js.map +0 -1
- package/trace.mjs.map +0 -1
package/src/onChange.ts
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import { getNodeValue } from './globals';
|
|
2
|
-
import { isArray } from './is';
|
|
3
|
-
import type { ListenerFn, ListenerParams, NodeValue, NodeValueListener, TrackingType } from './observableInterfaces';
|
|
4
|
-
|
|
5
|
-
export function onChange(
|
|
6
|
-
node: NodeValue,
|
|
7
|
-
callback: ListenerFn,
|
|
8
|
-
options: { trackingType?: TrackingType; initial?: boolean; immediate?: boolean; noArgs?: boolean } = {},
|
|
9
|
-
fromLinks?: Set<NodeValue>,
|
|
10
|
-
): () => void {
|
|
11
|
-
const { initial, immediate, noArgs } = options;
|
|
12
|
-
const { trackingType } = options;
|
|
13
|
-
|
|
14
|
-
let listeners = immediate ? node.listenersImmediate : node.listeners;
|
|
15
|
-
if (!listeners) {
|
|
16
|
-
listeners = new Set();
|
|
17
|
-
if (immediate) {
|
|
18
|
-
node.listenersImmediate = listeners;
|
|
19
|
-
} else {
|
|
20
|
-
node.listeners = listeners;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const listener: NodeValueListener = {
|
|
25
|
-
listener: callback,
|
|
26
|
-
track: trackingType,
|
|
27
|
-
noArgs,
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
listeners.add(listener);
|
|
31
|
-
|
|
32
|
-
if (initial) {
|
|
33
|
-
const value = getNodeValue(node);
|
|
34
|
-
callback({
|
|
35
|
-
value,
|
|
36
|
-
loading: true,
|
|
37
|
-
remote: false,
|
|
38
|
-
changes: [
|
|
39
|
-
{
|
|
40
|
-
path: [],
|
|
41
|
-
pathTypes: [],
|
|
42
|
-
prevAtPath: value,
|
|
43
|
-
valueAtPath: value,
|
|
44
|
-
},
|
|
45
|
-
],
|
|
46
|
-
getPrevious: () => undefined,
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
let extraDisposes: (() => void)[];
|
|
51
|
-
|
|
52
|
-
function addLinkedNodeListeners(childNode: NodeValue, cb: ListenerFn = callback, from?: NodeValue) {
|
|
53
|
-
// Don't add listeners for the same node more than once
|
|
54
|
-
if (!fromLinks?.has(childNode)) {
|
|
55
|
-
fromLinks ||= new Set();
|
|
56
|
-
fromLinks.add(from || node);
|
|
57
|
-
cb ||= callback;
|
|
58
|
-
const childOptions: Parameters<typeof onChange>[2] = {
|
|
59
|
-
trackingType: true,
|
|
60
|
-
...options,
|
|
61
|
-
};
|
|
62
|
-
// onChange for the linked node
|
|
63
|
-
extraDisposes = [...(extraDisposes || []), onChange(childNode, cb as ListenerFn, childOptions, fromLinks)];
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// Add listeners for linked to nodes
|
|
68
|
-
if (node.linkedToNode) {
|
|
69
|
-
addLinkedNodeListeners(node.linkedToNode);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// Add listeners for linked from nodes
|
|
73
|
-
node.linkedFromNodes?.forEach((linkedFromNode) => addLinkedNodeListeners(linkedFromNode));
|
|
74
|
-
|
|
75
|
-
// Go up through the parents and add listeners for linked from nodes
|
|
76
|
-
node.numListenersRecursive++;
|
|
77
|
-
let parent = node.parent;
|
|
78
|
-
let pathParent: string[] = [node!.key!];
|
|
79
|
-
while (parent) {
|
|
80
|
-
if (parent.linkedFromNodes) {
|
|
81
|
-
for (const linkedFromNode of parent.linkedFromNodes) {
|
|
82
|
-
if (!fromLinks?.has(linkedFromNode)) {
|
|
83
|
-
const cb = createCb(linkedFromNode, pathParent, callback);
|
|
84
|
-
addLinkedNodeListeners(linkedFromNode, cb, parent);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
parent.numListenersRecursive++;
|
|
89
|
-
|
|
90
|
-
pathParent = [parent!.key!, ...pathParent];
|
|
91
|
-
parent = parent.parent;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return () => {
|
|
95
|
-
listeners!.delete(listener);
|
|
96
|
-
extraDisposes?.forEach((fn) => fn());
|
|
97
|
-
|
|
98
|
-
let parent = node;
|
|
99
|
-
while (parent) {
|
|
100
|
-
parent.numListenersRecursive--;
|
|
101
|
-
|
|
102
|
-
parent = parent.parent!;
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function createCb(linkedFromNode: NodeValue, path: string[], callback: ListenerFn) {
|
|
108
|
-
// Create a callback for a path that calls it with the current value at the path
|
|
109
|
-
let { valueAtPath: prevAtPath } = getValueAtPath(getNodeValue(linkedFromNode), path);
|
|
110
|
-
|
|
111
|
-
return function ({ value: valueA, loading, remote }: ListenerParams<any>) {
|
|
112
|
-
const { valueAtPath } = getValueAtPath(valueA, path);
|
|
113
|
-
if (valueAtPath !== prevAtPath) {
|
|
114
|
-
callback({
|
|
115
|
-
value: valueAtPath,
|
|
116
|
-
loading,
|
|
117
|
-
remote,
|
|
118
|
-
changes: [
|
|
119
|
-
{
|
|
120
|
-
path: [],
|
|
121
|
-
pathTypes: [],
|
|
122
|
-
prevAtPath,
|
|
123
|
-
valueAtPath,
|
|
124
|
-
},
|
|
125
|
-
],
|
|
126
|
-
getPrevious: () => prevAtPath,
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
prevAtPath = valueAtPath;
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
function getValueAtPath(
|
|
134
|
-
obj: Record<string, any>,
|
|
135
|
-
path: string[],
|
|
136
|
-
): { valueAtPath: any; pathTypes: ('object' | 'array')[] } {
|
|
137
|
-
let o: Record<string, any> = obj;
|
|
138
|
-
const pathTypes: ('object' | 'array')[] = [];
|
|
139
|
-
for (let i = 0; o && i < path.length; i++) {
|
|
140
|
-
pathTypes.push(isArray(o) ? 'array' : 'object');
|
|
141
|
-
const p = path[i];
|
|
142
|
-
o = (o as any)[p];
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
return { valueAtPath: o, pathTypes };
|
|
146
|
-
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { ObservablePersistenceConfig } from '@legendapp/state/sync';
|
|
2
|
-
|
|
3
|
-
export const observablePersistConfiguration: ObservablePersistenceConfig = {};
|
|
4
|
-
|
|
5
|
-
export function configureObservablePersistence(options?: ObservablePersistenceConfig) {
|
|
6
|
-
Object.assign(observablePersistConfiguration, options);
|
|
7
|
-
}
|
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
constructObjectWithPath,
|
|
3
|
-
deconstructObjectWithPath,
|
|
4
|
-
isArray,
|
|
5
|
-
isObject,
|
|
6
|
-
isString,
|
|
7
|
-
TypeAtPath,
|
|
8
|
-
internal,
|
|
9
|
-
} from '@legendapp/state';
|
|
10
|
-
import type { FieldTransforms } from '@legendapp/state/sync';
|
|
11
|
-
|
|
12
|
-
const { initializePathType, symbolDelete } = internal;
|
|
13
|
-
|
|
14
|
-
let validateMap: (map: Record<string, any>) => void;
|
|
15
|
-
|
|
16
|
-
export function transformPath(path: string[], pathTypes: TypeAtPath[], map: Record<string, any>): string[] {
|
|
17
|
-
const data: Record<string, any> = {};
|
|
18
|
-
let d: Record<string, any> | null = data;
|
|
19
|
-
for (let i = 0; i < path.length; i++) {
|
|
20
|
-
d = d![path[i]] = i === path.length - 1 ? null : initializePathType(pathTypes[i]);
|
|
21
|
-
}
|
|
22
|
-
let value = transformObject(data, map);
|
|
23
|
-
const pathOut = [];
|
|
24
|
-
for (let i = 0; i < path.length; i++) {
|
|
25
|
-
const key = Object.keys(value)[0];
|
|
26
|
-
pathOut.push(key);
|
|
27
|
-
value = value[key];
|
|
28
|
-
}
|
|
29
|
-
return pathOut;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function transformObject(dataIn: Record<string, any>, map: Record<string, any>) {
|
|
33
|
-
if (process.env.NODE_ENV === 'development') {
|
|
34
|
-
validateMap(map);
|
|
35
|
-
}
|
|
36
|
-
let ret = dataIn;
|
|
37
|
-
if (dataIn) {
|
|
38
|
-
if ((dataIn as unknown) === symbolDelete) return dataIn;
|
|
39
|
-
if (isString(dataIn)) {
|
|
40
|
-
return map[dataIn];
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
ret = {};
|
|
44
|
-
|
|
45
|
-
const dict = Object.keys(map).length === 1 && map['_dict'];
|
|
46
|
-
|
|
47
|
-
for (const key in dataIn) {
|
|
48
|
-
let v = dataIn[key];
|
|
49
|
-
|
|
50
|
-
if (dict) {
|
|
51
|
-
ret[key] = transformObject(v, dict);
|
|
52
|
-
} else {
|
|
53
|
-
const mapped = map[key];
|
|
54
|
-
if (mapped === undefined) {
|
|
55
|
-
// Don't transform dateModified if user doesn't want it
|
|
56
|
-
if (key !== '@') {
|
|
57
|
-
ret[key] = v;
|
|
58
|
-
if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
|
|
59
|
-
console.error('A fatal field transformation error has occurred', key, dataIn, map);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
} else if (mapped !== null) {
|
|
63
|
-
if (v !== undefined && v !== null) {
|
|
64
|
-
if (map[key + '_val']) {
|
|
65
|
-
const mapChild = map[key + '_val'];
|
|
66
|
-
if (isArray(v)) {
|
|
67
|
-
v = v.map((vChild) => mapChild[vChild]);
|
|
68
|
-
} else {
|
|
69
|
-
v = mapChild[v];
|
|
70
|
-
}
|
|
71
|
-
} else if (map[key + '_arr'] && isArray(v)) {
|
|
72
|
-
const mapChild = map[key + '_arr'];
|
|
73
|
-
v = v.map((vChild) => transformObject(vChild, mapChild));
|
|
74
|
-
} else if (isObject(v)) {
|
|
75
|
-
if (map[key + '_obj']) {
|
|
76
|
-
v = transformObject(v, map[key + '_obj']);
|
|
77
|
-
} else if (map[key + '_dict']) {
|
|
78
|
-
const mapChild = map[key + '_dict'];
|
|
79
|
-
const out: Record<string, any> = {};
|
|
80
|
-
for (const keyChild in v) {
|
|
81
|
-
out[keyChild] = transformObject(v[keyChild], mapChild);
|
|
82
|
-
}
|
|
83
|
-
v = out;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
ret[mapped] = v;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return ret;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export function transformObjectWithPath(
|
|
97
|
-
obj: object,
|
|
98
|
-
path: string[],
|
|
99
|
-
pathTypes: TypeAtPath[],
|
|
100
|
-
fieldTransforms: FieldTransforms<any>,
|
|
101
|
-
) {
|
|
102
|
-
const constructed = constructObjectWithPath(path, pathTypes, obj);
|
|
103
|
-
const transformed = transformObject(constructed, fieldTransforms);
|
|
104
|
-
const transformedPath = transformPath(path as string[], pathTypes, fieldTransforms);
|
|
105
|
-
return { path: transformedPath, obj: deconstructObjectWithPath(transformedPath, pathTypes, transformed) };
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const invertedMaps = new WeakMap();
|
|
109
|
-
|
|
110
|
-
export function invertFieldMap(obj: Record<string, any>) {
|
|
111
|
-
const existing = invertedMaps.get(obj);
|
|
112
|
-
if (existing) return existing;
|
|
113
|
-
|
|
114
|
-
const target: Record<string, any> = {} as any;
|
|
115
|
-
|
|
116
|
-
for (const key in obj) {
|
|
117
|
-
const val = obj[key];
|
|
118
|
-
if (key === '_dict') {
|
|
119
|
-
target[key] = invertFieldMap(val);
|
|
120
|
-
} else if (key.endsWith('_obj') || key.endsWith('_dict') || key.endsWith('_arr') || key.endsWith('_val')) {
|
|
121
|
-
const keyMapped = obj[key.replace(/_obj|_dict|_arr|_val$/, '')];
|
|
122
|
-
const suffix = key.match(/_obj|_dict|_arr|_val$/)![0];
|
|
123
|
-
target[keyMapped + suffix] = invertFieldMap(val);
|
|
124
|
-
} else if (typeof val === 'string') {
|
|
125
|
-
target[val] = key;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
invertedMaps.set(obj, target);
|
|
129
|
-
|
|
130
|
-
return target;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
if (process.env.NODE_ENV === 'development') {
|
|
134
|
-
validateMap = function (record: Record<string, any>) {
|
|
135
|
-
const values = Object.values(record).filter((value) => {
|
|
136
|
-
if (isObject(value)) {
|
|
137
|
-
validateMap(value);
|
|
138
|
-
} else {
|
|
139
|
-
return isString(value);
|
|
140
|
-
}
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
const uniques = Array.from(new Set(values));
|
|
144
|
-
if (values.length !== uniques.length) {
|
|
145
|
-
console.error('Field transform map has duplicate values', record, values.length, uniques.length);
|
|
146
|
-
}
|
|
147
|
-
return record;
|
|
148
|
-
};
|
|
149
|
-
}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { isPromise } from '@legendapp/state';
|
|
2
|
-
import {
|
|
3
|
-
type ObservablePersistRemoteClass,
|
|
4
|
-
type ObservablePersistRemoteFunctions,
|
|
5
|
-
type ObservablePersistRemoteGetParams,
|
|
6
|
-
} from '@legendapp/state/sync';
|
|
7
|
-
|
|
8
|
-
export function observablePersistRemoteFunctionsAdapter<T = {}>({
|
|
9
|
-
get,
|
|
10
|
-
set,
|
|
11
|
-
}: ObservablePersistRemoteFunctions<T>): ObservablePersistRemoteClass {
|
|
12
|
-
const ret: ObservablePersistRemoteClass = {};
|
|
13
|
-
|
|
14
|
-
if (get) {
|
|
15
|
-
ret.get = (async (params: ObservablePersistRemoteGetParams<T>) => {
|
|
16
|
-
try {
|
|
17
|
-
let value = get(params);
|
|
18
|
-
if (isPromise(value)) {
|
|
19
|
-
value = await value;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
params.onChange({
|
|
23
|
-
value,
|
|
24
|
-
dateModified: params.dateModified,
|
|
25
|
-
lastSync: params.lastSync,
|
|
26
|
-
mode: params.mode,
|
|
27
|
-
});
|
|
28
|
-
params.onGet();
|
|
29
|
-
// eslint-disable-next-line no-empty
|
|
30
|
-
} catch {}
|
|
31
|
-
}) as ObservablePersistRemoteClass['get'];
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (set) {
|
|
35
|
-
ret.set = set as ObservablePersistRemoteClass['set'];
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return ret;
|
|
39
|
-
}
|