@depup/jotai 2.18.0-depup.0

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 (215) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +25 -0
  3. package/babel/plugin-debug-label.d.ts +5 -0
  4. package/babel/plugin-debug-label.js +54 -0
  5. package/babel/plugin-react-refresh.d.ts +5 -0
  6. package/babel/plugin-react-refresh.js +72 -0
  7. package/babel/preset.d.ts +6 -0
  8. package/babel/preset.js +110 -0
  9. package/babel/utils.d.ts +5 -0
  10. package/benchmarks/simple-read.d.ts +2 -0
  11. package/benchmarks/simple-write.d.ts +2 -0
  12. package/benchmarks/subscribe-write.d.ts +2 -0
  13. package/changes.json +5 -0
  14. package/esm/babel/plugin-debug-label.d.mts +5 -0
  15. package/esm/babel/plugin-debug-label.mjs +103 -0
  16. package/esm/babel/plugin-react-refresh.d.mts +5 -0
  17. package/esm/babel/plugin-react-refresh.mjs +121 -0
  18. package/esm/babel/preset.d.mts +6 -0
  19. package/esm/babel/preset.mjs +179 -0
  20. package/esm/babel/utils.d.mts +5 -0
  21. package/esm/index.d.mts +2 -0
  22. package/esm/index.mjs +2 -0
  23. package/esm/react/Provider.d.mts +16 -0
  24. package/esm/react/useAtom.d.mts +13 -0
  25. package/esm/react/useAtomValue.d.mts +9 -0
  26. package/esm/react/useSetAtom.d.mts +7 -0
  27. package/esm/react/utils/useAtomCallback.d.mts +5 -0
  28. package/esm/react/utils/useHydrateAtoms.d.mts +14 -0
  29. package/esm/react/utils/useReducerAtom.d.mts +14 -0
  30. package/esm/react/utils/useResetAtom.d.mts +6 -0
  31. package/esm/react/utils.d.mts +4 -0
  32. package/esm/react/utils.mjs +57 -0
  33. package/esm/react.d.mts +4 -0
  34. package/esm/react.mjs +182 -0
  35. package/esm/utils.d.mts +2 -0
  36. package/esm/utils.mjs +2 -0
  37. package/esm/vanilla/atom.d.mts +47 -0
  38. package/esm/vanilla/internals.d.mts +164 -0
  39. package/esm/vanilla/internals.mjs +715 -0
  40. package/esm/vanilla/store.d.mts +5 -0
  41. package/esm/vanilla/typeUtils.d.mts +7 -0
  42. package/esm/vanilla/utils/atomFamily.d.mts +55 -0
  43. package/esm/vanilla/utils/atomWithDefault.d.mts +6 -0
  44. package/esm/vanilla/utils/atomWithLazy.d.mts +2 -0
  45. package/esm/vanilla/utils/atomWithObservable.d.mts +32 -0
  46. package/esm/vanilla/utils/atomWithReducer.d.mts +3 -0
  47. package/esm/vanilla/utils/atomWithRefresh.d.mts +6 -0
  48. package/esm/vanilla/utils/atomWithReset.d.mts +8 -0
  49. package/esm/vanilla/utils/atomWithStorage.d.mts +50 -0
  50. package/esm/vanilla/utils/constants.d.mts +1 -0
  51. package/esm/vanilla/utils/freezeAtom.d.mts +6 -0
  52. package/esm/vanilla/utils/loadable.d.mts +33 -0
  53. package/esm/vanilla/utils/selectAtom.d.mts +2 -0
  54. package/esm/vanilla/utils/splitAtom.d.mts +16 -0
  55. package/esm/vanilla/utils/unwrap.d.mts +5 -0
  56. package/esm/vanilla/utils.d.mts +14 -0
  57. package/esm/vanilla/utils.mjs +736 -0
  58. package/esm/vanilla.d.mts +4 -0
  59. package/esm/vanilla.mjs +59 -0
  60. package/index.d.ts +2 -0
  61. package/index.js +19 -0
  62. package/package.json +125 -0
  63. package/react/Provider.d.ts +16 -0
  64. package/react/useAtom.d.ts +13 -0
  65. package/react/useAtomValue.d.ts +9 -0
  66. package/react/useSetAtom.d.ts +7 -0
  67. package/react/utils/useAtomCallback.d.ts +5 -0
  68. package/react/utils/useHydrateAtoms.d.ts +14 -0
  69. package/react/utils/useReducerAtom.d.ts +14 -0
  70. package/react/utils/useResetAtom.d.ts +6 -0
  71. package/react/utils.d.ts +4 -0
  72. package/react/utils.js +100 -0
  73. package/react.d.ts +4 -0
  74. package/react.js +182 -0
  75. package/system/babel/plugin-debug-label.development.js +113 -0
  76. package/system/babel/plugin-debug-label.production.js +4 -0
  77. package/system/babel/plugin-react-refresh.development.js +131 -0
  78. package/system/babel/plugin-react-refresh.production.js +11 -0
  79. package/system/babel/preset.development.js +189 -0
  80. package/system/babel/preset.production.js +14 -0
  81. package/system/index.development.js +27 -0
  82. package/system/index.production.js +1 -0
  83. package/system/react/utils.development.js +79 -0
  84. package/system/react/utils.production.js +2 -0
  85. package/system/react.development.js +210 -0
  86. package/system/react.production.js +2 -0
  87. package/system/utils.development.js +27 -0
  88. package/system/utils.production.js +1 -0
  89. package/system/vanilla/internals.development.js +735 -0
  90. package/system/vanilla/internals.production.js +1 -0
  91. package/system/vanilla/utils.development.js +764 -0
  92. package/system/vanilla/utils.production.js +1 -0
  93. package/system/vanilla.development.js +75 -0
  94. package/system/vanilla.production.js +1 -0
  95. package/ts3.8/babel/plugin-debug-label.d.ts +6 -0
  96. package/ts3.8/babel/plugin-react-refresh.d.ts +6 -0
  97. package/ts3.8/babel/preset.d.ts +7 -0
  98. package/ts3.8/babel/utils.d.ts +6 -0
  99. package/ts3.8/benchmarks/simple-read.d.ts +3 -0
  100. package/ts3.8/benchmarks/simple-write.d.ts +3 -0
  101. package/ts3.8/benchmarks/subscribe-write.d.ts +3 -0
  102. package/ts3.8/esm/babel/plugin-debug-label.d.ts +6 -0
  103. package/ts3.8/esm/babel/plugin-react-refresh.d.ts +6 -0
  104. package/ts3.8/esm/babel/preset.d.ts +7 -0
  105. package/ts3.8/esm/babel/utils.d.ts +6 -0
  106. package/ts3.8/esm/index.d.ts +3 -0
  107. package/ts3.8/esm/react/Provider.d.ts +17 -0
  108. package/ts3.8/esm/react/useAtom.d.ts +28 -0
  109. package/ts3.8/esm/react/useAtomValue.d.ts +10 -0
  110. package/ts3.8/esm/react/useSetAtom.d.ts +8 -0
  111. package/ts3.8/esm/react/utils/useAtomCallback.d.ts +6 -0
  112. package/ts3.8/esm/react/utils/useHydrateAtoms.d.ts +27 -0
  113. package/ts3.8/esm/react/utils/useReducerAtom.d.ts +21 -0
  114. package/ts3.8/esm/react/utils/useResetAtom.d.ts +9 -0
  115. package/ts3.8/esm/react/utils.d.ts +5 -0
  116. package/ts3.8/esm/react.d.ts +5 -0
  117. package/ts3.8/esm/utils.d.ts +3 -0
  118. package/ts3.8/esm/vanilla/atom.d.ts +48 -0
  119. package/ts3.8/esm/vanilla/internals.d.ts +165 -0
  120. package/ts3.8/esm/vanilla/store.d.ts +6 -0
  121. package/ts3.8/esm/vanilla/typeUtils.d.ts +8 -0
  122. package/ts3.8/esm/vanilla/utils/atomFamily.d.ts +56 -0
  123. package/ts3.8/esm/vanilla/utils/atomWithDefault.d.ts +11 -0
  124. package/ts3.8/esm/vanilla/utils/atomWithLazy.d.ts +3 -0
  125. package/ts3.8/esm/vanilla/utils/atomWithObservable.d.ts +37 -0
  126. package/ts3.8/esm/vanilla/utils/atomWithReducer.d.ts +8 -0
  127. package/ts3.8/esm/vanilla/utils/atomWithRefresh.d.ts +10 -0
  128. package/ts3.8/esm/vanilla/utils/atomWithReset.d.ts +11 -0
  129. package/ts3.8/esm/vanilla/utils/atomWithStorage.d.ts +53 -0
  130. package/ts3.8/esm/vanilla/utils/constants.d.ts +2 -0
  131. package/ts3.8/esm/vanilla/utils/freezeAtom.d.ts +7 -0
  132. package/ts3.8/esm/vanilla/utils/loadable.d.ts +34 -0
  133. package/ts3.8/esm/vanilla/utils/selectAtom.d.ts +3 -0
  134. package/ts3.8/esm/vanilla/utils/splitAtom.d.ts +21 -0
  135. package/ts3.8/esm/vanilla/utils/unwrap.d.ts +6 -0
  136. package/ts3.8/esm/vanilla/utils.d.ts +15 -0
  137. package/ts3.8/esm/vanilla.d.ts +5 -0
  138. package/ts3.8/index.d.ts +3 -0
  139. package/ts3.8/react/Provider.d.ts +17 -0
  140. package/ts3.8/react/useAtom.d.ts +28 -0
  141. package/ts3.8/react/useAtomValue.d.ts +10 -0
  142. package/ts3.8/react/useSetAtom.d.ts +8 -0
  143. package/ts3.8/react/utils/useAtomCallback.d.ts +6 -0
  144. package/ts3.8/react/utils/useHydrateAtoms.d.ts +27 -0
  145. package/ts3.8/react/utils/useReducerAtom.d.ts +21 -0
  146. package/ts3.8/react/utils/useResetAtom.d.ts +9 -0
  147. package/ts3.8/react/utils.d.ts +5 -0
  148. package/ts3.8/react.d.ts +5 -0
  149. package/ts3.8/utils.d.ts +3 -0
  150. package/ts3.8/vanilla/atom.d.ts +48 -0
  151. package/ts3.8/vanilla/internals.d.ts +165 -0
  152. package/ts3.8/vanilla/store.d.ts +6 -0
  153. package/ts3.8/vanilla/typeUtils.d.ts +8 -0
  154. package/ts3.8/vanilla/utils/atomFamily.d.ts +56 -0
  155. package/ts3.8/vanilla/utils/atomWithDefault.d.ts +11 -0
  156. package/ts3.8/vanilla/utils/atomWithLazy.d.ts +3 -0
  157. package/ts3.8/vanilla/utils/atomWithObservable.d.ts +37 -0
  158. package/ts3.8/vanilla/utils/atomWithReducer.d.ts +8 -0
  159. package/ts3.8/vanilla/utils/atomWithRefresh.d.ts +10 -0
  160. package/ts3.8/vanilla/utils/atomWithReset.d.ts +11 -0
  161. package/ts3.8/vanilla/utils/atomWithStorage.d.ts +53 -0
  162. package/ts3.8/vanilla/utils/constants.d.ts +2 -0
  163. package/ts3.8/vanilla/utils/freezeAtom.d.ts +7 -0
  164. package/ts3.8/vanilla/utils/loadable.d.ts +34 -0
  165. package/ts3.8/vanilla/utils/selectAtom.d.ts +3 -0
  166. package/ts3.8/vanilla/utils/splitAtom.d.ts +21 -0
  167. package/ts3.8/vanilla/utils/unwrap.d.ts +6 -0
  168. package/ts3.8/vanilla/utils.d.ts +15 -0
  169. package/ts3.8/vanilla.d.ts +5 -0
  170. package/ts_version_3.8_and_above_is_required.d.ts +0 -0
  171. package/umd/babel/plugin-debug-label.development.js +58 -0
  172. package/umd/babel/plugin-debug-label.production.js +1 -0
  173. package/umd/babel/plugin-react-refresh.development.js +76 -0
  174. package/umd/babel/plugin-react-refresh.production.js +1 -0
  175. package/umd/babel/preset.development.js +114 -0
  176. package/umd/babel/preset.production.js +1 -0
  177. package/umd/index.development.js +20 -0
  178. package/umd/index.production.js +1 -0
  179. package/umd/react/utils.development.js +101 -0
  180. package/umd/react/utils.production.js +1 -0
  181. package/umd/react.development.js +184 -0
  182. package/umd/react.production.js +1 -0
  183. package/umd/utils.development.js +20 -0
  184. package/umd/utils.production.js +1 -0
  185. package/umd/vanilla/internals.development.js +785 -0
  186. package/umd/vanilla/internals.production.js +1 -0
  187. package/umd/vanilla/utils.development.js +858 -0
  188. package/umd/vanilla/utils.production.js +1 -0
  189. package/umd/vanilla.development.js +64 -0
  190. package/umd/vanilla.production.js +1 -0
  191. package/utils.d.ts +2 -0
  192. package/utils.js +19 -0
  193. package/vanilla/atom.d.ts +47 -0
  194. package/vanilla/internals.d.ts +164 -0
  195. package/vanilla/internals.js +779 -0
  196. package/vanilla/store.d.ts +5 -0
  197. package/vanilla/typeUtils.d.ts +7 -0
  198. package/vanilla/utils/atomFamily.d.ts +55 -0
  199. package/vanilla/utils/atomWithDefault.d.ts +6 -0
  200. package/vanilla/utils/atomWithLazy.d.ts +2 -0
  201. package/vanilla/utils/atomWithObservable.d.ts +32 -0
  202. package/vanilla/utils/atomWithReducer.d.ts +3 -0
  203. package/vanilla/utils/atomWithRefresh.d.ts +6 -0
  204. package/vanilla/utils/atomWithReset.d.ts +8 -0
  205. package/vanilla/utils/atomWithStorage.d.ts +50 -0
  206. package/vanilla/utils/constants.d.ts +1 -0
  207. package/vanilla/utils/freezeAtom.d.ts +6 -0
  208. package/vanilla/utils/loadable.d.ts +33 -0
  209. package/vanilla/utils/selectAtom.d.ts +2 -0
  210. package/vanilla/utils/splitAtom.d.ts +16 -0
  211. package/vanilla/utils/unwrap.d.ts +5 -0
  212. package/vanilla/utils.d.ts +14 -0
  213. package/vanilla/utils.js +854 -0
  214. package/vanilla.d.ts +4 -0
  215. package/vanilla.js +60 -0
@@ -0,0 +1,764 @@
1
+ System.register(['jotai/vanilla'], (function (exports) {
2
+ 'use strict';
3
+ var atom;
4
+ return {
5
+ setters: [function (module) {
6
+ atom = module.atom;
7
+ }],
8
+ execute: (function () {
9
+
10
+ exports({
11
+ atomFamily: atomFamily,
12
+ atomWithDefault: atomWithDefault,
13
+ atomWithLazy: atomWithLazy,
14
+ atomWithObservable: atomWithObservable,
15
+ atomWithReducer: atomWithReducer,
16
+ atomWithRefresh: atomWithRefresh,
17
+ atomWithReset: atomWithReset,
18
+ atomWithStorage: atomWithStorage,
19
+ createJSONStorage: createJSONStorage,
20
+ freezeAtom: freezeAtom,
21
+ freezeAtomCreator: freezeAtomCreator,
22
+ loadable: loadable,
23
+ selectAtom: selectAtom,
24
+ splitAtom: splitAtom,
25
+ unstable_withStorageValidator: withStorageValidator,
26
+ unwrap: unwrap
27
+ });
28
+
29
+ const RESET = exports("RESET", /* @__PURE__ */ Symbol(
30
+ "RESET"
31
+ ));
32
+
33
+ function atomWithReset(initialValue) {
34
+ const anAtom = atom(
35
+ initialValue,
36
+ (get, set, update) => {
37
+ const nextValue = typeof update === "function" ? update(get(anAtom)) : update;
38
+ set(anAtom, nextValue === RESET ? initialValue : nextValue);
39
+ }
40
+ );
41
+ return anAtom;
42
+ }
43
+
44
+ function atomWithReducer(initialValue, reducer) {
45
+ return atom(initialValue, function(get, set, action) {
46
+ set(this, reducer(get(this), action));
47
+ });
48
+ }
49
+
50
+ let didWarnDeprecation$1 = false;
51
+ function atomFamily(initializeAtom, areEqual) {
52
+ if (!didWarnDeprecation$1) {
53
+ console.warn(
54
+ "[DEPRECATED] atomFamily is deprecated and will be removed in v3. Please use the `jotai-family` package instead: https://github.com/jotaijs/jotai-family"
55
+ );
56
+ didWarnDeprecation$1 = true;
57
+ }
58
+ let shouldRemove = null;
59
+ const atoms = /* @__PURE__ */ new Map();
60
+ const listeners = /* @__PURE__ */ new Set();
61
+ const createAtom = (param) => {
62
+ let item;
63
+ if (areEqual === void 0) {
64
+ item = atoms.get(param);
65
+ } else {
66
+ for (const [key, value] of atoms) {
67
+ if (areEqual(key, param)) {
68
+ item = value;
69
+ break;
70
+ }
71
+ }
72
+ }
73
+ if (item !== void 0) {
74
+ if (shouldRemove == null ? void 0 : shouldRemove(item[1], param)) {
75
+ createAtom.remove(param);
76
+ } else {
77
+ return item[0];
78
+ }
79
+ }
80
+ const newAtom = initializeAtom(param);
81
+ atoms.set(param, [newAtom, Date.now()]);
82
+ notifyListeners("CREATE", param, newAtom);
83
+ return newAtom;
84
+ };
85
+ const notifyListeners = (type, param, atom) => {
86
+ for (const listener of listeners) {
87
+ listener({ type, param, atom });
88
+ }
89
+ };
90
+ createAtom.unstable_listen = (callback) => {
91
+ listeners.add(callback);
92
+ return () => {
93
+ listeners.delete(callback);
94
+ };
95
+ };
96
+ createAtom.getParams = () => atoms.keys();
97
+ createAtom.remove = (param) => {
98
+ if (areEqual === void 0) {
99
+ if (!atoms.has(param)) return;
100
+ const [atom] = atoms.get(param);
101
+ atoms.delete(param);
102
+ notifyListeners("REMOVE", param, atom);
103
+ } else {
104
+ for (const [key, [atom]] of atoms) {
105
+ if (areEqual(key, param)) {
106
+ atoms.delete(key);
107
+ notifyListeners("REMOVE", key, atom);
108
+ break;
109
+ }
110
+ }
111
+ }
112
+ };
113
+ createAtom.setShouldRemove = (fn) => {
114
+ shouldRemove = fn;
115
+ if (!shouldRemove) return;
116
+ for (const [key, [atom, createdAt]] of atoms) {
117
+ if (shouldRemove(createdAt, key)) {
118
+ atoms.delete(key);
119
+ notifyListeners("REMOVE", key, atom);
120
+ }
121
+ }
122
+ };
123
+ return createAtom;
124
+ }
125
+
126
+ const getCached$2 = (c, m, k) => (m.has(k) ? m : m.set(k, c())).get(k);
127
+ const cache1$3 = /* @__PURE__ */ new WeakMap();
128
+ const memo3 = (create, dep1, dep2, dep3) => {
129
+ const cache2 = getCached$2(() => /* @__PURE__ */ new WeakMap(), cache1$3, dep1);
130
+ const cache3 = getCached$2(() => /* @__PURE__ */ new WeakMap(), cache2, dep2);
131
+ return getCached$2(create, cache3, dep3);
132
+ };
133
+ function selectAtom(anAtom, selector, equalityFn = Object.is) {
134
+ return memo3(
135
+ () => {
136
+ const EMPTY = /* @__PURE__ */ Symbol();
137
+ const selectValue = ([value, prevSlice]) => {
138
+ if (prevSlice === EMPTY) {
139
+ return selector(value);
140
+ }
141
+ const slice = selector(value, prevSlice);
142
+ return equalityFn(prevSlice, slice) ? prevSlice : slice;
143
+ };
144
+ const derivedAtom = atom((get) => {
145
+ const prev = get(derivedAtom);
146
+ const value = get(anAtom);
147
+ return selectValue([value, prev]);
148
+ });
149
+ derivedAtom.init = EMPTY;
150
+ return derivedAtom;
151
+ },
152
+ anAtom,
153
+ selector,
154
+ equalityFn
155
+ );
156
+ }
157
+
158
+ const frozenAtoms = /* @__PURE__ */ new WeakSet();
159
+ const deepFreeze = (value) => {
160
+ if (typeof value !== "object" || value === null) {
161
+ return value;
162
+ }
163
+ Object.freeze(value);
164
+ const propNames = Object.getOwnPropertyNames(value);
165
+ for (const name of propNames) {
166
+ deepFreeze(value[name]);
167
+ }
168
+ return value;
169
+ };
170
+ function freezeAtom(anAtom) {
171
+ if (frozenAtoms.has(anAtom)) {
172
+ return anAtom;
173
+ }
174
+ frozenAtoms.add(anAtom);
175
+ const origRead = anAtom.read;
176
+ anAtom.read = function(get, options) {
177
+ return deepFreeze(origRead.call(this, get, options));
178
+ };
179
+ if ("write" in anAtom) {
180
+ const origWrite = anAtom.write;
181
+ anAtom.write = function(get, set, ...args) {
182
+ return origWrite.call(
183
+ this,
184
+ get,
185
+ (...setArgs) => {
186
+ if (setArgs[0] === anAtom) {
187
+ setArgs[1] = deepFreeze(setArgs[1]);
188
+ }
189
+ return set(...setArgs);
190
+ },
191
+ ...args
192
+ );
193
+ };
194
+ }
195
+ return anAtom;
196
+ }
197
+ function freezeAtomCreator(createAtom) {
198
+ {
199
+ console.warn(
200
+ "[DEPRECATED] freezeAtomCreator is deprecated, define it on users end"
201
+ );
202
+ }
203
+ return ((...args) => freezeAtom(createAtom(...args)));
204
+ }
205
+
206
+ const getCached$1 = (c, m, k) => (m.has(k) ? m : m.set(k, c())).get(k);
207
+ const cache1$2 = /* @__PURE__ */ new WeakMap();
208
+ const memo2$1 = (create, dep1, dep2) => {
209
+ const cache2 = getCached$1(() => /* @__PURE__ */ new WeakMap(), cache1$2, dep1);
210
+ return getCached$1(create, cache2, dep2);
211
+ };
212
+ const cacheKeyForEmptyKeyExtractor = {};
213
+ const isWritable = (atom2) => !!atom2.write;
214
+ const isFunction = (x) => typeof x === "function";
215
+ function splitAtom(arrAtom, keyExtractor) {
216
+ return memo2$1(
217
+ () => {
218
+ const mappingCache = /* @__PURE__ */ new WeakMap();
219
+ const getMapping = (arr, prev) => {
220
+ let mapping = mappingCache.get(arr);
221
+ if (mapping) {
222
+ return mapping;
223
+ }
224
+ const prevMapping = prev && mappingCache.get(prev);
225
+ const atomList = [];
226
+ const keyList = [];
227
+ arr.forEach((item, index) => {
228
+ const key = keyExtractor ? keyExtractor(item) : index;
229
+ keyList[index] = key;
230
+ const cachedAtom = prevMapping && prevMapping.atomList[prevMapping.keyList.indexOf(key)];
231
+ if (cachedAtom) {
232
+ atomList[index] = cachedAtom;
233
+ return;
234
+ }
235
+ const read = (get) => {
236
+ const prev2 = get(mappingAtom);
237
+ const currArr = get(arrAtom);
238
+ const mapping2 = getMapping(currArr, prev2 == null ? void 0 : prev2.arr);
239
+ const index2 = mapping2.keyList.indexOf(key);
240
+ if (index2 < 0 || index2 >= currArr.length) {
241
+ const prevItem = arr[getMapping(arr).keyList.indexOf(key)];
242
+ if (prevItem) {
243
+ return prevItem;
244
+ }
245
+ throw new Error("splitAtom: index out of bounds for read");
246
+ }
247
+ return currArr[index2];
248
+ };
249
+ const write = (get, set, update) => {
250
+ const prev2 = get(mappingAtom);
251
+ const arr2 = get(arrAtom);
252
+ const mapping2 = getMapping(arr2, prev2 == null ? void 0 : prev2.arr);
253
+ const index2 = mapping2.keyList.indexOf(key);
254
+ if (index2 < 0 || index2 >= arr2.length) {
255
+ throw new Error("splitAtom: index out of bounds for write");
256
+ }
257
+ const nextItem = isFunction(update) ? update(arr2[index2]) : update;
258
+ if (!Object.is(arr2[index2], nextItem)) {
259
+ set(arrAtom, [
260
+ ...arr2.slice(0, index2),
261
+ nextItem,
262
+ ...arr2.slice(index2 + 1)
263
+ ]);
264
+ }
265
+ };
266
+ atomList[index] = isWritable(arrAtom) ? atom(read, write) : atom(read);
267
+ });
268
+ if (prevMapping && prevMapping.keyList.length === keyList.length && prevMapping.keyList.every((x, i) => x === keyList[i])) {
269
+ mapping = prevMapping;
270
+ } else {
271
+ mapping = { arr, atomList, keyList };
272
+ }
273
+ mappingCache.set(arr, mapping);
274
+ return mapping;
275
+ };
276
+ const mappingAtom = atom((get) => {
277
+ const prev = get(mappingAtom);
278
+ const arr = get(arrAtom);
279
+ const mapping = getMapping(arr, prev == null ? void 0 : prev.arr);
280
+ return mapping;
281
+ });
282
+ {
283
+ mappingAtom.debugPrivate = true;
284
+ }
285
+ mappingAtom.init = void 0;
286
+ const splittedAtom = isWritable(arrAtom) ? atom(
287
+ (get) => get(mappingAtom).atomList,
288
+ (get, set, action) => {
289
+ switch (action.type) {
290
+ case "remove": {
291
+ const index = get(splittedAtom).indexOf(action.atom);
292
+ if (index >= 0) {
293
+ const arr = get(arrAtom);
294
+ set(arrAtom, [
295
+ ...arr.slice(0, index),
296
+ ...arr.slice(index + 1)
297
+ ]);
298
+ }
299
+ break;
300
+ }
301
+ case "insert": {
302
+ const index = action.before ? get(splittedAtom).indexOf(action.before) : get(splittedAtom).length;
303
+ if (index >= 0) {
304
+ const arr = get(arrAtom);
305
+ set(arrAtom, [
306
+ ...arr.slice(0, index),
307
+ action.value,
308
+ ...arr.slice(index)
309
+ ]);
310
+ }
311
+ break;
312
+ }
313
+ case "move": {
314
+ const index1 = get(splittedAtom).indexOf(action.atom);
315
+ const index2 = action.before ? get(splittedAtom).indexOf(action.before) : get(splittedAtom).length;
316
+ if (index1 >= 0 && index2 >= 0) {
317
+ const arr = get(arrAtom);
318
+ if (index1 < index2) {
319
+ set(arrAtom, [
320
+ ...arr.slice(0, index1),
321
+ ...arr.slice(index1 + 1, index2),
322
+ arr[index1],
323
+ ...arr.slice(index2)
324
+ ]);
325
+ } else {
326
+ set(arrAtom, [
327
+ ...arr.slice(0, index2),
328
+ arr[index1],
329
+ ...arr.slice(index2, index1),
330
+ ...arr.slice(index1 + 1)
331
+ ]);
332
+ }
333
+ }
334
+ break;
335
+ }
336
+ }
337
+ }
338
+ ) : atom((get) => get(mappingAtom).atomList);
339
+ return splittedAtom;
340
+ },
341
+ arrAtom,
342
+ keyExtractor || cacheKeyForEmptyKeyExtractor
343
+ );
344
+ }
345
+
346
+ function atomWithDefault(getDefault) {
347
+ const EMPTY = /* @__PURE__ */ Symbol();
348
+ const overwrittenAtom = atom(EMPTY);
349
+ {
350
+ overwrittenAtom.debugPrivate = true;
351
+ }
352
+ const anAtom = atom(
353
+ (get, options) => {
354
+ const overwritten = get(overwrittenAtom);
355
+ if (overwritten !== EMPTY) {
356
+ return overwritten;
357
+ }
358
+ return getDefault(get, options);
359
+ },
360
+ (get, set, update) => {
361
+ const newValue = typeof update === "function" ? update(get(anAtom)) : update;
362
+ set(overwrittenAtom, newValue === RESET ? EMPTY : newValue);
363
+ }
364
+ );
365
+ return anAtom;
366
+ }
367
+
368
+ const isPromiseLike$2 = (x) => typeof (x == null ? void 0 : x.then) === "function";
369
+ function withStorageValidator(validator) {
370
+ return (unknownStorage) => {
371
+ const storage = {
372
+ ...unknownStorage,
373
+ getItem: (key, initialValue) => {
374
+ const validate = (value2) => {
375
+ if (!validator(value2)) {
376
+ return initialValue;
377
+ }
378
+ return value2;
379
+ };
380
+ const value = unknownStorage.getItem(key, initialValue);
381
+ if (isPromiseLike$2(value)) {
382
+ return value.then(validate);
383
+ }
384
+ return validate(value);
385
+ }
386
+ };
387
+ return storage;
388
+ };
389
+ }
390
+ function createJSONStorage(getStringStorage = () => {
391
+ try {
392
+ return window.localStorage;
393
+ } catch (e) {
394
+ {
395
+ if (typeof window !== "undefined") {
396
+ console.warn(e);
397
+ }
398
+ }
399
+ return void 0;
400
+ }
401
+ }, options) {
402
+ var _a;
403
+ let lastStr;
404
+ let lastValue;
405
+ const storage = {
406
+ getItem: (key, initialValue) => {
407
+ var _a2, _b;
408
+ const parse = (str2) => {
409
+ str2 = str2 || "";
410
+ if (lastStr !== str2) {
411
+ try {
412
+ lastValue = JSON.parse(str2, options == null ? void 0 : options.reviver);
413
+ } catch (e) {
414
+ return initialValue;
415
+ }
416
+ lastStr = str2;
417
+ }
418
+ return lastValue;
419
+ };
420
+ const str = (_b = (_a2 = getStringStorage()) == null ? void 0 : _a2.getItem(key)) != null ? _b : null;
421
+ if (isPromiseLike$2(str)) {
422
+ return str.then(parse);
423
+ }
424
+ return parse(str);
425
+ },
426
+ setItem: (key, newValue) => {
427
+ var _a2;
428
+ return (_a2 = getStringStorage()) == null ? void 0 : _a2.setItem(
429
+ key,
430
+ JSON.stringify(newValue, options == null ? void 0 : options.replacer)
431
+ );
432
+ },
433
+ removeItem: (key) => {
434
+ var _a2;
435
+ return (_a2 = getStringStorage()) == null ? void 0 : _a2.removeItem(key);
436
+ }
437
+ };
438
+ const createHandleSubscribe = (subscriber2) => (key, callback, initialValue) => subscriber2(key, (v) => {
439
+ let newValue;
440
+ try {
441
+ newValue = JSON.parse(v || "");
442
+ } catch (e) {
443
+ newValue = initialValue;
444
+ }
445
+ callback(newValue);
446
+ });
447
+ let subscriber;
448
+ try {
449
+ subscriber = (_a = getStringStorage()) == null ? void 0 : _a.subscribe;
450
+ } catch (e) {
451
+ }
452
+ if (!subscriber && typeof window !== "undefined" && typeof window.addEventListener === "function" && window.Storage) {
453
+ subscriber = (key, callback) => {
454
+ if (!(getStringStorage() instanceof window.Storage)) {
455
+ return () => {
456
+ };
457
+ }
458
+ const storageEventCallback = (e) => {
459
+ if (e.storageArea === getStringStorage() && e.key === key) {
460
+ callback(e.newValue);
461
+ }
462
+ };
463
+ window.addEventListener("storage", storageEventCallback);
464
+ return () => {
465
+ window.removeEventListener("storage", storageEventCallback);
466
+ };
467
+ };
468
+ }
469
+ if (subscriber) {
470
+ storage.subscribe = createHandleSubscribe(subscriber);
471
+ }
472
+ return storage;
473
+ }
474
+ const defaultStorage = createJSONStorage();
475
+ function atomWithStorage(key, initialValue, storage = defaultStorage, options) {
476
+ const getOnInit = options == null ? void 0 : options.getOnInit;
477
+ const baseAtom = atom(
478
+ getOnInit ? storage.getItem(key, initialValue) : initialValue
479
+ );
480
+ {
481
+ baseAtom.debugPrivate = true;
482
+ }
483
+ baseAtom.onMount = (setAtom) => {
484
+ setAtom(storage.getItem(key, initialValue));
485
+ let unsub;
486
+ if (storage.subscribe) {
487
+ unsub = storage.subscribe(key, setAtom, initialValue);
488
+ }
489
+ return unsub;
490
+ };
491
+ const anAtom = atom(
492
+ (get) => get(baseAtom),
493
+ (get, set, update) => {
494
+ const nextValue = typeof update === "function" ? update(get(baseAtom)) : update;
495
+ if (nextValue === RESET) {
496
+ set(baseAtom, initialValue);
497
+ return storage.removeItem(key);
498
+ }
499
+ if (isPromiseLike$2(nextValue)) {
500
+ return nextValue.then((resolvedValue) => {
501
+ set(baseAtom, resolvedValue);
502
+ return storage.setItem(key, resolvedValue);
503
+ });
504
+ }
505
+ set(baseAtom, nextValue);
506
+ return storage.setItem(key, nextValue);
507
+ }
508
+ );
509
+ return anAtom;
510
+ }
511
+
512
+ const isPromiseLike$1 = (x) => typeof (x == null ? void 0 : x.then) === "function";
513
+ function atomWithObservable(getObservable, options) {
514
+ const returnResultData = (result) => {
515
+ if ("e" in result) {
516
+ throw result.e;
517
+ }
518
+ return result.d;
519
+ };
520
+ const observableResultAtom = atom((get) => {
521
+ var _a;
522
+ let observable = getObservable(get);
523
+ const itself = (_a = observable[Symbol.observable]) == null ? void 0 : _a.call(observable);
524
+ if (itself) {
525
+ observable = itself;
526
+ }
527
+ let resolve;
528
+ const makePending = () => new Promise((r) => {
529
+ resolve = r;
530
+ });
531
+ const initialResult = options && "initialValue" in options ? {
532
+ d: typeof options.initialValue === "function" ? options.initialValue() : options.initialValue
533
+ } : makePending();
534
+ let setResult;
535
+ let lastResult;
536
+ const listener = (result) => {
537
+ lastResult = result;
538
+ resolve == null ? void 0 : resolve(result);
539
+ setResult == null ? void 0 : setResult(result);
540
+ };
541
+ let subscription;
542
+ let timer;
543
+ const isNotMounted = () => !setResult;
544
+ const unsubscribe = () => {
545
+ if (subscription) {
546
+ subscription.unsubscribe();
547
+ subscription = void 0;
548
+ }
549
+ };
550
+ const start = () => {
551
+ if (subscription) {
552
+ clearTimeout(timer);
553
+ subscription.unsubscribe();
554
+ }
555
+ subscription = observable.subscribe({
556
+ next: (d) => listener({ d }),
557
+ error: (e) => listener({ e }),
558
+ complete: () => {
559
+ }
560
+ });
561
+ if (isNotMounted() && (options == null ? void 0 : options.unstable_timeout)) {
562
+ timer = setTimeout(unsubscribe, options.unstable_timeout);
563
+ }
564
+ };
565
+ start();
566
+ const resultAtom = atom(lastResult || initialResult);
567
+ {
568
+ resultAtom.debugPrivate = true;
569
+ }
570
+ resultAtom.onMount = (update) => {
571
+ setResult = update;
572
+ if (lastResult) {
573
+ update(lastResult);
574
+ }
575
+ if (subscription) {
576
+ clearTimeout(timer);
577
+ } else {
578
+ start();
579
+ }
580
+ return () => {
581
+ setResult = void 0;
582
+ if (options == null ? void 0 : options.unstable_timeout) {
583
+ timer = setTimeout(unsubscribe, options.unstable_timeout);
584
+ } else {
585
+ unsubscribe();
586
+ }
587
+ };
588
+ };
589
+ return [resultAtom, observable, makePending, start, isNotMounted];
590
+ });
591
+ {
592
+ observableResultAtom.debugPrivate = true;
593
+ }
594
+ const observableAtom = atom(
595
+ (get) => {
596
+ const [resultAtom] = get(observableResultAtom);
597
+ const result = get(resultAtom);
598
+ if (isPromiseLike$1(result)) {
599
+ return result.then(returnResultData);
600
+ }
601
+ return returnResultData(result);
602
+ },
603
+ (get, set, data) => {
604
+ const [resultAtom, observable, makePending, start, isNotMounted] = get(observableResultAtom);
605
+ if ("next" in observable) {
606
+ if (isNotMounted()) {
607
+ set(resultAtom, makePending());
608
+ start();
609
+ }
610
+ observable.next(data);
611
+ } else {
612
+ throw new Error("observable is not subject");
613
+ }
614
+ }
615
+ );
616
+ return observableAtom;
617
+ }
618
+
619
+ const getCached = (c, m, k) => (m.has(k) ? m : m.set(k, c())).get(k);
620
+ const cache1$1 = /* @__PURE__ */ new WeakMap();
621
+ const memo2 = (create, dep1, dep2) => {
622
+ const cache2 = getCached(() => /* @__PURE__ */ new WeakMap(), cache1$1, dep1);
623
+ return getCached(create, cache2, dep2);
624
+ };
625
+ const isPromiseLike = (p) => typeof (p == null ? void 0 : p.then) === "function";
626
+ const defaultFallback = () => void 0;
627
+ function unwrap(anAtom, fallback = defaultFallback) {
628
+ return memo2(
629
+ () => {
630
+ const promiseErrorCache = /* @__PURE__ */ new WeakMap();
631
+ const promiseResultCache = /* @__PURE__ */ new WeakMap();
632
+ const refreshAtom = atom(0);
633
+ const triggerRefreshAtom = atom([]);
634
+ triggerRefreshAtom.INTERNAL_onInit = (store) => {
635
+ store.set(triggerRefreshAtom, [
636
+ () => store.set(refreshAtom, (c) => c + 1)
637
+ ]);
638
+ };
639
+ {
640
+ refreshAtom.debugPrivate = true;
641
+ triggerRefreshAtom.debugPrivate = true;
642
+ }
643
+ const promiseAndValueAtom = atom((get) => {
644
+ get(refreshAtom);
645
+ let prev;
646
+ try {
647
+ prev = get(promiseAndValueAtom);
648
+ } catch (e) {
649
+ }
650
+ const promise = get(anAtom);
651
+ if (!isPromiseLike(promise)) {
652
+ return { v: promise };
653
+ }
654
+ if (promise !== (prev == null ? void 0 : prev.p)) {
655
+ promise.then(
656
+ (v) => {
657
+ promiseResultCache.set(promise, v);
658
+ const [triggerRefresh] = get(triggerRefreshAtom);
659
+ triggerRefresh();
660
+ },
661
+ (e) => {
662
+ promiseErrorCache.set(promise, e);
663
+ const [triggerRefresh] = get(triggerRefreshAtom);
664
+ triggerRefresh();
665
+ }
666
+ );
667
+ }
668
+ if (promiseErrorCache.has(promise)) {
669
+ throw promiseErrorCache.get(promise);
670
+ }
671
+ if (promiseResultCache.has(promise)) {
672
+ return {
673
+ p: promise,
674
+ v: promiseResultCache.get(promise)
675
+ };
676
+ }
677
+ if (prev && "v" in prev) {
678
+ return { p: promise, f: fallback(prev.v), v: prev.v };
679
+ }
680
+ return { p: promise, f: fallback() };
681
+ });
682
+ promiseAndValueAtom.init = void 0;
683
+ {
684
+ promiseAndValueAtom.debugPrivate = true;
685
+ }
686
+ return atom(
687
+ (get) => {
688
+ const state = get(promiseAndValueAtom);
689
+ if ("f" in state) {
690
+ return state.f;
691
+ }
692
+ return state.v;
693
+ },
694
+ (_get, set, ...args) => set(anAtom, ...args)
695
+ );
696
+ },
697
+ anAtom,
698
+ fallback
699
+ );
700
+ }
701
+
702
+ const cache1 = /* @__PURE__ */ new WeakMap();
703
+ const memo1 = (create, dep1) => (cache1.has(dep1) ? cache1 : cache1.set(dep1, create())).get(dep1);
704
+ let didWarnDeprecation = false;
705
+ function loadable(anAtom) {
706
+ if (!didWarnDeprecation) {
707
+ console.warn(
708
+ "[DEPRECATED] loadable is deprecated and will be removed in v3. Please use a userland util with the `unwrap` util: https://github.com/pmndrs/jotai/pull/3217"
709
+ );
710
+ didWarnDeprecation = true;
711
+ }
712
+ return memo1(() => {
713
+ const LOADING = { state: "loading" };
714
+ const unwrappedAtom = unwrap(anAtom, () => LOADING);
715
+ return atom((get) => {
716
+ try {
717
+ const data = get(unwrappedAtom);
718
+ if (data === LOADING) {
719
+ return LOADING;
720
+ }
721
+ return { state: "hasData", data };
722
+ } catch (error) {
723
+ return { state: "hasError", error };
724
+ }
725
+ });
726
+ }, anAtom);
727
+ }
728
+
729
+ function atomWithRefresh(read, write) {
730
+ const refreshAtom = atom(0);
731
+ {
732
+ refreshAtom.debugPrivate = true;
733
+ }
734
+ return atom(
735
+ (get, options) => {
736
+ get(refreshAtom);
737
+ return read(get, options);
738
+ },
739
+ (get, set, ...args) => {
740
+ if (args.length === 0) {
741
+ set(refreshAtom, (c) => c + 1);
742
+ } else if (write) {
743
+ return write(get, set, ...args);
744
+ } else {
745
+ throw new Error("refresh must be called without arguments");
746
+ }
747
+ }
748
+ );
749
+ }
750
+
751
+ function atomWithLazy(makeInitial) {
752
+ const a = atom(void 0);
753
+ delete a.init;
754
+ Object.defineProperty(a, "init", {
755
+ get() {
756
+ return makeInitial();
757
+ }
758
+ });
759
+ return a;
760
+ }
761
+
762
+ })
763
+ };
764
+ }));