@legendapp/state 3.0.0-alpha.0 → 3.0.0-alpha.2

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 (323) hide show
  1. package/.DS_Store +0 -0
  2. package/CHANGELOG.md +1 -827
  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 +6 -0
  95. package/helpers/trackHistory.js +21 -0
  96. package/helpers/trackHistory.mjs +19 -0
  97. package/helpers/undoRedo.d.mts +37 -0
  98. package/helpers/undoRedo.d.ts +37 -0
  99. package/helpers/undoRedo.js +68 -0
  100. package/helpers/undoRedo.mjs +66 -0
  101. package/index.d.mts +404 -0
  102. package/index.d.ts +371 -28
  103. package/index.js +2003 -2164
  104. package/index.mjs +2003 -2164
  105. package/package.json +254 -185
  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 -348
  113. package/persist-plugins/indexeddb.mjs +331 -348
  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 +909 -962
  169. package/sync.mjs +919 -972
  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/history.d.ts +0 -1
  195. package/history.js +0 -24
  196. package/history.js.map +0 -1
  197. package/history.mjs +0 -22
  198. package/history.mjs.map +0 -1
  199. package/index.js.map +0 -1
  200. package/index.mjs.map +0 -1
  201. package/persist-plugins/async-storage.js.map +0 -1
  202. package/persist-plugins/async-storage.mjs.map +0 -1
  203. package/persist-plugins/indexeddb.js.map +0 -1
  204. package/persist-plugins/indexeddb.mjs.map +0 -1
  205. package/persist-plugins/local-storage.js.map +0 -1
  206. package/persist-plugins/local-storage.mjs.map +0 -1
  207. package/persist-plugins/mmkv.js.map +0 -1
  208. package/persist-plugins/mmkv.mjs.map +0 -1
  209. package/react-hooks/createObservableHook.js.map +0 -1
  210. package/react-hooks/createObservableHook.mjs.map +0 -1
  211. package/react-hooks/useHover.js.map +0 -1
  212. package/react-hooks/useHover.mjs.map +0 -1
  213. package/react-hooks/useMeasure.js.map +0 -1
  214. package/react-hooks/useMeasure.mjs.map +0 -1
  215. package/react-hooks/useObservableNextRouter.js.map +0 -1
  216. package/react-hooks/useObservableNextRouter.mjs.map +0 -1
  217. package/react.js.map +0 -1
  218. package/react.mjs.map +0 -1
  219. package/src/ObservableObject.ts +0 -1350
  220. package/src/ObservablePrimitive.ts +0 -62
  221. package/src/babel/index.ts +0 -83
  222. package/src/batching.ts +0 -357
  223. package/src/computed.ts +0 -18
  224. package/src/config/enable$GetSet.ts +0 -30
  225. package/src/config/enableReactComponents.ts +0 -26
  226. package/src/config/enableReactNativeComponents.ts +0 -102
  227. package/src/config/enableReactTracking.ts +0 -62
  228. package/src/config/enableReactUse.ts +0 -32
  229. package/src/config/enable_PeekAssign.ts +0 -31
  230. package/src/config.ts +0 -47
  231. package/src/createObservable.ts +0 -47
  232. package/src/event.ts +0 -26
  233. package/src/globals.ts +0 -235
  234. package/src/helpers/pageHash.ts +0 -41
  235. package/src/helpers/pageHashParams.ts +0 -55
  236. package/src/helpers/time.ts +0 -30
  237. package/src/helpers.ts +0 -231
  238. package/src/history/trackHistory.ts +0 -29
  239. package/src/history/undoRedo.ts +0 -111
  240. package/src/is.ts +0 -63
  241. package/src/linked.ts +0 -17
  242. package/src/observable.ts +0 -32
  243. package/src/observableInterfaces.ts +0 -151
  244. package/src/observableTypes.ts +0 -232
  245. package/src/observe.ts +0 -89
  246. package/src/old-plugins/firebase.ts +0 -1053
  247. package/src/onChange.ts +0 -146
  248. package/src/persist/configureObservablePersistence.ts +0 -7
  249. package/src/persist/fieldTransformer.ts +0 -149
  250. package/src/persist/observablePersistRemoteFunctionsAdapter.ts +0 -39
  251. package/src/persist/persistObservable.ts +0 -1034
  252. package/src/persist-plugins/async-storage.ts +0 -99
  253. package/src/persist-plugins/indexeddb.ts +0 -432
  254. package/src/persist-plugins/local-storage.ts +0 -86
  255. package/src/persist-plugins/mmkv.ts +0 -91
  256. package/src/proxy.ts +0 -28
  257. package/src/react/Computed.tsx +0 -8
  258. package/src/react/For.tsx +0 -116
  259. package/src/react/Memo.tsx +0 -4
  260. package/src/react/Reactive.tsx +0 -53
  261. package/src/react/Show.tsx +0 -33
  262. package/src/react/Switch.tsx +0 -43
  263. package/src/react/react-globals.ts +0 -3
  264. package/src/react/reactInterfaces.ts +0 -32
  265. package/src/react/reactive-observer.tsx +0 -210
  266. package/src/react/useComputed.ts +0 -36
  267. package/src/react/useEffectOnce.ts +0 -41
  268. package/src/react/useIsMounted.ts +0 -16
  269. package/src/react/useMount.ts +0 -15
  270. package/src/react/useObservable.ts +0 -24
  271. package/src/react/useObservableReducer.ts +0 -52
  272. package/src/react/useObservableState.ts +0 -30
  273. package/src/react/useObserve.ts +0 -54
  274. package/src/react/useObserveEffect.ts +0 -40
  275. package/src/react/usePauseProvider.tsx +0 -16
  276. package/src/react/useSelector.ts +0 -167
  277. package/src/react/useUnmount.ts +0 -8
  278. package/src/react/useWhen.ts +0 -9
  279. package/src/react-hooks/createObservableHook.ts +0 -53
  280. package/src/react-hooks/useHover.ts +0 -40
  281. package/src/react-hooks/useMeasure.ts +0 -48
  282. package/src/react-hooks/useObservableNextRouter.ts +0 -137
  283. package/src/retry.ts +0 -71
  284. package/src/setupTracking.ts +0 -26
  285. package/src/sync/activateSyncedNode.ts +0 -128
  286. package/src/sync/configureObservableSync.ts +0 -7
  287. package/src/sync/persistTypes.ts +0 -216
  288. package/src/sync/syncHelpers.ts +0 -180
  289. package/src/sync/syncObservable.ts +0 -1056
  290. package/src/sync/syncObservableAdapter.ts +0 -31
  291. package/src/sync/syncTypes.ts +0 -189
  292. package/src/sync/synced.ts +0 -21
  293. package/src/sync-plugins/crud.ts +0 -412
  294. package/src/sync-plugins/fetch.ts +0 -80
  295. package/src/sync-plugins/keel.ts +0 -495
  296. package/src/sync-plugins/supabase.ts +0 -249
  297. package/src/sync-plugins/tanstack-query.ts +0 -113
  298. package/src/sync-plugins/tanstack-react-query.ts +0 -12
  299. package/src/trace/traceHelpers.ts +0 -11
  300. package/src/trace/useTraceListeners.ts +0 -34
  301. package/src/trace/useTraceUpdates.ts +0 -24
  302. package/src/trace/useVerifyNotTracking.ts +0 -33
  303. package/src/trace/useVerifyOneRender.ts +0 -10
  304. package/src/trackSelector.ts +0 -52
  305. package/src/tracking.ts +0 -43
  306. package/src/types/babel.d.ts +0 -12
  307. package/src/when.ts +0 -75
  308. package/sync-plugins/crud.js.map +0 -1
  309. package/sync-plugins/crud.mjs.map +0 -1
  310. package/sync-plugins/fetch.js.map +0 -1
  311. package/sync-plugins/fetch.mjs.map +0 -1
  312. package/sync-plugins/keel.js.map +0 -1
  313. package/sync-plugins/keel.mjs.map +0 -1
  314. package/sync-plugins/supabase.js.map +0 -1
  315. package/sync-plugins/supabase.mjs.map +0 -1
  316. package/sync-plugins/tanstack-query.js.map +0 -1
  317. package/sync-plugins/tanstack-query.mjs.map +0 -1
  318. package/sync-plugins/tanstack-react-query.js.map +0 -1
  319. package/sync-plugins/tanstack-react-query.mjs.map +0 -1
  320. package/sync.js.map +0 -1
  321. package/sync.mjs.map +0 -1
  322. package/trace.js.map +0 -1
  323. package/trace.mjs.map +0 -1
@@ -1,38 +1,33 @@
1
1
  import { observable } from '@legendapp/state';
2
2
 
3
- let _options = { setter: 'hash' };
3
+ // src/helpers/pageHash.ts
4
+ var _options = { setter: "hash" };
4
5
  function configurePageHash(options) {
5
- _options = options;
6
+ _options = options;
6
7
  }
7
- const hasWindow = typeof window !== 'undefined';
8
- const pageHash = observable(hasWindow ? window.location.hash.slice(1) : '');
8
+ var hasWindow = typeof window !== "undefined";
9
+ var pageHash = observable(hasWindow ? window.location.hash.slice(1) : "");
9
10
  if (hasWindow) {
10
- let isSetting = false;
11
- // Set the page hash when the observable changes
12
- pageHash.onChange(({ value }) => {
13
- if (!isSetting) {
14
- const hash = '#' + value;
15
- const setter = (_options === null || _options === void 0 ? void 0 : _options.setter) || 'hash';
16
- if (setter === 'pushState') {
17
- history.pushState(null, null, hash);
18
- }
19
- else if (setter === 'replaceState') {
20
- history.replaceState(null, null, hash);
21
- }
22
- else {
23
- location.hash = hash;
24
- }
25
- }
26
- });
27
- // Update the observable whenever the hash changes
28
- const cb = () => {
29
- isSetting = true;
30
- pageHash.set(window.location.hash.slice(1));
31
- isSetting = false;
32
- };
33
- // Subscribe to window hashChange event
34
- window.addEventListener('hashchange', cb);
11
+ let isSetting = false;
12
+ pageHash.onChange(({ value }) => {
13
+ if (!isSetting) {
14
+ const hash = "#" + value;
15
+ const setter = (_options == null ? void 0 : _options.setter) || "hash";
16
+ if (setter === "pushState") {
17
+ history.pushState(null, null, hash);
18
+ } else if (setter === "replaceState") {
19
+ history.replaceState(null, null, hash);
20
+ } else {
21
+ location.hash = hash;
22
+ }
23
+ }
24
+ });
25
+ const cb = () => {
26
+ isSetting = true;
27
+ pageHash.set(window.location.hash.slice(1));
28
+ isSetting = false;
29
+ };
30
+ window.addEventListener("hashchange", cb);
35
31
  }
36
32
 
37
33
  export { configurePageHash, pageHash };
38
- //# sourceMappingURL=pageHash.mjs.map
@@ -0,0 +1,9 @@
1
+ import { Observable } from '@legendapp/state';
2
+
3
+ interface Options {
4
+ setter: 'pushState' | 'replaceState' | 'hash';
5
+ }
6
+ declare function configurePageHashParams(options: Options): void;
7
+ declare const pageHashParams: Observable<Record<string, string>>;
8
+
9
+ export { configurePageHashParams, pageHashParams };
@@ -1,7 +1,9 @@
1
1
  import { Observable } from '@legendapp/state';
2
+
2
3
  interface Options {
3
4
  setter: 'pushState' | 'replaceState' | 'hash';
4
5
  }
5
6
  declare function configurePageHashParams(options: Options): void;
6
7
  declare const pageHashParams: Observable<Record<string, string>>;
8
+
7
9
  export { configurePageHashParams, pageHashParams };
@@ -2,51 +2,48 @@
2
2
 
3
3
  var state = require('@legendapp/state');
4
4
 
5
- let _options = { setter: 'hash' };
5
+ // src/helpers/pageHashParams.ts
6
+ var _options = { setter: "hash" };
6
7
  function configurePageHashParams(options) {
7
- _options = options;
8
+ _options = options;
8
9
  }
9
10
  function toParams(str) {
10
- const ret = {};
11
- const searchParams = new URLSearchParams(str);
12
- for (const [key, value] of searchParams) {
13
- ret[key] = value;
14
- }
15
- return ret;
11
+ const ret = {};
12
+ const searchParams = new URLSearchParams(str);
13
+ for (const [key, value] of searchParams) {
14
+ ret[key] = value;
15
+ }
16
+ return ret;
16
17
  }
17
18
  function toString(params) {
18
- return new URLSearchParams(params).toString().replace(/=$/, '');
19
+ return new URLSearchParams(params).toString().replace(/=$/, "");
19
20
  }
20
- const hasWindow = typeof window !== 'undefined';
21
- const pageHashParams = state.observable(hasWindow ? toParams(window.location.hash.slice(1)) : {});
21
+ var hasWindow = typeof window !== "undefined";
22
+ var pageHashParams = state.observable(
23
+ hasWindow ? toParams(window.location.hash.slice(1)) : {}
24
+ );
22
25
  if (hasWindow) {
23
- let isSetting = false;
24
- // Set the page hash when the observable changes
25
- pageHashParams.onChange(({ value }) => {
26
- if (!isSetting) {
27
- const hash = '#' + toString(value);
28
- const setter = (_options === null || _options === void 0 ? void 0 : _options.setter) || 'hash';
29
- if (setter === 'pushState') {
30
- history.pushState(null, null, hash);
31
- }
32
- else if (setter === 'replaceState') {
33
- history.replaceState(null, null, hash);
34
- }
35
- else {
36
- location.hash = hash;
37
- }
38
- }
39
- });
40
- // Update the observable whenever the hash changes
41
- const cb = () => {
42
- isSetting = true;
43
- pageHashParams.set(toParams(window.location.hash.slice(1)));
44
- isSetting = false;
45
- };
46
- // Subscribe to window hashChange event
47
- window.addEventListener('hashchange', cb);
26
+ let isSetting = false;
27
+ pageHashParams.onChange(({ value }) => {
28
+ if (!isSetting) {
29
+ const hash = "#" + toString(value);
30
+ const setter = (_options == null ? void 0 : _options.setter) || "hash";
31
+ if (setter === "pushState") {
32
+ history.pushState(null, null, hash);
33
+ } else if (setter === "replaceState") {
34
+ history.replaceState(null, null, hash);
35
+ } else {
36
+ location.hash = hash;
37
+ }
38
+ }
39
+ });
40
+ const cb = () => {
41
+ isSetting = true;
42
+ pageHashParams.set(toParams(window.location.hash.slice(1)));
43
+ isSetting = false;
44
+ };
45
+ window.addEventListener("hashchange", cb);
48
46
  }
49
47
 
50
48
  exports.configurePageHashParams = configurePageHashParams;
51
49
  exports.pageHashParams = pageHashParams;
52
- //# sourceMappingURL=pageHashParams.js.map
@@ -1,49 +1,46 @@
1
1
  import { observable } from '@legendapp/state';
2
2
 
3
- let _options = { setter: 'hash' };
3
+ // src/helpers/pageHashParams.ts
4
+ var _options = { setter: "hash" };
4
5
  function configurePageHashParams(options) {
5
- _options = options;
6
+ _options = options;
6
7
  }
7
8
  function toParams(str) {
8
- const ret = {};
9
- const searchParams = new URLSearchParams(str);
10
- for (const [key, value] of searchParams) {
11
- ret[key] = value;
12
- }
13
- return ret;
9
+ const ret = {};
10
+ const searchParams = new URLSearchParams(str);
11
+ for (const [key, value] of searchParams) {
12
+ ret[key] = value;
13
+ }
14
+ return ret;
14
15
  }
15
16
  function toString(params) {
16
- return new URLSearchParams(params).toString().replace(/=$/, '');
17
+ return new URLSearchParams(params).toString().replace(/=$/, "");
17
18
  }
18
- const hasWindow = typeof window !== 'undefined';
19
- const pageHashParams = observable(hasWindow ? toParams(window.location.hash.slice(1)) : {});
19
+ var hasWindow = typeof window !== "undefined";
20
+ var pageHashParams = observable(
21
+ hasWindow ? toParams(window.location.hash.slice(1)) : {}
22
+ );
20
23
  if (hasWindow) {
21
- let isSetting = false;
22
- // Set the page hash when the observable changes
23
- pageHashParams.onChange(({ value }) => {
24
- if (!isSetting) {
25
- const hash = '#' + toString(value);
26
- const setter = (_options === null || _options === void 0 ? void 0 : _options.setter) || 'hash';
27
- if (setter === 'pushState') {
28
- history.pushState(null, null, hash);
29
- }
30
- else if (setter === 'replaceState') {
31
- history.replaceState(null, null, hash);
32
- }
33
- else {
34
- location.hash = hash;
35
- }
36
- }
37
- });
38
- // Update the observable whenever the hash changes
39
- const cb = () => {
40
- isSetting = true;
41
- pageHashParams.set(toParams(window.location.hash.slice(1)));
42
- isSetting = false;
43
- };
44
- // Subscribe to window hashChange event
45
- window.addEventListener('hashchange', cb);
24
+ let isSetting = false;
25
+ pageHashParams.onChange(({ value }) => {
26
+ if (!isSetting) {
27
+ const hash = "#" + toString(value);
28
+ const setter = (_options == null ? void 0 : _options.setter) || "hash";
29
+ if (setter === "pushState") {
30
+ history.pushState(null, null, hash);
31
+ } else if (setter === "replaceState") {
32
+ history.replaceState(null, null, hash);
33
+ } else {
34
+ location.hash = hash;
35
+ }
36
+ }
37
+ });
38
+ const cb = () => {
39
+ isSetting = true;
40
+ pageHashParams.set(toParams(window.location.hash.slice(1)));
41
+ isSetting = false;
42
+ };
43
+ window.addEventListener("hashchange", cb);
46
44
  }
47
45
 
48
46
  export { configurePageHashParams, pageHashParams };
49
- //# sourceMappingURL=pageHashParams.mjs.map
@@ -0,0 +1,6 @@
1
+ import * as _legendapp_state from '@legendapp/state';
2
+
3
+ declare const currentTime: _legendapp_state.ObservablePrimitive<Date>;
4
+ declare const currentDay: _legendapp_state.ObservablePrimitive<Date>;
5
+
6
+ export { currentDay, currentTime };
package/helpers/time.d.ts CHANGED
@@ -1,3 +1,6 @@
1
- declare const currentTime: import("@legendapp/state").ObservablePrimitive<Date>;
2
- declare const currentDay: import("@legendapp/state").ObservablePrimitive<Date>;
3
- export { currentTime, currentDay };
1
+ import * as _legendapp_state from '@legendapp/state';
2
+
3
+ declare const currentTime: _legendapp_state.ObservablePrimitive<Date>;
4
+ declare const currentDay: _legendapp_state.ObservablePrimitive<Date>;
5
+
6
+ export { currentDay, currentTime };
package/helpers/time.js CHANGED
@@ -2,29 +2,29 @@
2
2
 
3
3
  var state = require('@legendapp/state');
4
4
 
5
- const MSPerMinute = 60000;
5
+ // src/helpers/time.ts
6
+ var MSPerMinute = 6e4;
6
7
  function clearTime(date) {
7
- date = new Date(date);
8
- date.setHours(0, 0, 0, 0);
9
- return date;
8
+ date = new Date(date);
9
+ date.setHours(0, 0, 0, 0);
10
+ return date;
10
11
  }
11
- let time = new Date();
12
- const currentTime = state.observable(time);
13
- const currentDay = state.observable(clearTime(time));
14
- const timeToSecond = (60 - time.getSeconds() + 1) * 1000;
12
+ var time = /* @__PURE__ */ new Date();
13
+ var currentTime = state.observable(time);
14
+ var currentDay = state.observable(clearTime(time));
15
+ var timeToSecond = (60 - time.getSeconds() + 1) * 1e3;
15
16
  function update() {
16
- const now = new Date();
17
- currentTime.set(now);
18
- if (now.getDate() !== time.getDate()) {
19
- currentDay.set(clearTime(now));
20
- }
21
- time = now;
17
+ const now = /* @__PURE__ */ new Date();
18
+ currentTime.set(now);
19
+ if (now.getDate() !== time.getDate()) {
20
+ currentDay.set(clearTime(now));
21
+ }
22
+ time = now;
22
23
  }
23
24
  setTimeout(() => {
24
- update();
25
- setInterval(update, MSPerMinute);
25
+ update();
26
+ setInterval(update, MSPerMinute);
26
27
  }, timeToSecond);
27
28
 
28
29
  exports.currentDay = currentDay;
29
30
  exports.currentTime = currentTime;
30
- //# sourceMappingURL=time.js.map
package/helpers/time.mjs CHANGED
@@ -1,27 +1,27 @@
1
1
  import { observable } from '@legendapp/state';
2
2
 
3
- const MSPerMinute = 60000;
3
+ // src/helpers/time.ts
4
+ var MSPerMinute = 6e4;
4
5
  function clearTime(date) {
5
- date = new Date(date);
6
- date.setHours(0, 0, 0, 0);
7
- return date;
6
+ date = new Date(date);
7
+ date.setHours(0, 0, 0, 0);
8
+ return date;
8
9
  }
9
- let time = new Date();
10
- const currentTime = observable(time);
11
- const currentDay = observable(clearTime(time));
12
- const timeToSecond = (60 - time.getSeconds() + 1) * 1000;
10
+ var time = /* @__PURE__ */ new Date();
11
+ var currentTime = observable(time);
12
+ var currentDay = observable(clearTime(time));
13
+ var timeToSecond = (60 - time.getSeconds() + 1) * 1e3;
13
14
  function update() {
14
- const now = new Date();
15
- currentTime.set(now);
16
- if (now.getDate() !== time.getDate()) {
17
- currentDay.set(clearTime(now));
18
- }
19
- time = now;
15
+ const now = /* @__PURE__ */ new Date();
16
+ currentTime.set(now);
17
+ if (now.getDate() !== time.getDate()) {
18
+ currentDay.set(clearTime(now));
19
+ }
20
+ time = now;
20
21
  }
21
22
  setTimeout(() => {
22
- update();
23
- setInterval(update, MSPerMinute);
23
+ update();
24
+ setInterval(update, MSPerMinute);
24
25
  }, timeToSecond);
25
26
 
26
27
  export { currentDay, currentTime };
27
- //# sourceMappingURL=time.mjs.map
@@ -0,0 +1,6 @@
1
+ import { ObservableParam } from '@legendapp/state';
2
+
3
+ type TimestampAsString = string;
4
+ declare function trackHistory<T>(value$: ObservableParam<T>, targetObservable?: ObservableParam<Record<TimestampAsString, Partial<T>>>): ObservableParam<Record<TimestampAsString, any>>;
5
+
6
+ export { trackHistory };
@@ -0,0 +1,6 @@
1
+ import { ObservableParam } from '@legendapp/state';
2
+
3
+ type TimestampAsString = string;
4
+ declare function trackHistory<T>(value$: ObservableParam<T>, targetObservable?: ObservableParam<Record<TimestampAsString, Partial<T>>>): ObservableParam<Record<TimestampAsString, any>>;
5
+
6
+ export { trackHistory };
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ var state = require('@legendapp/state');
4
+
5
+ // src/helpers/trackHistory.ts
6
+ function trackHistory(value$, targetObservable) {
7
+ const history = targetObservable != null ? targetObservable : state.observable();
8
+ value$.onChange(({ loading, remote, changes }) => {
9
+ if (!loading && !remote) {
10
+ const time = Date.now().toString();
11
+ for (let i = 0; i < changes.length; i++) {
12
+ const { path, prevAtPath, pathTypes } = changes[i];
13
+ const obj = state.constructObjectWithPath(path, pathTypes, prevAtPath);
14
+ state.mergeIntoObservable(history[time], obj);
15
+ }
16
+ }
17
+ });
18
+ return history;
19
+ }
20
+
21
+ exports.trackHistory = trackHistory;
@@ -0,0 +1,19 @@
1
+ import { observable, constructObjectWithPath, mergeIntoObservable } from '@legendapp/state';
2
+
3
+ // src/helpers/trackHistory.ts
4
+ function trackHistory(value$, targetObservable) {
5
+ const history = targetObservable != null ? targetObservable : observable();
6
+ value$.onChange(({ loading, remote, changes }) => {
7
+ if (!loading && !remote) {
8
+ const time = Date.now().toString();
9
+ for (let i = 0; i < changes.length; i++) {
10
+ const { path, prevAtPath, pathTypes } = changes[i];
11
+ const obj = constructObjectWithPath(path, pathTypes, prevAtPath);
12
+ mergeIntoObservable(history[time], obj);
13
+ }
14
+ }
15
+ });
16
+ return history;
17
+ }
18
+
19
+ export { trackHistory };
@@ -0,0 +1,37 @@
1
+ import { ObservablePrimitive } from '@legendapp/state';
2
+
3
+ type UndoRedoOptions = {
4
+ limit?: number;
5
+ };
6
+ /**
7
+ * Usage:
8
+ *
9
+ * Use this function to add undo/redo functionality to an observable.
10
+ *
11
+ * You can monitor how many undos or redos are available to enable/disable undo/redo
12
+ * UI elements with undo$ and redo$.
13
+ *
14
+ * If you undo and then make a change, it'll delete any redos and add the change, as expected.
15
+ *
16
+ * If you don't pass in a limit, it will keep all history. This means it can grow indefinitely.
17
+ *
18
+ * ```typescript
19
+ * const obs$ = observable({ test: 'hi', test2: 'a' });
20
+ * const { undo, redo, undos$, redos$, getHistory } = undoRedo(obs$, { limit: 40 });
21
+ * obs$.test.set('hello');
22
+ * undo();
23
+ * redo();
24
+ * // observables for # of undos/redos available
25
+ * undos$.get();
26
+ * redos$.get();
27
+ * ```
28
+ */
29
+ declare function undoRedo<T>(obs$: ObservablePrimitive<T>, options?: UndoRedoOptions): {
30
+ undo(): void;
31
+ redo(): void;
32
+ undos$: ObservablePrimitive<number>;
33
+ redos$: ObservablePrimitive<number>;
34
+ getHistory: () => T[];
35
+ };
36
+
37
+ export { undoRedo };
@@ -0,0 +1,37 @@
1
+ import { ObservablePrimitive } from '@legendapp/state';
2
+
3
+ type UndoRedoOptions = {
4
+ limit?: number;
5
+ };
6
+ /**
7
+ * Usage:
8
+ *
9
+ * Use this function to add undo/redo functionality to an observable.
10
+ *
11
+ * You can monitor how many undos or redos are available to enable/disable undo/redo
12
+ * UI elements with undo$ and redo$.
13
+ *
14
+ * If you undo and then make a change, it'll delete any redos and add the change, as expected.
15
+ *
16
+ * If you don't pass in a limit, it will keep all history. This means it can grow indefinitely.
17
+ *
18
+ * ```typescript
19
+ * const obs$ = observable({ test: 'hi', test2: 'a' });
20
+ * const { undo, redo, undos$, redos$, getHistory } = undoRedo(obs$, { limit: 40 });
21
+ * obs$.test.set('hello');
22
+ * undo();
23
+ * redo();
24
+ * // observables for # of undos/redos available
25
+ * undos$.get();
26
+ * redos$.get();
27
+ * ```
28
+ */
29
+ declare function undoRedo<T>(obs$: ObservablePrimitive<T>, options?: UndoRedoOptions): {
30
+ undo(): void;
31
+ redo(): void;
32
+ undos$: ObservablePrimitive<number>;
33
+ redos$: ObservablePrimitive<number>;
34
+ getHistory: () => T[];
35
+ };
36
+
37
+ export { undoRedo };
@@ -0,0 +1,68 @@
1
+ 'use strict';
2
+
3
+ var state = require('@legendapp/state');
4
+
5
+ // src/helpers/undoRedo.ts
6
+ function undoRedo(obs$, options) {
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;
26
+ }
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
+ };
66
+ }
67
+
68
+ exports.undoRedo = undoRedo;
@@ -0,0 +1,66 @@
1
+ import { observable, internal } from '@legendapp/state';
2
+
3
+ // src/helpers/undoRedo.ts
4
+ function undoRedo(obs$, options) {
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;
24
+ }
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
+ };
64
+ }
65
+
66
+ export { undoRedo };