@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.
Files changed (327) hide show
  1. package/.DS_Store +0 -0
  2. package/CHANGELOG.md +1 -831
  3. package/LICENSE +1 -21
  4. package/README.md +1 -141
  5. package/as/arrayAsRecord.d.mts +5 -0
  6. package/as/arrayAsRecord.d.ts +5 -0
  7. package/as/arrayAsRecord.js +28 -0
  8. package/as/arrayAsRecord.mjs +26 -0
  9. package/as/arrayAsSet.d.mts +5 -0
  10. package/as/arrayAsSet.d.ts +5 -0
  11. package/as/arrayAsSet.js +13 -0
  12. package/as/arrayAsSet.mjs +11 -0
  13. package/as/arrayAsString.d.mts +5 -0
  14. package/as/arrayAsString.d.ts +5 -0
  15. package/as/arrayAsString.js +13 -0
  16. package/as/arrayAsString.mjs +11 -0
  17. package/as/numberAsString.d.mts +5 -0
  18. package/as/numberAsString.d.ts +5 -0
  19. package/as/numberAsString.js +13 -0
  20. package/as/numberAsString.mjs +11 -0
  21. package/as/recordAsArray.d.mts +5 -0
  22. package/as/recordAsArray.d.ts +5 -0
  23. package/as/recordAsArray.js +25 -0
  24. package/as/recordAsArray.mjs +23 -0
  25. package/as/recordAsString.d.mts +5 -0
  26. package/as/recordAsString.d.ts +5 -0
  27. package/as/recordAsString.js +13 -0
  28. package/as/recordAsString.mjs +11 -0
  29. package/as/setAsArray.d.mts +5 -0
  30. package/as/setAsArray.d.ts +5 -0
  31. package/as/setAsArray.js +13 -0
  32. package/as/setAsArray.mjs +11 -0
  33. package/as/setAsString.d.mts +5 -0
  34. package/as/setAsString.d.ts +5 -0
  35. package/as/setAsString.js +13 -0
  36. package/as/setAsString.mjs +11 -0
  37. package/as/stringAsArray.d.mts +5 -0
  38. package/as/stringAsArray.d.ts +5 -0
  39. package/as/stringAsArray.js +13 -0
  40. package/as/stringAsArray.mjs +11 -0
  41. package/as/stringAsNumber.d.mts +5 -0
  42. package/as/stringAsNumber.d.ts +5 -0
  43. package/as/stringAsNumber.js +16 -0
  44. package/as/stringAsNumber.mjs +14 -0
  45. package/as/stringAsRecord.d.mts +5 -0
  46. package/as/stringAsRecord.d.ts +5 -0
  47. package/as/stringAsRecord.js +15 -0
  48. package/as/stringAsRecord.mjs +13 -0
  49. package/as/stringAsSet.d.mts +5 -0
  50. package/as/stringAsSet.d.ts +5 -0
  51. package/as/stringAsSet.js +13 -0
  52. package/as/stringAsSet.mjs +11 -0
  53. package/babel.d.mts +21 -0
  54. package/babel.d.ts +21 -2
  55. package/babel.js +57 -53
  56. package/babel.mjs +65 -0
  57. package/config/enable$GetSet.js +13 -14
  58. package/config/enable$GetSet.mjs +13 -14
  59. package/config/enableReactComponents.d.mts +9 -0
  60. package/config/enableReactComponents.d.ts +4 -2
  61. package/config/enableReactComponents.js +13 -10
  62. package/config/enableReactComponents.mjs +13 -10
  63. package/config/enableReactNativeComponents.d.mts +22 -0
  64. package/config/enableReactNativeComponents.d.ts +6 -4
  65. package/config/enableReactNativeComponents.js +43 -47
  66. package/config/enableReactNativeComponents.mjs +43 -47
  67. package/config/enableReactTracking.d.mts +7 -0
  68. package/config/enableReactTracking.d.ts +3 -2
  69. package/config/enableReactTracking.js +33 -38
  70. package/config/enableReactTracking.mjs +33 -38
  71. package/config/enableReactUse.d.mts +10 -0
  72. package/config/enableReactUse.d.ts +4 -1
  73. package/config/enableReactUse.js +15 -14
  74. package/config/enableReactUse.mjs +15 -14
  75. package/config/{enable$GetSet.d.ts → enable_GetSet.d.mts} +4 -2
  76. package/config/enable_GetSet.d.ts +10 -0
  77. package/config/enable_PeekAssign.d.mts +10 -0
  78. package/config/enable_PeekAssign.d.ts +4 -2
  79. package/config/enable_PeekAssign.js +13 -14
  80. package/config/enable_PeekAssign.mjs +13 -14
  81. package/helpers/pageHash.d.mts +9 -0
  82. package/helpers/pageHash.d.ts +2 -0
  83. package/helpers/pageHash.js +25 -30
  84. package/helpers/pageHash.mjs +25 -30
  85. package/helpers/pageHashParams.d.mts +9 -0
  86. package/helpers/pageHashParams.d.ts +2 -0
  87. package/helpers/pageHashParams.js +34 -37
  88. package/helpers/pageHashParams.mjs +34 -37
  89. package/helpers/time.d.mts +6 -0
  90. package/helpers/time.d.ts +6 -3
  91. package/helpers/time.js +17 -17
  92. package/helpers/time.mjs +17 -17
  93. package/helpers/trackHistory.d.mts +6 -0
  94. package/helpers/trackHistory.d.ts +4 -2
  95. package/helpers/trackHistory.js +13 -16
  96. package/helpers/trackHistory.mjs +13 -16
  97. package/helpers/undoRedo.d.mts +37 -0
  98. package/helpers/undoRedo.d.ts +5 -3
  99. package/helpers/undoRedo.js +59 -94
  100. package/helpers/undoRedo.mjs +59 -94
  101. package/index.d.mts +404 -0
  102. package/index.d.ts +371 -28
  103. package/index.js +2015 -2166
  104. package/index.mjs +2015 -2166
  105. package/package.json +254 -195
  106. package/persist-plugins/async-storage.d.mts +18 -0
  107. package/persist-plugins/async-storage.d.ts +6 -3
  108. package/persist-plugins/async-storage.js +79 -86
  109. package/persist-plugins/async-storage.mjs +79 -86
  110. package/persist-plugins/indexeddb.d.mts +29 -0
  111. package/persist-plugins/indexeddb.d.ts +6 -3
  112. package/persist-plugins/indexeddb.js +331 -352
  113. package/persist-plugins/indexeddb.mjs +331 -352
  114. package/persist-plugins/local-storage.d.mts +23 -0
  115. package/persist-plugins/local-storage.d.ts +8 -5
  116. package/persist-plugins/local-storage.js +74 -76
  117. package/persist-plugins/local-storage.mjs +74 -76
  118. package/persist-plugins/mmkv.d.mts +18 -0
  119. package/persist-plugins/mmkv.d.ts +6 -3
  120. package/persist-plugins/mmkv.js +82 -86
  121. package/persist-plugins/mmkv.mjs +82 -86
  122. package/react-hooks/createObservableHook.d.mts +5 -0
  123. package/react-hooks/createObservableHook.d.ts +4 -1
  124. package/react-hooks/createObservableHook.js +29 -30
  125. package/react-hooks/createObservableHook.mjs +25 -30
  126. package/react-hooks/useHover.d.mts +5 -0
  127. package/react-hooks/useHover.d.ts +5 -3
  128. package/react-hooks/useHover.js +29 -29
  129. package/react-hooks/useHover.mjs +29 -29
  130. package/react-hooks/useMeasure.d.mts +9 -0
  131. package/react-hooks/useMeasure.d.ts +5 -2
  132. package/react-hooks/useMeasure.js +30 -32
  133. package/react-hooks/useMeasure.mjs +30 -32
  134. package/react-hooks/useObservableNextRouter.d.mts +35 -0
  135. package/react-hooks/useObservableNextRouter.d.ts +9 -7
  136. package/react-hooks/useObservableNextRouter.js +64 -77
  137. package/react-hooks/useObservableNextRouter.mjs +60 -77
  138. package/react.d.mts +157 -0
  139. package/react.d.ts +157 -21
  140. package/react.js +458 -749
  141. package/react.mjs +457 -752
  142. package/sync-plugins/crud.d.mts +54 -0
  143. package/sync-plugins/crud.d.ts +12 -10
  144. package/sync-plugins/crud.js +253 -270
  145. package/sync-plugins/crud.mjs +253 -270
  146. package/sync-plugins/fetch.d.mts +21 -0
  147. package/sync-plugins/fetch.d.ts +7 -4
  148. package/sync-plugins/fetch.js +50 -37
  149. package/sync-plugins/fetch.mjs +50 -37
  150. package/sync-plugins/keel.d.mts +108 -0
  151. package/sync-plugins/keel.d.ts +17 -15
  152. package/sync-plugins/keel.js +229 -462
  153. package/sync-plugins/keel.mjs +227 -464
  154. package/sync-plugins/supabase.d.mts +39 -0
  155. package/sync-plugins/supabase.d.ts +16 -14
  156. package/sync-plugins/supabase.js +128 -128
  157. package/sync-plugins/supabase.mjs +128 -128
  158. package/sync-plugins/tanstack-query.d.mts +14 -0
  159. package/sync-plugins/tanstack-query.d.ts +7 -4
  160. package/sync-plugins/tanstack-query.js +51 -57
  161. package/sync-plugins/tanstack-query.mjs +51 -57
  162. package/sync-plugins/tanstack-react-query.d.mts +8 -0
  163. package/sync-plugins/tanstack-react-query.d.ts +6 -1
  164. package/sync-plugins/tanstack-react-query.js +2 -2
  165. package/sync-plugins/tanstack-react-query.mjs +2 -2
  166. package/sync.d.mts +351 -0
  167. package/sync.d.ts +349 -9
  168. package/sync.js +910 -964
  169. package/sync.mjs +920 -974
  170. package/trace.d.mts +9 -0
  171. package/trace.d.ts +9 -4
  172. package/trace.js +72 -62
  173. package/trace.mjs +72 -62
  174. package/types/babel.d.ts +1 -12
  175. package/babel.js.map +0 -1
  176. package/config/enable$GetSet.js.map +0 -1
  177. package/config/enable$GetSet.mjs.map +0 -1
  178. package/config/enableReactComponents.js.map +0 -1
  179. package/config/enableReactComponents.mjs.map +0 -1
  180. package/config/enableReactNativeComponents.js.map +0 -1
  181. package/config/enableReactNativeComponents.mjs.map +0 -1
  182. package/config/enableReactTracking.js.map +0 -1
  183. package/config/enableReactTracking.mjs.map +0 -1
  184. package/config/enableReactUse.js.map +0 -1
  185. package/config/enableReactUse.mjs.map +0 -1
  186. package/config/enable_PeekAssign.js.map +0 -1
  187. package/config/enable_PeekAssign.mjs.map +0 -1
  188. package/helpers/pageHash.js.map +0 -1
  189. package/helpers/pageHash.mjs.map +0 -1
  190. package/helpers/pageHashParams.js.map +0 -1
  191. package/helpers/pageHashParams.mjs.map +0 -1
  192. package/helpers/time.js.map +0 -1
  193. package/helpers/time.mjs.map +0 -1
  194. package/helpers/trackHistory.js.map +0 -1
  195. package/helpers/trackHistory.mjs.map +0 -1
  196. package/helpers/undoRedo.js.map +0 -1
  197. package/helpers/undoRedo.mjs.map +0 -1
  198. package/history.d.ts +0 -1
  199. package/history.js +0 -24
  200. package/history.js.map +0 -1
  201. package/history.mjs +0 -22
  202. package/history.mjs.map +0 -1
  203. package/index.js.map +0 -1
  204. package/index.mjs.map +0 -1
  205. package/persist-plugins/async-storage.js.map +0 -1
  206. package/persist-plugins/async-storage.mjs.map +0 -1
  207. package/persist-plugins/indexeddb.js.map +0 -1
  208. package/persist-plugins/indexeddb.mjs.map +0 -1
  209. package/persist-plugins/local-storage.js.map +0 -1
  210. package/persist-plugins/local-storage.mjs.map +0 -1
  211. package/persist-plugins/mmkv.js.map +0 -1
  212. package/persist-plugins/mmkv.mjs.map +0 -1
  213. package/react-hooks/createObservableHook.js.map +0 -1
  214. package/react-hooks/createObservableHook.mjs.map +0 -1
  215. package/react-hooks/useHover.js.map +0 -1
  216. package/react-hooks/useHover.mjs.map +0 -1
  217. package/react-hooks/useMeasure.js.map +0 -1
  218. package/react-hooks/useMeasure.mjs.map +0 -1
  219. package/react-hooks/useObservableNextRouter.js.map +0 -1
  220. package/react-hooks/useObservableNextRouter.mjs.map +0 -1
  221. package/react.js.map +0 -1
  222. package/react.mjs.map +0 -1
  223. package/src/ObservableObject.ts +0 -1350
  224. package/src/ObservablePrimitive.ts +0 -62
  225. package/src/babel/index.ts +0 -83
  226. package/src/batching.ts +0 -357
  227. package/src/computed.ts +0 -18
  228. package/src/config/enable$GetSet.ts +0 -30
  229. package/src/config/enableReactComponents.ts +0 -26
  230. package/src/config/enableReactNativeComponents.ts +0 -102
  231. package/src/config/enableReactTracking.ts +0 -62
  232. package/src/config/enableReactUse.ts +0 -32
  233. package/src/config/enable_PeekAssign.ts +0 -31
  234. package/src/config.ts +0 -47
  235. package/src/createObservable.ts +0 -47
  236. package/src/event.ts +0 -26
  237. package/src/globals.ts +0 -235
  238. package/src/helpers/pageHash.ts +0 -41
  239. package/src/helpers/pageHashParams.ts +0 -55
  240. package/src/helpers/time.ts +0 -30
  241. package/src/helpers/trackHistory.ts +0 -29
  242. package/src/helpers/undoRedo.ts +0 -111
  243. package/src/helpers.ts +0 -231
  244. package/src/is.ts +0 -63
  245. package/src/linked.ts +0 -17
  246. package/src/observable.ts +0 -32
  247. package/src/observableInterfaces.ts +0 -151
  248. package/src/observableTypes.ts +0 -232
  249. package/src/observe.ts +0 -89
  250. package/src/old-plugins/firebase.ts +0 -1053
  251. package/src/onChange.ts +0 -146
  252. package/src/persist/configureObservablePersistence.ts +0 -7
  253. package/src/persist/fieldTransformer.ts +0 -149
  254. package/src/persist/observablePersistRemoteFunctionsAdapter.ts +0 -39
  255. package/src/persist/persistObservable.ts +0 -1034
  256. package/src/persist-plugins/async-storage.ts +0 -99
  257. package/src/persist-plugins/indexeddb.ts +0 -439
  258. package/src/persist-plugins/local-storage.ts +0 -86
  259. package/src/persist-plugins/mmkv.ts +0 -91
  260. package/src/proxy.ts +0 -28
  261. package/src/react/Computed.tsx +0 -8
  262. package/src/react/For.tsx +0 -116
  263. package/src/react/Memo.tsx +0 -4
  264. package/src/react/Reactive.tsx +0 -53
  265. package/src/react/Show.tsx +0 -33
  266. package/src/react/Switch.tsx +0 -43
  267. package/src/react/react-globals.ts +0 -3
  268. package/src/react/reactInterfaces.ts +0 -32
  269. package/src/react/reactive-observer.tsx +0 -210
  270. package/src/react/useComputed.ts +0 -36
  271. package/src/react/useEffectOnce.ts +0 -41
  272. package/src/react/useIsMounted.ts +0 -16
  273. package/src/react/useMount.ts +0 -15
  274. package/src/react/useObservable.ts +0 -24
  275. package/src/react/useObservableReducer.ts +0 -52
  276. package/src/react/useObservableState.ts +0 -30
  277. package/src/react/useObserve.ts +0 -54
  278. package/src/react/useObserveEffect.ts +0 -40
  279. package/src/react/usePauseProvider.tsx +0 -16
  280. package/src/react/useSelector.ts +0 -167
  281. package/src/react/useUnmount.ts +0 -8
  282. package/src/react/useWhen.ts +0 -9
  283. package/src/react-hooks/createObservableHook.ts +0 -53
  284. package/src/react-hooks/useHover.ts +0 -40
  285. package/src/react-hooks/useMeasure.ts +0 -48
  286. package/src/react-hooks/useObservableNextRouter.ts +0 -137
  287. package/src/retry.ts +0 -71
  288. package/src/setupTracking.ts +0 -26
  289. package/src/sync/activateSyncedNode.ts +0 -128
  290. package/src/sync/configureObservableSync.ts +0 -7
  291. package/src/sync/persistTypes.ts +0 -216
  292. package/src/sync/syncHelpers.ts +0 -180
  293. package/src/sync/syncObservable.ts +0 -1056
  294. package/src/sync/syncObservableAdapter.ts +0 -31
  295. package/src/sync/syncTypes.ts +0 -189
  296. package/src/sync/synced.ts +0 -21
  297. package/src/sync-plugins/crud.ts +0 -412
  298. package/src/sync-plugins/fetch.ts +0 -80
  299. package/src/sync-plugins/keel.ts +0 -495
  300. package/src/sync-plugins/supabase.ts +0 -249
  301. package/src/sync-plugins/tanstack-query.ts +0 -113
  302. package/src/sync-plugins/tanstack-react-query.ts +0 -12
  303. package/src/trace/traceHelpers.ts +0 -11
  304. package/src/trace/useTraceListeners.ts +0 -34
  305. package/src/trace/useTraceUpdates.ts +0 -24
  306. package/src/trace/useVerifyNotTracking.ts +0 -33
  307. package/src/trace/useVerifyOneRender.ts +0 -10
  308. package/src/trackSelector.ts +0 -52
  309. package/src/tracking.ts +0 -43
  310. package/src/types/babel.d.ts +0 -12
  311. package/src/when.ts +0 -75
  312. package/sync-plugins/crud.js.map +0 -1
  313. package/sync-plugins/crud.mjs.map +0 -1
  314. package/sync-plugins/fetch.js.map +0 -1
  315. package/sync-plugins/fetch.mjs.map +0 -1
  316. package/sync-plugins/keel.js.map +0 -1
  317. package/sync-plugins/keel.mjs.map +0 -1
  318. package/sync-plugins/supabase.js.map +0 -1
  319. package/sync-plugins/supabase.mjs.map +0 -1
  320. package/sync-plugins/tanstack-query.js.map +0 -1
  321. package/sync-plugins/tanstack-query.mjs.map +0 -1
  322. package/sync-plugins/tanstack-react-query.js.map +0 -1
  323. package/sync-plugins/tanstack-react-query.mjs.map +0 -1
  324. package/sync.js.map +0 -1
  325. package/sync.mjs.map +0 -1
  326. package/trace.js.map +0 -1
  327. package/trace.mjs.map +0 -1
@@ -1,111 +0,0 @@
1
- import { type ObservablePrimitive, internal, observable } from '@legendapp/state';
2
-
3
- type UndoRedoOptions = {
4
- limit?: number;
5
- };
6
-
7
- /**
8
- * Usage:
9
- *
10
- * Use this function to add undo/redo functionality to an observable.
11
- *
12
- * You can monitor how many undos or redos are available to enable/disable undo/redo
13
- * UI elements with undo$ and redo$.
14
- *
15
- * If you undo and then make a change, it'll delete any redos and add the change, as expected.
16
- *
17
- * If you don't pass in a limit, it will keep all history. This means it can grow indefinitely.
18
- *
19
- * ```typescript
20
- * const obs$ = observable({ test: 'hi', test2: 'a' });
21
- * const { undo, redo, undos$, redos$, getHistory } = undoRedo(obs$, { limit: 40 });
22
- * obs$.test.set('hello');
23
- * undo();
24
- * redo();
25
- * // observables for # of undos/redos available
26
- * undos$.get();
27
- * redos$.get();
28
- * ```
29
- */
30
- export function undoRedo<T>(obs$: ObservablePrimitive<T>, options?: UndoRedoOptions) {
31
- let history = [] as T[];
32
- let historyPointer = 0;
33
- let restoringFromHistory = false;
34
-
35
- const undos$ = observable(0);
36
- const redos$ = observable(0);
37
-
38
- function updateUndoRedo() {
39
- undos$.set(historyPointer);
40
- redos$.set(history.length - historyPointer - 1);
41
- }
42
-
43
- obs$.onChange(({ getPrevious }) => {
44
- // Don't save history if we're restoring from history.
45
- if (restoringFromHistory) return;
46
-
47
- // Don't save history if this is a remote change.
48
- // History will be saved remotely by the client making the local change.
49
- if (internal.globalState.isLoadingRemote || internal.globalState.isLoadingLocal) return;
50
-
51
- // if the history array is empty, grab the previous value as the initial value
52
- if (!history.length) {
53
- const previous = getPrevious();
54
- if (previous) history.push(internal.clone(previous));
55
- historyPointer = 0;
56
- }
57
-
58
- // We're just going to store a copy of the whole object every time it changes.
59
- const snapshot = internal.clone(obs$.get());
60
-
61
- if (options?.limit) {
62
- // limit means the number of undos
63
- history = history.slice(Math.max(0, history.length - options.limit));
64
- } else {
65
- history = history.slice(0, historyPointer + 1);
66
- }
67
-
68
- // we add another history item, which is limit + 1 -- but it's the current one
69
- history.push(snapshot);
70
-
71
- // We're going to keep a pointer to the current history state.
72
- // This way, we can undo to many previous states, and redo.
73
- historyPointer = history.length - 1;
74
-
75
- updateUndoRedo();
76
- });
77
-
78
- return {
79
- undo() {
80
- if (historyPointer > 0) {
81
- historyPointer--;
82
-
83
- const snapshot = internal.clone(history[historyPointer]);
84
- restoringFromHistory = true;
85
- obs$.set(snapshot);
86
- restoringFromHistory = false;
87
- } else {
88
- console.warn('Already at the beginning of undo history');
89
- }
90
-
91
- updateUndoRedo();
92
- },
93
- redo() {
94
- if (historyPointer < history.length - 1) {
95
- historyPointer++;
96
-
97
- const snapshot = internal.clone(history[historyPointer]);
98
- restoringFromHistory = true;
99
- obs$.set(snapshot);
100
- restoringFromHistory = false;
101
- } else {
102
- console.warn('Already at the end of undo history');
103
- }
104
-
105
- updateUndoRedo();
106
- },
107
- undos$: undos$,
108
- redos$: redos$,
109
- getHistory: () => history,
110
- };
111
- }
package/src/helpers.ts DELETED
@@ -1,231 +0,0 @@
1
- import { beginBatch, endBatch } from './batching';
2
- import { getNode, globalState, isObservable, setNodeValue, symbolDelete, symbolOpaque } from './globals';
3
- import { isArray, isEmpty, isFunction, isMap, isNumber, isObject } from './is';
4
- import type { Change, ObserveEvent, OpaqueObject, Selector, TypeAtPath } from './observableInterfaces';
5
- import type { Observable, ObservableParam } from './observableTypes';
6
-
7
- export function computeSelector<T>(selector: Selector<T>, e?: ObserveEvent<T>, retainObservable?: boolean): T {
8
- let c = selector as any;
9
- if (!isObservable(c) && isFunction(c)) {
10
- c = e ? c(e) : c();
11
- }
12
-
13
- return isObservable(c) && !retainObservable ? c.get() : c;
14
- }
15
-
16
- export function getObservableIndex(value$: ObservableParam): number {
17
- const node = getNode(value$);
18
- const n = +node.key! as number;
19
- return isNumber(n) ? n : -1;
20
- }
21
-
22
- export function opaqueObject<T extends object>(value: T): OpaqueObject<T> {
23
- if (value) {
24
- (value as OpaqueObject<T>)[symbolOpaque] = true;
25
- }
26
- return value as OpaqueObject<T>;
27
- }
28
-
29
- const getValueAtPathReducer = (o: any, p: any) => o && o[p];
30
- export function getValueAtPath(obj: Record<string, any>, path: string[]): any {
31
- return path.reduce(getValueAtPathReducer, obj);
32
- }
33
-
34
- export function setAtPath<T extends object>(
35
- obj: T,
36
- path: string[],
37
- pathTypes: TypeAtPath[],
38
- value: any,
39
- mode?: 'set' | 'merge',
40
- fullObj?: T,
41
- restore?: (path: string[], value: any) => void,
42
- ) {
43
- let p: string | undefined = undefined;
44
- let o: Record<string, any> = obj;
45
- if (path.length > 0) {
46
- let oFull: Record<string, any> | undefined = fullObj;
47
- for (let i = 0; i < path.length; i++) {
48
- p = path[i];
49
- if (o[p] === symbolDelete) {
50
- // If this was previously deleted, restore it
51
- if (oFull) {
52
- o[p] = oFull[p];
53
- restore?.(path.slice(0, i + 1), o[p]);
54
- }
55
- return obj;
56
- } else if (o[p] === undefined && value === undefined && i === path.length - 1) {
57
- // If setting undefined and the key is undefined, no need to initialize or set it
58
- return obj;
59
- } else if (o[p] === undefined || o[p] === null) {
60
- o[p] = initializePathType(pathTypes[i]);
61
- }
62
- if (i < path.length - 1) {
63
- o = o[p];
64
- if (oFull) {
65
- oFull = oFull[p];
66
- }
67
- }
68
- }
69
- }
70
-
71
- // Don't set if the value is the same. This prevents creating a new key
72
- // when setting undefined on an object without this key
73
- if (p === undefined) {
74
- if (mode === 'merge') {
75
- obj = _mergeIntoObservable(obj, value);
76
- } else {
77
- obj = value;
78
- }
79
- } else {
80
- if (mode === 'merge') {
81
- o[p] = _mergeIntoObservable(o[p], value);
82
- } else if (isMap(o)) {
83
- o.set(p, value);
84
- } else {
85
- o[p] = value;
86
- }
87
- }
88
-
89
- return obj;
90
- }
91
- export function setInObservableAtPath(
92
- value$: ObservableParam,
93
- path: string[],
94
- pathTypes: TypeAtPath[],
95
- value: any,
96
- mode: 'assign' | 'set' | 'merge',
97
- ) {
98
- let o: Record<string, any> = value$;
99
- let v = value;
100
- for (let i = 0; i < path.length; i++) {
101
- const p = path[i];
102
- if (!o.peek()[p]) {
103
- o[p].set(initializePathType(pathTypes[i]));
104
- }
105
- o = o[p];
106
- v = v[p];
107
- }
108
-
109
- if (v === symbolDelete) {
110
- (o as Observable).delete();
111
- }
112
- // Assign if possible, or set otherwise
113
- else if (mode === 'assign' && (o as Observable).assign && isObject(o.peek())) {
114
- (o as Observable<{}>).assign(v);
115
- } else if (mode === 'merge') {
116
- mergeIntoObservable(o, v);
117
- } else {
118
- o.set(v);
119
- }
120
- }
121
- export function mergeIntoObservable<T extends ObservableParam<Record<string, any>> | object>(
122
- target: T,
123
- ...sources: any[]
124
- ): T {
125
- beginBatch();
126
- globalState.isMerging = true;
127
- for (let i = 0; i < sources.length; i++) {
128
- target = _mergeIntoObservable(target, sources[i]);
129
- }
130
- globalState.isMerging = false;
131
- endBatch();
132
- return target;
133
- }
134
- function _mergeIntoObservable<T extends ObservableParam<Record<string, any>> | object>(target: T, source: any): T {
135
- if (isObservable(source)) {
136
- source = source.peek();
137
- }
138
- const needsSet = isObservable(target);
139
- const targetValue = needsSet ? target.peek() : target;
140
-
141
- const isTargetArr = isArray(targetValue);
142
- const isTargetObj = !isTargetArr && isObject(targetValue);
143
-
144
- if ((isTargetObj && isObject(source) && !isEmpty(targetValue)) || (isTargetArr && targetValue.length > 0)) {
145
- const keys: string[] = Object.keys(source);
146
-
147
- for (let i = 0; i < keys.length; i++) {
148
- const key = keys[i];
149
- const sourceValue = (source as Record<string, any>)[key];
150
- if (sourceValue === symbolDelete) {
151
- needsSet && (target as any)[key]?.delete
152
- ? (target as any)[key].delete()
153
- : delete (target as Record<string, any>)[key];
154
- } else {
155
- const isObj = isObject(sourceValue);
156
- const isArr = !isObj && isArray(sourceValue);
157
- const targetChild = (target as Record<string, any>)[key];
158
- if ((isObj || isArr) && targetChild && (needsSet || !isEmpty(targetChild))) {
159
- if (!needsSet && (!targetChild || (isObj ? !isObject(targetChild) : !isArray(targetChild)))) {
160
- (target as Record<string, any>)[key] = sourceValue;
161
- } else {
162
- _mergeIntoObservable(targetChild, sourceValue);
163
- }
164
- } else {
165
- needsSet
166
- ? targetChild.set(sourceValue)
167
- : (((target as Record<string, any>)[key] as any) = sourceValue);
168
- }
169
- }
170
- }
171
- } else if (source !== undefined) {
172
- needsSet ? target.set(source) : ((target as any) = source);
173
- }
174
-
175
- return target;
176
- }
177
- export function constructObjectWithPath(path: string[], pathTypes: TypeAtPath[], value: any): object {
178
- let out;
179
- if (path.length > 0) {
180
- let o: Record<string, any> = (out = {});
181
- for (let i = 0; i < path.length; i++) {
182
- const p = path[i];
183
- o[p] = i === path.length - 1 ? value : initializePathType(pathTypes[i]);
184
- o = o[p];
185
- }
186
- } else {
187
- out = value;
188
- }
189
-
190
- return out;
191
- }
192
- export function deconstructObjectWithPath(path: string[], pathTypes: TypeAtPath[], value: any): object {
193
- let o = value;
194
- for (let i = 0; i < path.length; i++) {
195
- const p = path[i];
196
- o = o ? o[p] : initializePathType(pathTypes[i]);
197
- }
198
-
199
- return o;
200
- }
201
- export function isObservableValueReady(value: any) {
202
- return !!value && ((!isObject(value) && !isArray(value)) || !isEmpty(value));
203
- }
204
-
205
- export function setSilently(value$: ObservableParam, newValue: any) {
206
- const node = getNode(value$);
207
- return setNodeValue(node, newValue).newValue;
208
- }
209
-
210
- export function initializePathType(pathType: TypeAtPath): any {
211
- switch (pathType) {
212
- case 'array':
213
- return [];
214
- case 'object':
215
- return {};
216
- case 'map':
217
- return new Map();
218
- case 'set':
219
- return new Set();
220
- }
221
- }
222
- export function applyChange<T extends object>(value: T, change: Change, applyPrevious?: boolean): T {
223
- const { path, valueAtPath, prevAtPath, pathTypes } = change;
224
- return setAtPath(value, path as string[], pathTypes, applyPrevious ? prevAtPath : valueAtPath);
225
- }
226
- export function applyChanges<T extends object>(value: T, changes: Change[], applyPrevious?: boolean): T {
227
- for (let i = 0; i < changes.length; i++) {
228
- value = applyChange(value, changes[i], applyPrevious);
229
- }
230
- return value;
231
- }
package/src/is.ts DELETED
@@ -1,63 +0,0 @@
1
- import type { ChildNodeValue, NodeValue } from './observableInterfaces';
2
-
3
- export const hasOwnProperty = Object.prototype.hasOwnProperty;
4
-
5
- export function isArray(obj: unknown): obj is Array<any> {
6
- return Array.isArray(obj);
7
- }
8
- export function isString(obj: unknown): obj is string {
9
- return typeof obj === 'string';
10
- }
11
- export function isObject(obj: unknown): obj is Record<any, any> {
12
- return !!obj && typeof obj === 'object' && !(obj instanceof Date) && !isArray(obj);
13
- }
14
- export function isFunction(obj: unknown): obj is Function {
15
- return typeof obj === 'function';
16
- }
17
- export function isPrimitive(arg: unknown): arg is string | number | bigint | boolean | symbol {
18
- const type = typeof arg;
19
- return arg !== undefined && (isDate(arg) || (type !== 'object' && type !== 'function'));
20
- }
21
- export function isDate(obj: unknown): obj is Date {
22
- return obj instanceof Date;
23
- }
24
- export function isSymbol(obj: unknown): obj is symbol {
25
- return typeof obj === 'symbol';
26
- }
27
- export function isBoolean(obj: unknown): obj is boolean {
28
- return typeof obj === 'boolean';
29
- }
30
- export function isPromise<T>(obj: unknown): obj is Promise<T> {
31
- return obj instanceof Promise;
32
- }
33
- export function isMap(obj: unknown): obj is Map<any, any> {
34
- return obj instanceof Map;
35
- }
36
- export function isNumber(obj: unknown): obj is number {
37
- const n = obj as number;
38
- return n - n < 1;
39
- }
40
- export function isEmpty(obj: object): boolean {
41
- // Looping and returning false on the first property is faster than Object.keys(obj).length === 0
42
- // https://jsbench.me/qfkqv692c8
43
- if (!obj) return false;
44
- if (isArray(obj)) return obj.length === 0;
45
- for (const key in obj) {
46
- if (hasOwnProperty.call(obj, key)) {
47
- return false;
48
- }
49
- }
50
- return true;
51
- }
52
- export function isNullOrUndefined(value: any): value is undefined | null {
53
- return value === undefined || value === null;
54
- }
55
- const setPrimitives = new Set(['boolean', 'string', 'number']);
56
- /** @internal */
57
- export function isActualPrimitive(arg: unknown): arg is boolean | string | number {
58
- return setPrimitives.has(typeof arg);
59
- }
60
- /** @internal */
61
- export function isChildNodeValue(node: NodeValue): node is ChildNodeValue {
62
- return !!node.parent;
63
- }
package/src/linked.ts DELETED
@@ -1,17 +0,0 @@
1
- import { isFunction } from './is';
2
- import { symbolLinked } from './globals';
3
- import { Linked, LinkedOptions } from './observableInterfaces';
4
-
5
- export function linked<T>(params: LinkedOptions<T> | (() => T), options?: LinkedOptions<T>): Linked<T> {
6
- if (isFunction(params)) {
7
- params = { get: params };
8
- }
9
- if (options) {
10
- params = { ...params, ...options };
11
- }
12
- const ret = function () {
13
- return { [symbolLinked]: params };
14
- };
15
- ret.prototype[symbolLinked] = params;
16
- return ret as Linked<T>;
17
- }
package/src/observable.ts DELETED
@@ -1,32 +0,0 @@
1
- import { extractPromise, getProxy, peekInternal } from './ObservableObject';
2
- import { ObservablePrimitiveClass } from './ObservablePrimitive';
3
- import { createObservable } from './createObservable';
4
- import { getNode } from './globals';
5
- import type { Observable, ObservableParam, ObservablePrimitive, RecursiveValueOrFunction } from './observableTypes';
6
- import type { ObservableSyncState } from './sync/syncTypes';
7
-
8
- export function observable<T>(): Observable<T | undefined>;
9
- export function observable<T>(
10
- value: Promise<RecursiveValueOrFunction<T>> | (() => RecursiveValueOrFunction<T>) | RecursiveValueOrFunction<T>,
11
- ): Observable<T>;
12
- export function observable<T>(value: T): Observable<T>;
13
- export function observable<T>(value?: T): Observable<any> {
14
- return createObservable(value, false, extractPromise, getProxy, ObservablePrimitiveClass) as any;
15
- }
16
-
17
- export function observablePrimitive<T>(value: Promise<T>): ObservablePrimitive<T>;
18
- export function observablePrimitive<T>(value?: T): ObservablePrimitive<T>;
19
- export function observablePrimitive<T>(value?: T | Promise<T>): ObservablePrimitive<T> {
20
- return createObservable(value, true, extractPromise, getProxy, ObservablePrimitiveClass) as any;
21
- }
22
-
23
- export function syncState(obs: ObservableParam) {
24
- const node = getNode(obs);
25
- if (!node.state) {
26
- peekInternal(node);
27
- }
28
- if (!node.state) {
29
- node.state = observable({} as ObservableSyncState);
30
- }
31
- return node.state!;
32
- }
@@ -1,151 +0,0 @@
1
- import type { symbolOpaque } from './globals';
2
- import type { Observable, ObservableParam } from './observableTypes';
3
- import type { ObservableSyncState, SyncedOptions } from './sync/syncTypes';
4
-
5
- export type TrackingType = undefined | true | symbol; // true === shallow
6
-
7
- export interface GetOptions {
8
- shallow: boolean;
9
- }
10
-
11
- export type OpaqueObject<T> = T & { [symbolOpaque]: true };
12
-
13
- export interface ListenerParams<T = any> {
14
- value: T;
15
- getPrevious: () => T;
16
- changes: Change[];
17
- remote: boolean;
18
- /** @internal */
19
- loading: boolean;
20
- }
21
-
22
- export type ListenerFn<T = any> = (params: ListenerParams<T>) => void;
23
-
24
- export interface ObservableEvent {
25
- fire(): void;
26
- on(cb?: () => void): ObservableListenerDispose;
27
- get(): void;
28
- }
29
-
30
- export type TypeAtPath = 'object' | 'array' | 'map' | 'set';
31
-
32
- export interface Change {
33
- path: string[];
34
- pathTypes: TypeAtPath[];
35
- valueAtPath: any;
36
- prevAtPath: any;
37
- }
38
-
39
- export type RecordValue<T> = T extends Record<string, infer t> ? t : never;
40
- export type ArrayValue<T> = T extends Array<infer t> ? t : never;
41
- export type ObservableValue<T> = T extends Observable<infer t> ? t : never;
42
-
43
- export type Selector<T> = ObservableParam<T> | ObservableEvent | (() => T) | T;
44
-
45
- export type ClassConstructor<I, Args extends any[] = any[]> = new (...args: Args) => I;
46
- export type ObservableListenerDispose = () => void;
47
-
48
- export interface ObservableRoot {
49
- _: any;
50
- set?: (value: any) => void;
51
- }
52
-
53
- export type Primitive = boolean | string | number | Date;
54
- export type NotPrimitive<T> = T extends Primitive ? never : T;
55
-
56
- export interface NodeValueListener {
57
- track: TrackingType;
58
- noArgs?: boolean;
59
- listener: ListenerFn;
60
- }
61
- export interface TrackingState {
62
- nodes?: Map<NodeValue, TrackingNode>;
63
- traceListeners?: (nodes: Map<NodeValue, TrackingNode>) => void;
64
- traceUpdates?: (fn: Function) => Function;
65
- }
66
-
67
- interface BaseNodeValue {
68
- children?: Map<string, ChildNodeValue>;
69
- proxy?: object;
70
- root: ObservableRoot;
71
- listeners?: Set<NodeValueListener>;
72
- listenersImmediate?: Set<NodeValueListener>;
73
- isEvent?: boolean;
74
- linkedToNode?: NodeValue;
75
- linkedToNodeDispose?: () => void;
76
- activatedObserveDispose?: () => void;
77
- linkedFromNodes?: Set<NodeValue>;
78
- isSetting?: number;
79
- isAssigning?: number;
80
- isComputing?: boolean;
81
- parentOther?: NodeValue;
82
- functions?: Map<string, Function | Observable<any>>;
83
- lazy?: boolean;
84
- lazyFn?: Function;
85
- needsExtract?: boolean;
86
- numListenersRecursive: number;
87
- state?: Observable<ObservableSyncState>;
88
- activated?: boolean;
89
- activationState?: SyncedOptions & { onError?: () => void; persistedRetry?: boolean };
90
- dirtyFn?: () => void;
91
- dirtyChildren?: Set<NodeValue>;
92
- }
93
-
94
- export interface RootNodeValue extends BaseNodeValue {
95
- parent?: undefined;
96
- key?: undefined;
97
- }
98
-
99
- export interface ChildNodeValue extends BaseNodeValue {
100
- parent: NodeValue;
101
- key: string;
102
- }
103
-
104
- export type NodeValue = RootNodeValue | ChildNodeValue;
105
-
106
- export interface TrackingNode {
107
- node: NodeValue;
108
- track: TrackingType;
109
- num: number;
110
- }
111
- export interface ObserveEvent<T> {
112
- num: number;
113
- previous?: T | undefined;
114
- cancel?: boolean;
115
- onCleanup?: () => void;
116
- }
117
- export interface ObserveEventCallback<T> {
118
- num: number;
119
- previous?: T | undefined;
120
- value?: T;
121
- cancel: boolean;
122
- nodes: Map<NodeValue, TrackingNode> | undefined;
123
- refresh: () => void;
124
- onCleanup?: () => void;
125
- onCleanupReaction?: () => void;
126
- }
127
-
128
- export type SetParams<T> = ListenerParams<T extends Promise<infer t> ? t : T>;
129
-
130
- export interface LinkedOptions<T = any> {
131
- get?: () => Promise<T> | T;
132
- set?: (params: SetParams<T>) => void | Promise<any>;
133
- waitFor?: Selector<any>;
134
- waitForSet?: ((params: WaitForSetFnParams<T>) => any) | Promise<any> | ObservableParam<any> | ObservableEvent;
135
- initial?: (() => T) | T;
136
- activate?: 'auto' | 'lazy';
137
- }
138
-
139
- export interface WaitForSetFnParams<T = any> {
140
- value: T;
141
- changes: Change[];
142
- }
143
-
144
- export type GetMode = 'set' | 'assign' | 'merge' | 'append' | 'prepend';
145
- export interface UpdateFnParams {
146
- value: unknown;
147
- mode?: GetMode;
148
- lastSync?: number | undefined;
149
- }
150
- export type UpdateFn = (params: UpdateFnParams) => void;
151
- export type Linked<T> = T;