@choksheak/react-utils 0.4.7 → 0.4.8

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 (108) hide show
  1. package/NoSSR.cjs +5 -2
  2. package/{NoSSR.mjs → NoSSR.js} +4 -1
  3. package/NoSSR.min.cjs +14 -1
  4. package/NoSSR.min.cjs.map +1 -1
  5. package/NoSSR.min.js +15 -0
  6. package/{NoSSR.min.mjs.map → NoSSR.min.js.map} +1 -1
  7. package/chunk-4YV72T4D.min.js +105 -0
  8. package/{storage.min.mjs.map → chunk-4YV72T4D.min.js.map} +1 -1
  9. package/chunk-5PDGU37E.cjs +104 -0
  10. package/{storage.mjs → chunk-7CPBZFDV.js} +5 -3
  11. package/{stringify.mjs → chunk-7XC6EVMX.js} +2 -0
  12. package/chunk-AXDXFCZV.min.js +35 -0
  13. package/{stringify.min.mjs.map → chunk-AXDXFCZV.min.js.map} +1 -1
  14. package/chunk-C6X4GPZ4.min.cjs +596 -0
  15. package/chunk-C6X4GPZ4.min.cjs.map +1 -0
  16. package/{sharedState.mjs → chunk-CIDQGOPA.js} +23 -8
  17. package/chunk-CYTO5EZE.cjs +34 -0
  18. package/chunk-HFIQE4XZ.cjs +595 -0
  19. package/chunk-IS3BZTWE.cjs +35 -0
  20. package/chunk-JYZY2SHD.min.js +596 -0
  21. package/chunk-JYZY2SHD.min.js.map +1 -0
  22. package/chunk-K2L3OVGY.min.cjs +18 -0
  23. package/chunk-K2L3OVGY.min.cjs.map +1 -0
  24. package/chunk-KGDXU6Q2.min.cjs +222 -0
  25. package/chunk-KGDXU6Q2.min.cjs.map +1 -0
  26. package/chunk-MNWSPF5J.min.js +18 -0
  27. package/{useDeepMemo.min.mjs.map → chunk-MNWSPF5J.min.js.map} +1 -1
  28. package/chunk-P2E73RBM.min.cjs +35 -0
  29. package/chunk-P2E73RBM.min.cjs.map +1 -0
  30. package/chunk-PCGGKBOL.min.js +222 -0
  31. package/chunk-PCGGKBOL.min.js.map +1 -0
  32. package/chunk-PHITDXZT.min.js +36 -0
  33. package/chunk-PHITDXZT.min.js.map +1 -0
  34. package/{sharedQuery.mjs → chunk-RFKUMDNU.js} +17 -6
  35. package/chunk-RRQBFF4W.min.cjs +105 -0
  36. package/chunk-RRQBFF4W.min.cjs.map +1 -0
  37. package/chunk-TST6JKQ5.min.cjs +36 -0
  38. package/chunk-TST6JKQ5.min.cjs.map +1 -0
  39. package/chunk-U67V476Y.js +35 -0
  40. package/chunk-UNDIVFP5.cjs +221 -0
  41. package/chunk-WXLUEDBO.cjs +17 -0
  42. package/{useDeepMemo.mjs → chunk-YTXGK6BR.js} +2 -0
  43. package/dist-4BDJ7HYS.min.js +546 -0
  44. package/dist-4BDJ7HYS.min.js.map +1 -0
  45. package/dist-HKOCAKHU.min.cjs +546 -0
  46. package/dist-HKOCAKHU.min.cjs.map +1 -0
  47. package/dist-RQU6CWRG.js +545 -0
  48. package/dist-UKP7SPWZ.cjs +545 -0
  49. package/index.cjs +43 -5
  50. package/index.d.cts +2 -2
  51. package/index.d.ts +2 -2
  52. package/index.js +43 -0
  53. package/index.min.cjs +43 -1
  54. package/index.min.cjs.map +1 -1
  55. package/index.min.js +44 -0
  56. package/index.min.js.map +1 -0
  57. package/magic-string.es-2AQNCEBL.min.js +1309 -0
  58. package/magic-string.es-2AQNCEBL.min.js.map +1 -0
  59. package/magic-string.es-HC5PDAU3.cjs +1308 -0
  60. package/magic-string.es-KDMUXKGI.js +1308 -0
  61. package/magic-string.es-XJ27RPMN.min.cjs +1309 -0
  62. package/magic-string.es-XJ27RPMN.min.cjs.map +1 -0
  63. package/package.json +48 -8
  64. package/sharedQuery.cjs +8 -575
  65. package/sharedQuery.d.cts +7 -92
  66. package/sharedQuery.d.ts +7 -92
  67. package/sharedQuery.js +17 -0
  68. package/sharedQuery.min.cjs +17 -1
  69. package/sharedQuery.min.cjs.map +1 -1
  70. package/sharedQuery.min.js +18 -0
  71. package/sharedQuery.min.js.map +1 -0
  72. package/sharedState.cjs +11 -199
  73. package/sharedState.d.cts +12 -57
  74. package/sharedState.d.ts +12 -57
  75. package/sharedState.js +18 -0
  76. package/sharedState.min.cjs +18 -1
  77. package/sharedState.min.cjs.map +1 -1
  78. package/sharedState.min.js +19 -0
  79. package/sharedState.min.js.map +1 -0
  80. package/storage.cjs +7 -98
  81. package/storage.js +11 -0
  82. package/storage.min.cjs +11 -1
  83. package/storage.min.cjs.map +1 -1
  84. package/storage.min.js +12 -0
  85. package/storage.min.js.map +1 -0
  86. package/stringify.cjs +5 -30
  87. package/stringify.js +7 -0
  88. package/stringify.min.cjs +7 -1
  89. package/stringify.min.cjs.map +1 -1
  90. package/stringify.min.js +8 -0
  91. package/stringify.min.js.map +1 -0
  92. package/useDeepMemo.cjs +5 -13
  93. package/useDeepMemo.js +7 -0
  94. package/useDeepMemo.min.cjs +7 -1
  95. package/useDeepMemo.min.cjs.map +1 -1
  96. package/useDeepMemo.min.js +8 -0
  97. package/useDeepMemo.min.js.map +1 -0
  98. package/NoSSR.min.mjs +0 -2
  99. package/index.min.mjs +0 -2
  100. package/index.min.mjs.map +0 -1
  101. package/index.mjs +0 -5
  102. package/sharedQuery.min.mjs +0 -2
  103. package/sharedQuery.min.mjs.map +0 -1
  104. package/sharedState.min.mjs +0 -2
  105. package/sharedState.min.mjs.map +0 -1
  106. package/storage.min.mjs +0 -2
  107. package/stringify.min.mjs +0 -2
  108. package/useDeepMemo.min.mjs +0 -2
@@ -0,0 +1,221 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2
+
3
+ var _chunk5PDGU37Ecjs = require('./chunk-5PDGU37E.cjs');
4
+
5
+ // src/sharedState.ts
6
+ var _timeConstants = require('@choksheak/ts-utils/timeConstants');
7
+ var _react = require('react');
8
+ var SharedStateConfig = {
9
+ /**
10
+ * Default duration for storage expiration in milliseconds. Initial value
11
+ * is 30 days, but can be changed to anything.
12
+ */
13
+ storeExpiryMs: 30 * _timeConstants.MS_PER_DAY,
14
+ /**
15
+ * True to load from storage only on mount, false (default) to load in
16
+ * the top level scope when the shared state is defined.
17
+ */
18
+ lazyLoad: false,
19
+ /** Default to log to console. */
20
+ log: (...args) => console.log("[sharedState]", ...args)
21
+ };
22
+ function configureSharedState(config) {
23
+ Object.assign(SharedStateConfig, config);
24
+ }
25
+ function createPubSubStore() {
26
+ const dataByKey = /* @__PURE__ */ new Map();
27
+ const subscribersByKey = /* @__PURE__ */ new Map();
28
+ function notify(key, next, prev) {
29
+ const subscribers = subscribersByKey.get(key);
30
+ if (!subscribers) return;
31
+ const copy = Array.from(subscribers.values());
32
+ for (const subscriber of copy) {
33
+ try {
34
+ subscriber(next, prev);
35
+ } catch (e) {
36
+ console.error(`Error invoking subscriber:`, e);
37
+ }
38
+ }
39
+ }
40
+ return {
41
+ // This could be undefined if T includes undefined, else it should never
42
+ // return undefined because the user has to always specify the default value.
43
+ get(key) {
44
+ return dataByKey.get(key);
45
+ },
46
+ set(key, value) {
47
+ const prev = dataByKey.get(key);
48
+ if (prev === value) return;
49
+ dataByKey.set(key, value);
50
+ notify(key, value, prev);
51
+ },
52
+ delete(key) {
53
+ dataByKey.delete(key);
54
+ },
55
+ setNoNotify(key, value) {
56
+ dataByKey.set(key, value);
57
+ },
58
+ // Subscribe to changes of a specific key
59
+ subscribe(key, subscriber) {
60
+ let subscribers = subscribersByKey.get(key);
61
+ if (!subscribers) {
62
+ subscribers = /* @__PURE__ */ new Set();
63
+ subscribersByKey.set(key, subscribers);
64
+ }
65
+ subscribers.add(subscriber);
66
+ return () => {
67
+ subscribers.delete(subscriber);
68
+ if (subscribers.size === 0) subscribersByKey.delete(key);
69
+ };
70
+ }
71
+ };
72
+ }
73
+ function getOrCreatePubSubStore() {
74
+ if (typeof window !== "undefined") {
75
+ const t = window.globalThis;
76
+ return t.reactUtilsPubSubStore || (t.reactUtilsPubSubStore = createPubSubStore());
77
+ }
78
+ return createPubSubStore();
79
+ }
80
+ var pubSubStore = getOrCreatePubSubStore();
81
+ var STORAGE_KEY = "state";
82
+ var stateKey = 0;
83
+ function getNextStateKey() {
84
+ if (typeof window !== "undefined") {
85
+ const t = window.globalThis;
86
+ t.reactUtilsStateKey = (_nullishCoalesce(t.reactUtilsStateKey, () => ( 0))) + 1;
87
+ return t.reactUtilsStateKey;
88
+ }
89
+ return ++stateKey;
90
+ }
91
+ function sharedState(defaultValue, options) {
92
+ let readyResolve = () => {
93
+ };
94
+ const readyPromise = new Promise((resolve) => {
95
+ readyResolve = resolve;
96
+ });
97
+ const resolveReadyPromise = readyResolve;
98
+ const log = _optionalChain([options, 'optionalAccess', _ => _.log]) || SharedStateConfig.log;
99
+ const pubSubKey = String(getNextStateKey());
100
+ if (log && _optionalChain([options, 'optionalAccess', _2 => _2.name])) {
101
+ log(`${options.name} -> ${pubSubKey}`);
102
+ }
103
+ const storageAdapter = _chunk5PDGU37Ecjs.getStorageAdapter.call(void 0,
104
+ options,
105
+ SharedStateConfig.storeExpiryMs
106
+ );
107
+ pubSubStore.setNoNotify(pubSubKey, defaultValue);
108
+ const lazyLoad = _optionalChain([options, 'optionalAccess', _3 => _3.lazyLoad]);
109
+ const lazy = lazyLoad !== void 0 ? lazyLoad : SharedStateConfig.lazyLoad;
110
+ function completeInit(stored) {
111
+ const value = stored !== void 0 ? stored : defaultValue;
112
+ pubSubStore.set(pubSubKey, value);
113
+ obj.initDone = true;
114
+ resolveReadyPromise();
115
+ }
116
+ const obj = {
117
+ name: _optionalChain([options, 'optionalAccess', _4 => _4.name]),
118
+ defaultValue,
119
+ pubSubKey,
120
+ /** Allow users to await for the completion of data init. */
121
+ readyPromise,
122
+ storeExpiryMs: SharedStateConfig.storeExpiryMs,
123
+ lazy,
124
+ storageAdapter,
125
+ initStarted: false,
126
+ initDone: false,
127
+ /** `subscribe` function required by useSyncExternalStore. */
128
+ subscribe(subscriber) {
129
+ return pubSubStore.subscribe(pubSubKey, () => subscriber());
130
+ },
131
+ /** Returns true if there is a backing store. */
132
+ hasStore() {
133
+ return Boolean(storageAdapter);
134
+ },
135
+ /**
136
+ * Set a value directly into the store. Does nothing if there is no
137
+ * backing store.
138
+ *
139
+ * Warning: If you want to set the value properly, you should use
140
+ * `setValue()` instead which also updates the in-memory copy.
141
+ */
142
+ setInStore(value) {
143
+ return _optionalChain([storageAdapter, 'optionalAccess', _5 => _5.set, 'call', _6 => _6(STORAGE_KEY, value)]);
144
+ },
145
+ /**
146
+ * Get a value directly from the store. Returns undefined if there is no
147
+ * backing store.
148
+ */
149
+ getFromStore() {
150
+ return _optionalChain([storageAdapter, 'optionalAccess', _7 => _7.get, 'call', _8 => _8(STORAGE_KEY)]);
151
+ },
152
+ /**
153
+ * Get the value of the shared state.
154
+ * `getSnapshot` function — must return same ref if value unchanged.
155
+ * Note that this would return the default value if data init from store is
156
+ * not completed yet.
157
+ */
158
+ getSnapshot() {
159
+ return pubSubStore.get(pubSubKey);
160
+ },
161
+ /** Set the value of the shared state. */
162
+ setValue(next) {
163
+ if (typeof next === "function") {
164
+ const prev = pubSubStore.get(pubSubKey);
165
+ next = next(prev);
166
+ }
167
+ pubSubStore.set(pubSubKey, next);
168
+ void obj.setInStore(next);
169
+ },
170
+ /**
171
+ * Remove the value of the shared state. Actually this just sets the value
172
+ * back to the given default value.
173
+ */
174
+ delete() {
175
+ pubSubStore.set(pubSubKey, defaultValue);
176
+ return _optionalChain([storageAdapter, 'optionalAccess', _9 => _9.set, 'call', _10 => _10(STORAGE_KEY, defaultValue)]);
177
+ },
178
+ /** Initial default handling. */
179
+ initDefaultValueOnce() {
180
+ if (obj.initDone || obj.initStarted) return;
181
+ obj.initStarted = true;
182
+ const maybePromise = obj.getFromStore();
183
+ if (maybePromise instanceof Promise) {
184
+ return maybePromise.then(completeInit);
185
+ } else {
186
+ const stored = maybePromise;
187
+ completeInit(stored);
188
+ }
189
+ }
190
+ };
191
+ if (!lazy) {
192
+ obj.initDefaultValueOnce();
193
+ }
194
+ return obj;
195
+ }
196
+ function useSharedState(state) {
197
+ void state.initDefaultValueOnce();
198
+ const value = _react.useSyncExternalStore.call(void 0,
199
+ (onStoreChange) => state.subscribe(onStoreChange),
200
+ () => state.getSnapshot(),
201
+ // getServerSnapshot is needed to prevent SSR dev errors in nextjs.
202
+ () => state.getSnapshot()
203
+ );
204
+ return [value, state.setValue];
205
+ }
206
+ function useSharedStateValue(state) {
207
+ return useSharedState(state)[0];
208
+ }
209
+ function useSharedStateSetter(state) {
210
+ void state.initDefaultValueOnce();
211
+ return state.setValue;
212
+ }
213
+
214
+
215
+
216
+
217
+
218
+
219
+
220
+
221
+ exports.SharedStateConfig = SharedStateConfig; exports.configureSharedState = configureSharedState; exports.sharedState = sharedState; exports.useSharedState = useSharedState; exports.useSharedStateValue = useSharedStateValue; exports.useSharedStateSetter = useSharedStateSetter;
@@ -0,0 +1,17 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/useDeepMemo.ts
2
+ var _lodashes = require('lodash-es');
3
+ var _react = require('react');
4
+ function useDeepMemo(value) {
5
+ const [state, setState] = _react.useState.call(void 0, value);
6
+ return _react.useMemo.call(void 0, () => {
7
+ if (_lodashes.isEqual.call(void 0, state, value)) {
8
+ return state;
9
+ }
10
+ setState(value);
11
+ return value;
12
+ }, [state, value]);
13
+ }
14
+
15
+
16
+
17
+ exports.useDeepMemo = useDeepMemo;
@@ -1,3 +1,4 @@
1
+ // src/useDeepMemo.ts
1
2
  import { isEqual } from "lodash-es";
2
3
  import { useMemo, useState } from "react";
3
4
  function useDeepMemo(value) {
@@ -10,6 +11,7 @@ function useDeepMemo(value) {
10
11
  return value;
11
12
  }, [state, value]);
12
13
  }
14
+
13
15
  export {
14
16
  useDeepMemo
15
17
  };