@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
@@ -2,102 +2,67 @@
2
2
 
3
3
  var state = require('@legendapp/state');
4
4
 
5
- /**
6
- * Usage:
7
- *
8
- * Use this function to add undo/redo functionality to an observable.
9
- *
10
- * You can monitor how many undos or redos are available to enable/disable undo/redo
11
- * UI elements with undo$ and redo$.
12
- *
13
- * If you undo and then make a change, it'll delete any redos and add the change, as expected.
14
- *
15
- * If you don't pass in a limit, it will keep all history. This means it can grow indefinitely.
16
- *
17
- * ```typescript
18
- * const obs$ = observable({ test: 'hi', test2: 'a' });
19
- * const { undo, redo, undos$, redos$, getHistory } = undoRedo(obs$, { limit: 40 });
20
- * obs$.test.set('hello');
21
- * undo();
22
- * redo();
23
- * // observables for # of undos/redos available
24
- * undos$.get();
25
- * redos$.get();
26
- * ```
27
- */
5
+ // src/helpers/undoRedo.ts
28
6
  function undoRedo(obs$, options) {
29
- let history = [];
30
- let historyPointer = 0;
31
- let restoringFromHistory = false;
32
- const undos$ = state.observable(0);
33
- const redos$ = state.observable(0);
34
- function updateUndoRedo() {
35
- undos$.set(historyPointer);
36
- redos$.set(history.length - historyPointer - 1);
7
+ let history = [];
8
+ let historyPointer = 0;
9
+ let restoringFromHistory = false;
10
+ const undos$ = state.observable(0);
11
+ const redos$ = state.observable(0);
12
+ function updateUndoRedo() {
13
+ undos$.set(historyPointer);
14
+ redos$.set(history.length - historyPointer - 1);
15
+ }
16
+ obs$.onChange(({ getPrevious }) => {
17
+ if (restoringFromHistory)
18
+ return;
19
+ if (state.internal.globalState.isLoadingRemote || state.internal.globalState.isLoadingLocal)
20
+ return;
21
+ if (!history.length) {
22
+ const previous = getPrevious();
23
+ if (previous)
24
+ history.push(state.internal.clone(previous));
25
+ historyPointer = 0;
37
26
  }
38
- obs$.onChange(({ getPrevious }) => {
39
- // Don't save history if we're restoring from history.
40
- if (restoringFromHistory)
41
- return;
42
- // Don't save history if this is a remote change.
43
- // History will be saved remotely by the client making the local change.
44
- if (state.internal.globalState.isLoadingRemote || state.internal.globalState.isLoadingLocal)
45
- return;
46
- // if the history array is empty, grab the previous value as the initial value
47
- if (!history.length) {
48
- const previous = getPrevious();
49
- if (previous)
50
- history.push(state.internal.clone(previous));
51
- historyPointer = 0;
52
- }
53
- // We're just going to store a copy of the whole object every time it changes.
54
- const snapshot = state.internal.clone(obs$.get());
55
- if (options === null || options === void 0 ? void 0 : options.limit) {
56
- // limit means the number of undos
57
- history = history.slice(Math.max(0, history.length - options.limit));
58
- }
59
- else {
60
- history = history.slice(0, historyPointer + 1);
61
- }
62
- // we add another history item, which is limit + 1 -- but it's the current one
63
- history.push(snapshot);
64
- // We're going to keep a pointer to the current history state.
65
- // This way, we can undo to many previous states, and redo.
66
- historyPointer = history.length - 1;
67
- updateUndoRedo();
68
- });
69
- return {
70
- undo() {
71
- if (historyPointer > 0) {
72
- historyPointer--;
73
- const snapshot = state.internal.clone(history[historyPointer]);
74
- restoringFromHistory = true;
75
- obs$.set(snapshot);
76
- restoringFromHistory = false;
77
- }
78
- else {
79
- console.warn('Already at the beginning of undo history');
80
- }
81
- updateUndoRedo();
82
- },
83
- redo() {
84
- if (historyPointer < history.length - 1) {
85
- historyPointer++;
86
- const snapshot = state.internal.clone(history[historyPointer]);
87
- restoringFromHistory = true;
88
- obs$.set(snapshot);
89
- restoringFromHistory = false;
90
- }
91
- else {
92
- console.warn('Already at the end of undo history');
93
- }
94
- updateUndoRedo();
95
- },
96
- undos$: undos$,
97
- redos$: redos$,
98
- getHistory: () => history,
99
- };
27
+ const snapshot = state.internal.clone(obs$.get());
28
+ if (options == null ? void 0 : options.limit) {
29
+ history = history.slice(Math.max(0, history.length - options.limit));
30
+ } else {
31
+ history = history.slice(0, historyPointer + 1);
32
+ }
33
+ history.push(snapshot);
34
+ historyPointer = history.length - 1;
35
+ updateUndoRedo();
36
+ });
37
+ return {
38
+ undo() {
39
+ if (historyPointer > 0) {
40
+ historyPointer--;
41
+ const snapshot = state.internal.clone(history[historyPointer]);
42
+ restoringFromHistory = true;
43
+ obs$.set(snapshot);
44
+ restoringFromHistory = false;
45
+ } else {
46
+ console.warn("Already at the beginning of undo history");
47
+ }
48
+ updateUndoRedo();
49
+ },
50
+ redo() {
51
+ if (historyPointer < history.length - 1) {
52
+ historyPointer++;
53
+ const snapshot = state.internal.clone(history[historyPointer]);
54
+ restoringFromHistory = true;
55
+ obs$.set(snapshot);
56
+ restoringFromHistory = false;
57
+ } else {
58
+ console.warn("Already at the end of undo history");
59
+ }
60
+ updateUndoRedo();
61
+ },
62
+ undos$,
63
+ redos$,
64
+ getHistory: () => history
65
+ };
100
66
  }
101
67
 
102
68
  exports.undoRedo = undoRedo;
103
- //# sourceMappingURL=undoRedo.js.map
@@ -1,101 +1,66 @@
1
1
  import { observable, internal } from '@legendapp/state';
2
2
 
3
- /**
4
- * Usage:
5
- *
6
- * Use this function to add undo/redo functionality to an observable.
7
- *
8
- * You can monitor how many undos or redos are available to enable/disable undo/redo
9
- * UI elements with undo$ and redo$.
10
- *
11
- * If you undo and then make a change, it'll delete any redos and add the change, as expected.
12
- *
13
- * If you don't pass in a limit, it will keep all history. This means it can grow indefinitely.
14
- *
15
- * ```typescript
16
- * const obs$ = observable({ test: 'hi', test2: 'a' });
17
- * const { undo, redo, undos$, redos$, getHistory } = undoRedo(obs$, { limit: 40 });
18
- * obs$.test.set('hello');
19
- * undo();
20
- * redo();
21
- * // observables for # of undos/redos available
22
- * undos$.get();
23
- * redos$.get();
24
- * ```
25
- */
3
+ // src/helpers/undoRedo.ts
26
4
  function undoRedo(obs$, options) {
27
- let history = [];
28
- let historyPointer = 0;
29
- let restoringFromHistory = false;
30
- const undos$ = observable(0);
31
- const redos$ = observable(0);
32
- function updateUndoRedo() {
33
- undos$.set(historyPointer);
34
- redos$.set(history.length - historyPointer - 1);
5
+ let history = [];
6
+ let historyPointer = 0;
7
+ let restoringFromHistory = false;
8
+ const undos$ = observable(0);
9
+ const redos$ = observable(0);
10
+ function updateUndoRedo() {
11
+ undos$.set(historyPointer);
12
+ redos$.set(history.length - historyPointer - 1);
13
+ }
14
+ obs$.onChange(({ getPrevious }) => {
15
+ if (restoringFromHistory)
16
+ return;
17
+ if (internal.globalState.isLoadingRemote || internal.globalState.isLoadingLocal)
18
+ return;
19
+ if (!history.length) {
20
+ const previous = getPrevious();
21
+ if (previous)
22
+ history.push(internal.clone(previous));
23
+ historyPointer = 0;
35
24
  }
36
- obs$.onChange(({ getPrevious }) => {
37
- // Don't save history if we're restoring from history.
38
- if (restoringFromHistory)
39
- return;
40
- // Don't save history if this is a remote change.
41
- // History will be saved remotely by the client making the local change.
42
- if (internal.globalState.isLoadingRemote || internal.globalState.isLoadingLocal)
43
- return;
44
- // if the history array is empty, grab the previous value as the initial value
45
- if (!history.length) {
46
- const previous = getPrevious();
47
- if (previous)
48
- history.push(internal.clone(previous));
49
- historyPointer = 0;
50
- }
51
- // We're just going to store a copy of the whole object every time it changes.
52
- const snapshot = internal.clone(obs$.get());
53
- if (options === null || options === void 0 ? void 0 : options.limit) {
54
- // limit means the number of undos
55
- history = history.slice(Math.max(0, history.length - options.limit));
56
- }
57
- else {
58
- history = history.slice(0, historyPointer + 1);
59
- }
60
- // we add another history item, which is limit + 1 -- but it's the current one
61
- history.push(snapshot);
62
- // We're going to keep a pointer to the current history state.
63
- // This way, we can undo to many previous states, and redo.
64
- historyPointer = history.length - 1;
65
- updateUndoRedo();
66
- });
67
- return {
68
- undo() {
69
- if (historyPointer > 0) {
70
- historyPointer--;
71
- const snapshot = internal.clone(history[historyPointer]);
72
- restoringFromHistory = true;
73
- obs$.set(snapshot);
74
- restoringFromHistory = false;
75
- }
76
- else {
77
- console.warn('Already at the beginning of undo history');
78
- }
79
- updateUndoRedo();
80
- },
81
- redo() {
82
- if (historyPointer < history.length - 1) {
83
- historyPointer++;
84
- const snapshot = internal.clone(history[historyPointer]);
85
- restoringFromHistory = true;
86
- obs$.set(snapshot);
87
- restoringFromHistory = false;
88
- }
89
- else {
90
- console.warn('Already at the end of undo history');
91
- }
92
- updateUndoRedo();
93
- },
94
- undos$: undos$,
95
- redos$: redos$,
96
- getHistory: () => history,
97
- };
25
+ const snapshot = internal.clone(obs$.get());
26
+ if (options == null ? void 0 : options.limit) {
27
+ history = history.slice(Math.max(0, history.length - options.limit));
28
+ } else {
29
+ history = history.slice(0, historyPointer + 1);
30
+ }
31
+ history.push(snapshot);
32
+ historyPointer = history.length - 1;
33
+ updateUndoRedo();
34
+ });
35
+ return {
36
+ undo() {
37
+ if (historyPointer > 0) {
38
+ historyPointer--;
39
+ const snapshot = internal.clone(history[historyPointer]);
40
+ restoringFromHistory = true;
41
+ obs$.set(snapshot);
42
+ restoringFromHistory = false;
43
+ } else {
44
+ console.warn("Already at the beginning of undo history");
45
+ }
46
+ updateUndoRedo();
47
+ },
48
+ redo() {
49
+ if (historyPointer < history.length - 1) {
50
+ historyPointer++;
51
+ const snapshot = internal.clone(history[historyPointer]);
52
+ restoringFromHistory = true;
53
+ obs$.set(snapshot);
54
+ restoringFromHistory = false;
55
+ } else {
56
+ console.warn("Already at the end of undo history");
57
+ }
58
+ updateUndoRedo();
59
+ },
60
+ undos$,
61
+ redos$,
62
+ getHistory: () => history
63
+ };
98
64
  }
99
65
 
100
66
  export { undoRedo };
101
- //# sourceMappingURL=undoRedo.mjs.map