@storybook/react-native 10.2.2-alpha.5 → 10.2.2-next.6

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 (2) hide show
  1. package/dist/index.js +708 -530
  2. package/package.json +14 -15
package/dist/index.js CHANGED
@@ -51,6 +51,9 @@ var Platform = {
51
51
  return true;
52
52
  }
53
53
  return false;
54
+ },
55
+ get Version() {
56
+ return "0.0.0";
54
57
  }
55
58
  };
56
59
  var Platform_default = Platform;
@@ -91,609 +94,716 @@ var import_react_native_theming = require("@storybook/react-native-theming");
91
94
  var import_react3 = __toESM(require("react"));
92
95
  var import_react_native2 = require("react-native");
93
96
 
94
- // ../../node_modules/jotai/esm/vanilla.mjs
97
+ // ../../node_modules/jotai/esm/vanilla/internals.mjs
95
98
  var import_meta = {};
96
- var keyCount = 0;
97
- function atom(read, write) {
98
- const key = `atom${++keyCount}`;
99
- const config = {
100
- toString: () => key
101
- };
102
- if (typeof read === "function") {
103
- config.read = read;
104
- } else {
105
- config.init = read;
106
- config.read = function(get) {
107
- return get(this);
108
- };
109
- config.write = function(get, set, arg) {
110
- return set(
111
- this,
112
- typeof arg === "function" ? arg(get(this)) : arg
113
- );
99
+ function hasInitialValue(atom2) {
100
+ return "init" in atom2;
101
+ }
102
+ function isActuallyWritableAtom(atom2) {
103
+ return !!atom2.write;
104
+ }
105
+ function isAtomStateInitialized(atomState) {
106
+ return "v" in atomState || "e" in atomState;
107
+ }
108
+ function returnAtomValue(atomState) {
109
+ if ("e" in atomState) {
110
+ throw atomState.e;
111
+ }
112
+ if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production" && !("v" in atomState)) {
113
+ throw new Error("[Bug] atom state is not initialized");
114
+ }
115
+ return atomState.v;
116
+ }
117
+ var promiseStateMap = /* @__PURE__ */ new WeakMap();
118
+ function isPendingPromise(value) {
119
+ var _a;
120
+ return isPromiseLike(value) && !!((_a = promiseStateMap.get(value)) == null ? void 0 : _a[0]);
121
+ }
122
+ function abortPromise(promise) {
123
+ const promiseState = promiseStateMap.get(promise);
124
+ if (promiseState == null ? void 0 : promiseState[0]) {
125
+ promiseState[0] = false;
126
+ promiseState[1].forEach((fn) => fn());
127
+ }
128
+ }
129
+ function registerAbortHandler(promise, abortHandler) {
130
+ let promiseState = promiseStateMap.get(promise);
131
+ if (!promiseState) {
132
+ promiseState = [true, /* @__PURE__ */ new Set()];
133
+ promiseStateMap.set(promise, promiseState);
134
+ const settle = () => {
135
+ promiseState[0] = false;
114
136
  };
137
+ promise.then(settle, settle);
115
138
  }
116
- if (write) {
117
- config.write = write;
139
+ promiseState[1].add(abortHandler);
140
+ }
141
+ function isPromiseLike(p) {
142
+ return typeof (p == null ? void 0 : p.then) === "function";
143
+ }
144
+ function addPendingPromiseToDependency(atom2, promise, dependencyAtomState) {
145
+ if (!dependencyAtomState.p.has(atom2)) {
146
+ dependencyAtomState.p.add(atom2);
147
+ const cleanup = () => dependencyAtomState.p.delete(atom2);
148
+ promise.then(cleanup, cleanup);
118
149
  }
119
- return config;
120
150
  }
121
- var hasInitialValue = (atom2) => "init" in atom2;
122
- var isActuallyWritableAtom = (atom2) => !!atom2.write;
123
- var cancelPromiseMap = /* @__PURE__ */ new WeakMap();
124
- var registerCancelPromise = (promise, cancel) => {
125
- cancelPromiseMap.set(promise, cancel);
126
- promise.catch(() => {
127
- }).finally(() => cancelPromiseMap.delete(promise));
128
- };
129
- var cancelPromise = (promise, next) => {
130
- const cancel = cancelPromiseMap.get(promise);
131
- if (cancel) {
132
- cancelPromiseMap.delete(promise);
133
- cancel(next);
151
+ function getMountedOrPendingDependents(atom2, atomState, mountedMap) {
152
+ var _a;
153
+ const dependents = /* @__PURE__ */ new Set();
154
+ for (const a of ((_a = mountedMap.get(atom2)) == null ? void 0 : _a.t) || []) {
155
+ dependents.add(a);
134
156
  }
157
+ for (const atomWithPendingPromise of atomState.p) {
158
+ dependents.add(atomWithPendingPromise);
159
+ }
160
+ return dependents;
161
+ }
162
+ var BUILDING_BLOCK_atomRead = (_store, atom2, ...params) => atom2.read(...params);
163
+ var BUILDING_BLOCK_atomWrite = (_store, atom2, ...params) => atom2.write(...params);
164
+ var BUILDING_BLOCK_atomOnInit = (store, atom2) => {
165
+ var _a;
166
+ return (_a = atom2.INTERNAL_onInit) == null ? void 0 : _a.call(atom2, store);
135
167
  };
136
- var resolvePromise = (promise, value) => {
137
- promise.status = "fulfilled";
138
- promise.value = value;
139
- };
140
- var rejectPromise = (promise, e) => {
141
- promise.status = "rejected";
142
- promise.reason = e;
168
+ var BUILDING_BLOCK_atomOnMount = (_store, atom2, setAtom) => {
169
+ var _a;
170
+ return (_a = atom2.onMount) == null ? void 0 : _a.call(atom2, setAtom);
143
171
  };
144
- var isPromiseLike = (x) => typeof (x == null ? void 0 : x.then) === "function";
145
- var isEqualAtomValue = (a, b) => !!a && "v" in a && "v" in b && Object.is(a.v, b.v);
146
- var isEqualAtomError = (a, b) => !!a && "e" in a && "e" in b && Object.is(a.e, b.e);
147
- var hasPromiseAtomValue = (a) => !!a && "v" in a && a.v instanceof Promise;
148
- var isEqualPromiseAtomValue = (a, b) => "v" in a && "v" in b && a.v.orig && a.v.orig === b.v.orig;
149
- var returnAtomValue = (atomState) => {
150
- if ("e" in atomState) {
151
- throw atomState.e;
172
+ var BUILDING_BLOCK_ensureAtomState = (store, atom2) => {
173
+ var _a;
174
+ const buildingBlocks = getInternalBuildingBlocks(store);
175
+ const atomStateMap = buildingBlocks[0];
176
+ const storeHooks = buildingBlocks[6];
177
+ const atomOnInit = buildingBlocks[9];
178
+ if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production" && !atom2) {
179
+ throw new Error("Atom is undefined or null");
152
180
  }
153
- return atomState.v;
154
- };
155
- var createStore = () => {
156
- const atomStateMap = /* @__PURE__ */ new WeakMap();
157
- const mountedMap = /* @__PURE__ */ new WeakMap();
158
- const pendingMap = /* @__PURE__ */ new Map();
159
- let storeListenersRev2;
160
- let mountedAtoms;
161
- if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production") {
162
- storeListenersRev2 = /* @__PURE__ */ new Set();
163
- mountedAtoms = /* @__PURE__ */ new Set();
181
+ let atomState = atomStateMap.get(atom2);
182
+ if (!atomState) {
183
+ atomState = { d: /* @__PURE__ */ new Map(), p: /* @__PURE__ */ new Set(), n: 0 };
184
+ atomStateMap.set(atom2, atomState);
185
+ (_a = storeHooks.i) == null ? void 0 : _a.call(storeHooks, atom2);
186
+ atomOnInit == null ? void 0 : atomOnInit(store, atom2);
164
187
  }
165
- const getAtomState = (atom2) => atomStateMap.get(atom2);
166
- const setAtomState = (atom2, atomState) => {
167
- if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production") {
168
- Object.freeze(atomState);
188
+ return atomState;
189
+ };
190
+ var BUILDING_BLOCK_flushCallbacks = (store) => {
191
+ const buildingBlocks = getInternalBuildingBlocks(store);
192
+ const mountedMap = buildingBlocks[1];
193
+ const changedAtoms = buildingBlocks[3];
194
+ const mountCallbacks = buildingBlocks[4];
195
+ const unmountCallbacks = buildingBlocks[5];
196
+ const storeHooks = buildingBlocks[6];
197
+ const recomputeInvalidatedAtoms = buildingBlocks[13];
198
+ const errors = [];
199
+ const call = (fn) => {
200
+ try {
201
+ fn();
202
+ } catch (e) {
203
+ errors.push(e);
169
204
  }
170
- const prevAtomState = atomStateMap.get(atom2);
171
- atomStateMap.set(atom2, atomState);
172
- if (!pendingMap.has(atom2)) {
173
- pendingMap.set(atom2, prevAtomState);
205
+ };
206
+ do {
207
+ if (storeHooks.f) {
208
+ call(storeHooks.f);
209
+ }
210
+ const callbacks = /* @__PURE__ */ new Set();
211
+ const add = callbacks.add.bind(callbacks);
212
+ changedAtoms.forEach((atom2) => {
213
+ var _a;
214
+ return (_a = mountedMap.get(atom2)) == null ? void 0 : _a.l.forEach(add);
215
+ });
216
+ changedAtoms.clear();
217
+ unmountCallbacks.forEach(add);
218
+ unmountCallbacks.clear();
219
+ mountCallbacks.forEach(add);
220
+ mountCallbacks.clear();
221
+ callbacks.forEach(call);
222
+ if (changedAtoms.size) {
223
+ recomputeInvalidatedAtoms(store);
224
+ }
225
+ } while (changedAtoms.size || unmountCallbacks.size || mountCallbacks.size);
226
+ if (errors.length) {
227
+ throw new AggregateError(errors);
228
+ }
229
+ };
230
+ var BUILDING_BLOCK_recomputeInvalidatedAtoms = (store) => {
231
+ const buildingBlocks = getInternalBuildingBlocks(store);
232
+ const mountedMap = buildingBlocks[1];
233
+ const invalidatedAtoms = buildingBlocks[2];
234
+ const changedAtoms = buildingBlocks[3];
235
+ const ensureAtomState = buildingBlocks[11];
236
+ const readAtomState = buildingBlocks[14];
237
+ const mountDependencies = buildingBlocks[17];
238
+ const topSortedReversed = [];
239
+ const visiting = /* @__PURE__ */ new WeakSet();
240
+ const visited = /* @__PURE__ */ new WeakSet();
241
+ const stack = Array.from(changedAtoms);
242
+ while (stack.length) {
243
+ const a = stack[stack.length - 1];
244
+ const aState = ensureAtomState(store, a);
245
+ if (visited.has(a)) {
246
+ stack.pop();
247
+ continue;
174
248
  }
175
- if (hasPromiseAtomValue(prevAtomState)) {
176
- const next = "v" in atomState ? atomState.v instanceof Promise ? atomState.v : Promise.resolve(atomState.v) : Promise.reject(atomState.e);
177
- if (prevAtomState.v !== next) {
178
- cancelPromise(prevAtomState.v, next);
249
+ if (visiting.has(a)) {
250
+ if (invalidatedAtoms.get(a) === aState.n) {
251
+ topSortedReversed.push([a, aState]);
252
+ } else if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production" && invalidatedAtoms.has(a)) {
253
+ throw new Error("[Bug] invalidated atom exists");
179
254
  }
255
+ visited.add(a);
256
+ stack.pop();
257
+ continue;
180
258
  }
181
- };
182
- const updateDependencies = (atom2, nextAtomState, nextDependencies) => {
183
- const dependencies = /* @__PURE__ */ new Map();
184
- let changed = false;
185
- nextDependencies.forEach((aState, a) => {
186
- if (!aState && a === atom2) {
187
- aState = nextAtomState;
259
+ visiting.add(a);
260
+ for (const d of getMountedOrPendingDependents(a, aState, mountedMap)) {
261
+ if (!visiting.has(d)) {
262
+ stack.push(d);
188
263
  }
189
- if (aState) {
190
- dependencies.set(a, aState);
191
- if (nextAtomState.d.get(a) !== aState) {
192
- changed = true;
193
- }
194
- } else if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production") {
195
- console.warn("[Bug] atom state not found");
264
+ }
265
+ }
266
+ for (let i = topSortedReversed.length - 1; i >= 0; --i) {
267
+ const [a, aState] = topSortedReversed[i];
268
+ let hasChangedDeps = false;
269
+ for (const dep of aState.d.keys()) {
270
+ if (dep !== a && changedAtoms.has(dep)) {
271
+ hasChangedDeps = true;
272
+ break;
196
273
  }
197
- });
198
- if (changed || nextAtomState.d.size !== dependencies.size) {
199
- nextAtomState.d = dependencies;
200
274
  }
201
- };
202
- const setAtomValue = (atom2, value, nextDependencies) => {
203
- const prevAtomState = getAtomState(atom2);
204
- const nextAtomState = {
205
- d: (prevAtomState == null ? void 0 : prevAtomState.d) || /* @__PURE__ */ new Map(),
206
- v: value
207
- };
208
- if (nextDependencies) {
209
- updateDependencies(atom2, nextAtomState, nextDependencies);
275
+ if (hasChangedDeps) {
276
+ readAtomState(store, a);
277
+ mountDependencies(store, a);
210
278
  }
211
- if (isEqualAtomValue(prevAtomState, nextAtomState) && prevAtomState.d === nextAtomState.d) {
212
- return prevAtomState;
279
+ invalidatedAtoms.delete(a);
280
+ }
281
+ };
282
+ var storeMutationSet = /* @__PURE__ */ new WeakSet();
283
+ var BUILDING_BLOCK_readAtomState = (store, atom2) => {
284
+ var _a, _b;
285
+ const buildingBlocks = getInternalBuildingBlocks(store);
286
+ const mountedMap = buildingBlocks[1];
287
+ const invalidatedAtoms = buildingBlocks[2];
288
+ const changedAtoms = buildingBlocks[3];
289
+ const storeHooks = buildingBlocks[6];
290
+ const atomRead = buildingBlocks[7];
291
+ const ensureAtomState = buildingBlocks[11];
292
+ const flushCallbacks = buildingBlocks[12];
293
+ const recomputeInvalidatedAtoms = buildingBlocks[13];
294
+ const readAtomState = buildingBlocks[14];
295
+ const writeAtomState = buildingBlocks[16];
296
+ const mountDependencies = buildingBlocks[17];
297
+ const setAtomStateValueOrPromise = buildingBlocks[20];
298
+ const atomState = ensureAtomState(store, atom2);
299
+ if (isAtomStateInitialized(atomState)) {
300
+ if (mountedMap.has(atom2) && invalidatedAtoms.get(atom2) !== atomState.n) {
301
+ return atomState;
213
302
  }
214
- if (hasPromiseAtomValue(prevAtomState) && hasPromiseAtomValue(nextAtomState) && isEqualPromiseAtomValue(prevAtomState, nextAtomState)) {
215
- if (prevAtomState.d === nextAtomState.d) {
216
- return prevAtomState;
217
- } else {
218
- nextAtomState.v = prevAtomState.v;
303
+ let hasChangedDeps = false;
304
+ for (const [a, n] of atomState.d) {
305
+ if (readAtomState(store, a).n !== n) {
306
+ hasChangedDeps = true;
307
+ break;
219
308
  }
220
309
  }
221
- setAtomState(atom2, nextAtomState);
222
- return nextAtomState;
223
- };
224
- const setAtomValueOrPromise = (atom2, valueOrPromise, nextDependencies, abortPromise) => {
225
- if (isPromiseLike(valueOrPromise)) {
226
- let continuePromise;
227
- const updatePromiseDependencies = () => {
228
- const prevAtomState = getAtomState(atom2);
229
- if (!hasPromiseAtomValue(prevAtomState) || prevAtomState.v !== promise) {
230
- return;
231
- }
232
- const nextAtomState = setAtomValue(
233
- atom2,
234
- promise,
235
- nextDependencies
236
- );
237
- if (mountedMap.has(atom2) && prevAtomState.d !== nextAtomState.d) {
238
- mountDependencies(atom2, nextAtomState, prevAtomState.d);
239
- }
240
- };
241
- const promise = new Promise((resolve, reject) => {
242
- let settled = false;
243
- valueOrPromise.then(
244
- (v) => {
245
- if (!settled) {
246
- settled = true;
247
- resolvePromise(promise, v);
248
- resolve(v);
249
- updatePromiseDependencies();
250
- }
251
- },
252
- (e) => {
253
- if (!settled) {
254
- settled = true;
255
- rejectPromise(promise, e);
256
- reject(e);
257
- updatePromiseDependencies();
258
- }
259
- }
260
- );
261
- continuePromise = (next) => {
262
- if (!settled) {
263
- settled = true;
264
- next.then(
265
- (v) => resolvePromise(promise, v),
266
- (e) => rejectPromise(promise, e)
267
- );
268
- resolve(next);
269
- }
270
- };
271
- });
272
- promise.orig = valueOrPromise;
273
- promise.status = "pending";
274
- registerCancelPromise(promise, (next) => {
275
- if (next) {
276
- continuePromise(next);
277
- }
278
- abortPromise == null ? void 0 : abortPromise();
279
- });
280
- return setAtomValue(atom2, promise, nextDependencies);
310
+ if (!hasChangedDeps) {
311
+ return atomState;
281
312
  }
282
- return setAtomValue(atom2, valueOrPromise, nextDependencies);
283
- };
284
- const setAtomError = (atom2, error, nextDependencies) => {
285
- const prevAtomState = getAtomState(atom2);
286
- const nextAtomState = {
287
- d: (prevAtomState == null ? void 0 : prevAtomState.d) || /* @__PURE__ */ new Map(),
288
- e: error
289
- };
290
- if (nextDependencies) {
291
- updateDependencies(atom2, nextAtomState, nextDependencies);
313
+ }
314
+ atomState.d.clear();
315
+ let isSync = true;
316
+ function mountDependenciesIfAsync() {
317
+ if (mountedMap.has(atom2)) {
318
+ mountDependencies(store, atom2);
319
+ recomputeInvalidatedAtoms(store);
320
+ flushCallbacks(store);
292
321
  }
293
- if (isEqualAtomError(prevAtomState, nextAtomState) && prevAtomState.d === nextAtomState.d) {
294
- return prevAtomState;
322
+ }
323
+ function getter(a) {
324
+ var _a2;
325
+ if (a === atom2) {
326
+ const aState2 = ensureAtomState(store, a);
327
+ if (!isAtomStateInitialized(aState2)) {
328
+ if (hasInitialValue(a)) {
329
+ setAtomStateValueOrPromise(store, a, a.init);
330
+ } else {
331
+ throw new Error("no atom init");
332
+ }
333
+ }
334
+ return returnAtomValue(aState2);
295
335
  }
296
- setAtomState(atom2, nextAtomState);
297
- return nextAtomState;
298
- };
299
- const readAtomState = (atom2, force) => {
300
- const atomState = getAtomState(atom2);
301
- if (!force && atomState) {
336
+ const aState = readAtomState(store, a);
337
+ try {
338
+ return returnAtomValue(aState);
339
+ } finally {
340
+ atomState.d.set(a, aState.n);
341
+ if (isPendingPromise(atomState.v)) {
342
+ addPendingPromiseToDependency(atom2, atomState.v, aState);
343
+ }
302
344
  if (mountedMap.has(atom2)) {
303
- return atomState;
345
+ (_a2 = mountedMap.get(a)) == null ? void 0 : _a2.t.add(atom2);
304
346
  }
305
- if (Array.from(atomState.d).every(([a, s]) => {
306
- if (a === atom2) {
307
- return true;
308
- }
309
- const aState = readAtomState(a);
310
- return aState === s || isEqualAtomValue(aState, s);
311
- })) {
312
- return atomState;
347
+ if (!isSync) {
348
+ mountDependenciesIfAsync();
313
349
  }
314
350
  }
315
- const nextDependencies = /* @__PURE__ */ new Map();
316
- let isSync = true;
317
- const getter = (a) => {
318
- if (a === atom2) {
319
- const aState2 = getAtomState(a);
320
- if (aState2) {
321
- nextDependencies.set(a, aState2);
322
- return returnAtomValue(aState2);
323
- }
324
- if (hasInitialValue(a)) {
325
- nextDependencies.set(a, void 0);
326
- return a.init;
327
- }
328
- throw new Error("no atom init");
351
+ }
352
+ let controller;
353
+ let setSelf;
354
+ const options = {
355
+ get signal() {
356
+ if (!controller) {
357
+ controller = new AbortController();
329
358
  }
330
- const aState = readAtomState(a);
331
- nextDependencies.set(a, aState);
332
- return returnAtomValue(aState);
333
- };
334
- let controller;
335
- let setSelf;
336
- const options = {
337
- get signal() {
338
- if (!controller) {
339
- controller = new AbortController();
340
- }
341
- return controller.signal;
342
- },
343
- get setSelf() {
344
- if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production" && !isActuallyWritableAtom(atom2)) {
345
- console.warn("setSelf function cannot be used with read-only atom");
346
- }
347
- if (!setSelf && isActuallyWritableAtom(atom2)) {
348
- setSelf = (...args) => {
349
- if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production" && isSync) {
350
- console.warn("setSelf function cannot be called in sync");
351
- }
352
- if (!isSync) {
353
- return writeAtom(atom2, ...args);
359
+ return controller.signal;
360
+ },
361
+ get setSelf() {
362
+ if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production") {
363
+ console.warn(
364
+ "[DEPRECATED] setSelf is deprecated and will be removed in v3."
365
+ );
366
+ }
367
+ if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production" && !isActuallyWritableAtom(atom2)) {
368
+ console.warn("setSelf function cannot be used with read-only atom");
369
+ }
370
+ if (!setSelf && isActuallyWritableAtom(atom2)) {
371
+ setSelf = (...args) => {
372
+ if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production" && isSync) {
373
+ console.warn("setSelf function cannot be called in sync");
374
+ }
375
+ if (!isSync) {
376
+ try {
377
+ return writeAtomState(store, atom2, ...args);
378
+ } finally {
379
+ recomputeInvalidatedAtoms(store);
380
+ flushCallbacks(store);
354
381
  }
355
- };
356
- }
357
- return setSelf;
382
+ }
383
+ };
358
384
  }
359
- };
360
- try {
361
- const valueOrPromise = atom2.read(getter, options);
362
- return setAtomValueOrPromise(
363
- atom2,
364
- valueOrPromise,
365
- nextDependencies,
366
- () => controller == null ? void 0 : controller.abort()
367
- );
368
- } catch (error) {
369
- return setAtomError(atom2, error, nextDependencies);
370
- } finally {
371
- isSync = false;
385
+ return setSelf;
372
386
  }
373
387
  };
374
- const readAtom = (atom2) => returnAtomValue(readAtomState(atom2));
375
- const addAtom = (atom2) => {
376
- let mounted = mountedMap.get(atom2);
377
- if (!mounted) {
378
- mounted = mountAtom(atom2);
388
+ const prevEpochNumber = atomState.n;
389
+ try {
390
+ if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production") {
391
+ storeMutationSet.delete(store);
379
392
  }
380
- return mounted;
381
- };
382
- const canUnmountAtom = (atom2, mounted) => !mounted.l.size && (!mounted.t.size || mounted.t.size === 1 && mounted.t.has(atom2));
383
- const delAtom = (atom2) => {
384
- const mounted = mountedMap.get(atom2);
385
- if (mounted && canUnmountAtom(atom2, mounted)) {
386
- unmountAtom(atom2);
393
+ const valueOrPromise = atomRead(store, atom2, getter, options);
394
+ if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production" && storeMutationSet.has(store)) {
395
+ console.warn(
396
+ "Detected store mutation during atom read. This is not supported."
397
+ );
387
398
  }
388
- };
389
- const recomputeDependents = (atom2) => {
390
- const dependencyMap = /* @__PURE__ */ new Map();
391
- const dirtyMap = /* @__PURE__ */ new WeakMap();
392
- const getDependents = (a) => {
393
- var _a;
394
- const dependents = new Set((_a = mountedMap.get(a)) == null ? void 0 : _a.t);
395
- pendingMap.forEach((_, pendingAtom) => {
396
- var _a2;
397
- if ((_a2 = getAtomState(pendingAtom)) == null ? void 0 : _a2.d.has(a)) {
398
- dependents.add(pendingAtom);
399
- }
400
- });
401
- return dependents;
402
- };
403
- const loop1 = (a) => {
404
- getDependents(a).forEach((dependent) => {
405
- if (dependent !== a) {
406
- dependencyMap.set(
407
- dependent,
408
- (dependencyMap.get(dependent) || /* @__PURE__ */ new Set()).add(a)
409
- );
410
- dirtyMap.set(dependent, (dirtyMap.get(dependent) || 0) + 1);
411
- loop1(dependent);
412
- }
413
- });
414
- };
415
- loop1(atom2);
416
- const loop2 = (a) => {
417
- getDependents(a).forEach((dependent) => {
418
- var _a;
419
- if (dependent !== a) {
420
- let dirtyCount = dirtyMap.get(dependent);
421
- if (dirtyCount) {
422
- dirtyMap.set(dependent, --dirtyCount);
423
- }
424
- if (!dirtyCount) {
425
- let isChanged = !!((_a = dependencyMap.get(dependent)) == null ? void 0 : _a.size);
426
- if (isChanged) {
427
- const prevAtomState = getAtomState(dependent);
428
- const nextAtomState = readAtomState(dependent, true);
429
- isChanged = !isEqualAtomValue(prevAtomState, nextAtomState);
430
- }
431
- if (!isChanged) {
432
- dependencyMap.forEach((s) => s.delete(dependent));
433
- }
434
- }
435
- loop2(dependent);
436
- }
437
- });
438
- };
439
- loop2(atom2);
440
- };
441
- const writeAtomState = (atom2, ...args) => {
442
- let isSync = true;
443
- const getter = (a) => returnAtomValue(readAtomState(a));
444
- const setter = (a, ...args2) => {
445
- let r;
399
+ setAtomStateValueOrPromise(store, atom2, valueOrPromise);
400
+ if (isPromiseLike(valueOrPromise)) {
401
+ registerAbortHandler(valueOrPromise, () => controller == null ? void 0 : controller.abort());
402
+ valueOrPromise.then(mountDependenciesIfAsync, mountDependenciesIfAsync);
403
+ }
404
+ (_a = storeHooks.r) == null ? void 0 : _a.call(storeHooks, atom2);
405
+ return atomState;
406
+ } catch (error) {
407
+ delete atomState.v;
408
+ atomState.e = error;
409
+ ++atomState.n;
410
+ return atomState;
411
+ } finally {
412
+ isSync = false;
413
+ if (prevEpochNumber !== atomState.n && invalidatedAtoms.get(atom2) === prevEpochNumber) {
414
+ invalidatedAtoms.set(atom2, atomState.n);
415
+ changedAtoms.add(atom2);
416
+ (_b = storeHooks.c) == null ? void 0 : _b.call(storeHooks, atom2);
417
+ }
418
+ }
419
+ };
420
+ var BUILDING_BLOCK_invalidateDependents = (store, atom2) => {
421
+ const buildingBlocks = getInternalBuildingBlocks(store);
422
+ const mountedMap = buildingBlocks[1];
423
+ const invalidatedAtoms = buildingBlocks[2];
424
+ const ensureAtomState = buildingBlocks[11];
425
+ const stack = [atom2];
426
+ while (stack.length) {
427
+ const a = stack.pop();
428
+ const aState = ensureAtomState(store, a);
429
+ for (const d of getMountedOrPendingDependents(a, aState, mountedMap)) {
430
+ const dState = ensureAtomState(store, d);
431
+ invalidatedAtoms.set(d, dState.n);
432
+ stack.push(d);
433
+ }
434
+ }
435
+ };
436
+ var BUILDING_BLOCK_writeAtomState = (store, atom2, ...args) => {
437
+ const buildingBlocks = getInternalBuildingBlocks(store);
438
+ const changedAtoms = buildingBlocks[3];
439
+ const storeHooks = buildingBlocks[6];
440
+ const atomWrite = buildingBlocks[8];
441
+ const ensureAtomState = buildingBlocks[11];
442
+ const flushCallbacks = buildingBlocks[12];
443
+ const recomputeInvalidatedAtoms = buildingBlocks[13];
444
+ const readAtomState = buildingBlocks[14];
445
+ const invalidateDependents = buildingBlocks[15];
446
+ const writeAtomState = buildingBlocks[16];
447
+ const mountDependencies = buildingBlocks[17];
448
+ const setAtomStateValueOrPromise = buildingBlocks[20];
449
+ let isSync = true;
450
+ const getter = (a) => returnAtomValue(readAtomState(store, a));
451
+ const setter = (a, ...args2) => {
452
+ var _a;
453
+ const aState = ensureAtomState(store, a);
454
+ try {
446
455
  if (a === atom2) {
447
456
  if (!hasInitialValue(a)) {
448
457
  throw new Error("atom not writable");
449
458
  }
450
- const prevAtomState = getAtomState(a);
451
- const nextAtomState = setAtomValueOrPromise(a, args2[0]);
452
- if (!isEqualAtomValue(prevAtomState, nextAtomState)) {
453
- recomputeDependents(a);
459
+ if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production") {
460
+ storeMutationSet.add(store);
461
+ }
462
+ const prevEpochNumber = aState.n;
463
+ const v = args2[0];
464
+ setAtomStateValueOrPromise(store, a, v);
465
+ mountDependencies(store, a);
466
+ if (prevEpochNumber !== aState.n) {
467
+ changedAtoms.add(a);
468
+ invalidateDependents(store, a);
469
+ (_a = storeHooks.c) == null ? void 0 : _a.call(storeHooks, a);
454
470
  }
471
+ return void 0;
455
472
  } else {
456
- r = writeAtomState(a, ...args2);
473
+ return writeAtomState(store, a, ...args2);
457
474
  }
475
+ } finally {
458
476
  if (!isSync) {
459
- const flushed = flushPending();
460
- if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production") {
461
- storeListenersRev2.forEach(
462
- (l) => l({ type: "async-write", flushed })
463
- );
464
- }
477
+ recomputeInvalidatedAtoms(store);
478
+ flushCallbacks(store);
465
479
  }
466
- return r;
467
- };
468
- const result = atom2.write(getter, setter, ...args);
469
- isSync = false;
470
- return result;
471
- };
472
- const writeAtom = (atom2, ...args) => {
473
- const result = writeAtomState(atom2, ...args);
474
- const flushed = flushPending();
475
- if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production") {
476
- storeListenersRev2.forEach(
477
- (l) => l({ type: "write", flushed })
478
- );
479
480
  }
480
- return result;
481
481
  };
482
- const mountAtom = (atom2, initialDependent, onMountQueue) => {
483
- var _a;
484
- const queue = onMountQueue || [];
485
- (_a = getAtomState(atom2)) == null ? void 0 : _a.d.forEach((_, a) => {
486
- const aMounted = mountedMap.get(a);
487
- if (aMounted) {
482
+ try {
483
+ return atomWrite(store, atom2, getter, setter, ...args);
484
+ } finally {
485
+ isSync = false;
486
+ }
487
+ };
488
+ var BUILDING_BLOCK_mountDependencies = (store, atom2) => {
489
+ var _a;
490
+ const buildingBlocks = getInternalBuildingBlocks(store);
491
+ const mountedMap = buildingBlocks[1];
492
+ const changedAtoms = buildingBlocks[3];
493
+ const storeHooks = buildingBlocks[6];
494
+ const ensureAtomState = buildingBlocks[11];
495
+ const invalidateDependents = buildingBlocks[15];
496
+ const mountAtom = buildingBlocks[18];
497
+ const unmountAtom = buildingBlocks[19];
498
+ const atomState = ensureAtomState(store, atom2);
499
+ const mounted = mountedMap.get(atom2);
500
+ if (mounted && !isPendingPromise(atomState.v)) {
501
+ for (const [a, n] of atomState.d) {
502
+ if (!mounted.d.has(a)) {
503
+ const aState = ensureAtomState(store, a);
504
+ const aMounted = mountAtom(store, a);
488
505
  aMounted.t.add(atom2);
489
- } else {
490
- if (a !== atom2) {
491
- mountAtom(a, atom2, queue);
506
+ mounted.d.add(a);
507
+ if (n !== aState.n) {
508
+ changedAtoms.add(a);
509
+ invalidateDependents(store, a);
510
+ (_a = storeHooks.c) == null ? void 0 : _a.call(storeHooks, a);
492
511
  }
493
512
  }
494
- });
495
- readAtomState(atom2);
496
- const mounted = {
497
- t: new Set(initialDependent && [initialDependent]),
498
- l: /* @__PURE__ */ new Set()
499
- };
500
- mountedMap.set(atom2, mounted);
501
- if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production") {
502
- mountedAtoms.add(atom2);
503
- }
504
- if (isActuallyWritableAtom(atom2) && atom2.onMount) {
505
- const { onMount } = atom2;
506
- queue.push(() => {
507
- const onUnmount = onMount((...args) => writeAtom(atom2, ...args));
508
- if (onUnmount) {
509
- mounted.u = onUnmount;
510
- }
511
- });
512
513
  }
513
- if (!onMountQueue) {
514
- queue.forEach((f) => f());
515
- }
516
- return mounted;
517
- };
518
- const unmountAtom = (atom2) => {
519
- var _a;
520
- const onUnmount = (_a = mountedMap.get(atom2)) == null ? void 0 : _a.u;
521
- if (onUnmount) {
522
- onUnmount();
514
+ for (const a of mounted.d) {
515
+ if (!atomState.d.has(a)) {
516
+ mounted.d.delete(a);
517
+ const aMounted = unmountAtom(store, a);
518
+ aMounted == null ? void 0 : aMounted.t.delete(atom2);
519
+ }
523
520
  }
524
- mountedMap.delete(atom2);
525
- if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production") {
526
- mountedAtoms.delete(atom2);
521
+ }
522
+ };
523
+ var BUILDING_BLOCK_mountAtom = (store, atom2) => {
524
+ var _a;
525
+ const buildingBlocks = getInternalBuildingBlocks(store);
526
+ const mountedMap = buildingBlocks[1];
527
+ const mountCallbacks = buildingBlocks[4];
528
+ const storeHooks = buildingBlocks[6];
529
+ const atomOnMount = buildingBlocks[10];
530
+ const ensureAtomState = buildingBlocks[11];
531
+ const flushCallbacks = buildingBlocks[12];
532
+ const recomputeInvalidatedAtoms = buildingBlocks[13];
533
+ const readAtomState = buildingBlocks[14];
534
+ const writeAtomState = buildingBlocks[16];
535
+ const mountAtom = buildingBlocks[18];
536
+ const atomState = ensureAtomState(store, atom2);
537
+ let mounted = mountedMap.get(atom2);
538
+ if (!mounted) {
539
+ readAtomState(store, atom2);
540
+ for (const a of atomState.d.keys()) {
541
+ const aMounted = mountAtom(store, a);
542
+ aMounted.t.add(atom2);
527
543
  }
528
- const atomState = getAtomState(atom2);
529
- if (atomState) {
530
- if (hasPromiseAtomValue(atomState)) {
531
- cancelPromise(atomState.v);
532
- }
533
- atomState.d.forEach((_, a) => {
534
- if (a !== atom2) {
535
- const mounted = mountedMap.get(a);
536
- if (mounted) {
537
- mounted.t.delete(atom2);
538
- if (canUnmountAtom(a, mounted)) {
539
- unmountAtom(a);
544
+ mounted = {
545
+ l: /* @__PURE__ */ new Set(),
546
+ d: new Set(atomState.d.keys()),
547
+ t: /* @__PURE__ */ new Set()
548
+ };
549
+ mountedMap.set(atom2, mounted);
550
+ if (isActuallyWritableAtom(atom2)) {
551
+ const processOnMount = () => {
552
+ let isSync = true;
553
+ const setAtom = (...args) => {
554
+ try {
555
+ return writeAtomState(store, atom2, ...args);
556
+ } finally {
557
+ if (!isSync) {
558
+ recomputeInvalidatedAtoms(store);
559
+ flushCallbacks(store);
540
560
  }
541
561
  }
562
+ };
563
+ try {
564
+ const onUnmount = atomOnMount(store, atom2, setAtom);
565
+ if (onUnmount) {
566
+ mounted.u = () => {
567
+ isSync = true;
568
+ try {
569
+ onUnmount();
570
+ } finally {
571
+ isSync = false;
572
+ }
573
+ };
574
+ }
575
+ } finally {
576
+ isSync = false;
542
577
  }
543
- });
544
- } else if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production") {
545
- console.warn("[Bug] could not find atom state to unmount", atom2);
578
+ };
579
+ mountCallbacks.add(processOnMount);
546
580
  }
547
- };
548
- const mountDependencies = (atom2, atomState, prevDependencies) => {
549
- const depSet = new Set(atomState.d.keys());
550
- const maybeUnmountAtomSet = /* @__PURE__ */ new Set();
551
- prevDependencies == null ? void 0 : prevDependencies.forEach((_, a) => {
552
- if (depSet.has(a)) {
553
- depSet.delete(a);
554
- return;
555
- }
556
- maybeUnmountAtomSet.add(a);
557
- const mounted = mountedMap.get(a);
558
- if (mounted) {
559
- mounted.t.delete(atom2);
560
- }
561
- });
562
- depSet.forEach((a) => {
563
- const mounted = mountedMap.get(a);
564
- if (mounted) {
565
- mounted.t.add(atom2);
566
- } else if (mountedMap.has(atom2)) {
567
- mountAtom(a, atom2);
568
- }
569
- });
570
- maybeUnmountAtomSet.forEach((a) => {
571
- const mounted = mountedMap.get(a);
572
- if (mounted && canUnmountAtom(a, mounted)) {
573
- unmountAtom(a);
574
- }
575
- });
576
- };
577
- const flushPending = () => {
578
- let flushed;
579
- if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production") {
580
- flushed = /* @__PURE__ */ new Set();
581
+ (_a = storeHooks.m) == null ? void 0 : _a.call(storeHooks, atom2);
582
+ }
583
+ return mounted;
584
+ };
585
+ var BUILDING_BLOCK_unmountAtom = (store, atom2) => {
586
+ var _a, _b;
587
+ const buildingBlocks = getInternalBuildingBlocks(store);
588
+ const mountedMap = buildingBlocks[1];
589
+ const unmountCallbacks = buildingBlocks[5];
590
+ const storeHooks = buildingBlocks[6];
591
+ const ensureAtomState = buildingBlocks[11];
592
+ const unmountAtom = buildingBlocks[19];
593
+ const atomState = ensureAtomState(store, atom2);
594
+ let mounted = mountedMap.get(atom2);
595
+ if (!mounted || mounted.l.size) {
596
+ return mounted;
597
+ }
598
+ let isDependent = false;
599
+ for (const a of mounted.t) {
600
+ if ((_a = mountedMap.get(a)) == null ? void 0 : _a.d.has(atom2)) {
601
+ isDependent = true;
602
+ break;
581
603
  }
582
- while (pendingMap.size) {
583
- const pending = Array.from(pendingMap);
584
- pendingMap.clear();
585
- pending.forEach(([atom2, prevAtomState]) => {
586
- const atomState = getAtomState(atom2);
587
- if (atomState) {
588
- const mounted = mountedMap.get(atom2);
589
- if (mounted && atomState.d !== (prevAtomState == null ? void 0 : prevAtomState.d)) {
590
- mountDependencies(atom2, atomState, prevAtomState == null ? void 0 : prevAtomState.d);
591
- }
592
- if (mounted && !// TODO This seems pretty hacky. Hope to fix it.
593
- // Maybe we could `mountDependencies` in `setAtomState`?
594
- (!hasPromiseAtomValue(prevAtomState) && (isEqualAtomValue(prevAtomState, atomState) || isEqualAtomError(prevAtomState, atomState)))) {
595
- mounted.l.forEach((listener) => listener());
596
- if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production") {
597
- flushed.add(atom2);
598
- }
599
- }
600
- } else if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production") {
601
- console.warn("[Bug] no atom state to flush");
602
- }
603
- });
604
+ }
605
+ if (!isDependent) {
606
+ if (mounted.u) {
607
+ unmountCallbacks.add(mounted.u);
604
608
  }
605
- if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production") {
606
- return flushed;
609
+ mounted = void 0;
610
+ mountedMap.delete(atom2);
611
+ for (const a of atomState.d.keys()) {
612
+ const aMounted = unmountAtom(store, a);
613
+ aMounted == null ? void 0 : aMounted.t.delete(atom2);
607
614
  }
608
- };
609
- const subscribeAtom = (atom2, listener) => {
610
- const mounted = addAtom(atom2);
611
- const flushed = flushPending();
612
- const listeners = mounted.l;
613
- listeners.add(listener);
614
- if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production") {
615
- storeListenersRev2.forEach(
616
- (l) => l({ type: "sub", flushed })
615
+ (_b = storeHooks.u) == null ? void 0 : _b.call(storeHooks, atom2);
616
+ return void 0;
617
+ }
618
+ return mounted;
619
+ };
620
+ var BUILDING_BLOCK_setAtomStateValueOrPromise = (store, atom2, valueOrPromise) => {
621
+ const ensureAtomState = getInternalBuildingBlocks(store)[11];
622
+ const atomState = ensureAtomState(store, atom2);
623
+ const hasPrevValue = "v" in atomState;
624
+ const prevValue = atomState.v;
625
+ if (isPromiseLike(valueOrPromise)) {
626
+ for (const a of atomState.d.keys()) {
627
+ addPendingPromiseToDependency(
628
+ atom2,
629
+ valueOrPromise,
630
+ ensureAtomState(store, a)
617
631
  );
618
632
  }
619
- return () => {
620
- listeners.delete(listener);
621
- delAtom(atom2);
622
- if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production") {
623
- storeListenersRev2.forEach((l) => l({ type: "unsub" }));
624
- }
625
- };
626
- };
627
- if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production") {
628
- return {
629
- get: readAtom,
630
- set: writeAtom,
631
- sub: subscribeAtom,
632
- // store dev methods (these are tentative and subject to change without notice)
633
- dev_subscribe_store: (l, rev) => {
634
- if (rev !== 2) {
635
- throw new Error("The current StoreListener revision is 2.");
636
- }
637
- storeListenersRev2.add(l);
638
- return () => {
639
- storeListenersRev2.delete(l);
640
- };
641
- },
642
- dev_get_mounted_atoms: () => mountedAtoms.values(),
643
- dev_get_atom_state: (a) => atomStateMap.get(a),
644
- dev_get_mounted: (a) => mountedMap.get(a),
645
- dev_restore_atoms: (values) => {
646
- for (const [atom2, valueOrPromise] of values) {
647
- if (hasInitialValue(atom2)) {
648
- setAtomValueOrPromise(atom2, valueOrPromise);
649
- recomputeDependents(atom2);
650
- }
651
- }
652
- const flushed = flushPending();
653
- storeListenersRev2.forEach(
654
- (l) => l({ type: "restore", flushed })
655
- );
656
- }
657
- };
658
633
  }
659
- return {
660
- get: readAtom,
661
- set: writeAtom,
662
- sub: subscribeAtom
634
+ atomState.v = valueOrPromise;
635
+ delete atomState.e;
636
+ if (!hasPrevValue || !Object.is(prevValue, atomState.v)) {
637
+ ++atomState.n;
638
+ if (isPromiseLike(prevValue)) {
639
+ abortPromise(prevValue);
640
+ }
641
+ }
642
+ };
643
+ var BUILDING_BLOCK_storeGet = (store, atom2) => {
644
+ const readAtomState = getInternalBuildingBlocks(store)[14];
645
+ return returnAtomValue(readAtomState(store, atom2));
646
+ };
647
+ var BUILDING_BLOCK_storeSet = (store, atom2, ...args) => {
648
+ const buildingBlocks = getInternalBuildingBlocks(store);
649
+ const flushCallbacks = buildingBlocks[12];
650
+ const recomputeInvalidatedAtoms = buildingBlocks[13];
651
+ const writeAtomState = buildingBlocks[16];
652
+ try {
653
+ return writeAtomState(store, atom2, ...args);
654
+ } finally {
655
+ recomputeInvalidatedAtoms(store);
656
+ flushCallbacks(store);
657
+ }
658
+ };
659
+ var BUILDING_BLOCK_storeSub = (store, atom2, listener) => {
660
+ const buildingBlocks = getInternalBuildingBlocks(store);
661
+ const flushCallbacks = buildingBlocks[12];
662
+ const mountAtom = buildingBlocks[18];
663
+ const unmountAtom = buildingBlocks[19];
664
+ const mounted = mountAtom(store, atom2);
665
+ const listeners = mounted.l;
666
+ listeners.add(listener);
667
+ flushCallbacks(store);
668
+ return () => {
669
+ listeners.delete(listener);
670
+ unmountAtom(store, atom2);
671
+ flushCallbacks(store);
663
672
  };
664
673
  };
665
- var defaultStore;
666
- if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production") {
667
- if (typeof globalThis.__NUMBER_OF_JOTAI_INSTANCES__ === "number") {
668
- ++globalThis.__NUMBER_OF_JOTAI_INSTANCES__;
674
+ var buildingBlockMap = /* @__PURE__ */ new WeakMap();
675
+ var getInternalBuildingBlocks = (store) => {
676
+ const buildingBlocks = buildingBlockMap.get(store);
677
+ if ((import_meta.env ? import_meta.env.MODE : void 0) !== "production" && !buildingBlocks) {
678
+ throw new Error(
679
+ "Store must be created by buildStore to read its building blocks"
680
+ );
681
+ }
682
+ return buildingBlocks;
683
+ };
684
+ function buildStore(...buildArgs) {
685
+ const store = {
686
+ get(atom2) {
687
+ const storeGet = getInternalBuildingBlocks(store)[21];
688
+ return storeGet(store, atom2);
689
+ },
690
+ set(atom2, ...args) {
691
+ const storeSet = getInternalBuildingBlocks(store)[22];
692
+ return storeSet(store, atom2, ...args);
693
+ },
694
+ sub(atom2, listener) {
695
+ const storeSub = getInternalBuildingBlocks(store)[23];
696
+ return storeSub(store, atom2, listener);
697
+ }
698
+ };
699
+ const buildingBlocks = [
700
+ // store state
701
+ /* @__PURE__ */ new WeakMap(),
702
+ // atomStateMap
703
+ /* @__PURE__ */ new WeakMap(),
704
+ // mountedMap
705
+ /* @__PURE__ */ new WeakMap(),
706
+ // invalidatedAtoms
707
+ /* @__PURE__ */ new Set(),
708
+ // changedAtoms
709
+ /* @__PURE__ */ new Set(),
710
+ // mountCallbacks
711
+ /* @__PURE__ */ new Set(),
712
+ // unmountCallbacks
713
+ {},
714
+ // storeHooks
715
+ // atom interceptors
716
+ BUILDING_BLOCK_atomRead,
717
+ BUILDING_BLOCK_atomWrite,
718
+ BUILDING_BLOCK_atomOnInit,
719
+ BUILDING_BLOCK_atomOnMount,
720
+ // building-block functions
721
+ BUILDING_BLOCK_ensureAtomState,
722
+ BUILDING_BLOCK_flushCallbacks,
723
+ BUILDING_BLOCK_recomputeInvalidatedAtoms,
724
+ BUILDING_BLOCK_readAtomState,
725
+ BUILDING_BLOCK_invalidateDependents,
726
+ BUILDING_BLOCK_writeAtomState,
727
+ BUILDING_BLOCK_mountDependencies,
728
+ BUILDING_BLOCK_mountAtom,
729
+ BUILDING_BLOCK_unmountAtom,
730
+ BUILDING_BLOCK_setAtomStateValueOrPromise,
731
+ BUILDING_BLOCK_storeGet,
732
+ BUILDING_BLOCK_storeSet,
733
+ BUILDING_BLOCK_storeSub,
734
+ void 0
735
+ ].map((fn, i) => buildArgs[i] || fn);
736
+ buildingBlockMap.set(store, Object.freeze(buildingBlocks));
737
+ return store;
738
+ }
739
+
740
+ // ../../node_modules/jotai/esm/vanilla.mjs
741
+ var import_meta2 = {};
742
+ var keyCount = 0;
743
+ function atom(read, write) {
744
+ const key = `atom${++keyCount}`;
745
+ const config = {
746
+ toString() {
747
+ return (import_meta2.env ? import_meta2.env.MODE : void 0) !== "production" && this.debugLabel ? key + ":" + this.debugLabel : key;
748
+ }
749
+ };
750
+ if (typeof read === "function") {
751
+ config.read = read;
669
752
  } else {
670
- globalThis.__NUMBER_OF_JOTAI_INSTANCES__ = 1;
753
+ config.init = read;
754
+ config.read = defaultRead;
755
+ config.write = defaultWrite;
756
+ }
757
+ if (write) {
758
+ config.write = write;
671
759
  }
760
+ return config;
761
+ }
762
+ function defaultRead(get) {
763
+ return get(this);
764
+ }
765
+ function defaultWrite(get, set, arg) {
766
+ return set(
767
+ this,
768
+ typeof arg === "function" ? arg(get(this)) : arg
769
+ );
770
+ }
771
+ var overriddenCreateStore;
772
+ function createStore() {
773
+ if (overriddenCreateStore) {
774
+ return overriddenCreateStore();
775
+ }
776
+ return buildStore();
672
777
  }
673
- var getDefaultStore = () => {
778
+ var defaultStore;
779
+ function getDefaultStore() {
674
780
  if (!defaultStore) {
675
781
  defaultStore = createStore();
782
+ if ((import_meta2.env ? import_meta2.env.MODE : void 0) !== "production") {
783
+ globalThis.__JOTAI_DEFAULT_STORE__ || (globalThis.__JOTAI_DEFAULT_STORE__ = defaultStore);
784
+ if (globalThis.__JOTAI_DEFAULT_STORE__ !== defaultStore) {
785
+ console.warn(
786
+ "Detected multiple Jotai instances. It may cause unexpected behavior with the default store. https://github.com/pmndrs/jotai/discussions/2044"
787
+ );
788
+ }
789
+ }
676
790
  }
677
791
  return defaultStore;
678
- };
792
+ }
679
793
 
680
794
  // ../../node_modules/jotai/esm/react.mjs
681
795
  var import_react = __toESM(require("react"), 1);
682
- var import_meta2 = {};
683
- var StoreContext = (0, import_react.createContext)(void 0);
684
- var useStore = (options) => {
796
+ var import_meta3 = {};
797
+ var StoreContext = (0, import_react.createContext)(
798
+ void 0
799
+ );
800
+ function useStore(options) {
685
801
  const store = (0, import_react.useContext)(StoreContext);
686
802
  return (options == null ? void 0 : options.store) || store || getDefaultStore();
687
- };
803
+ }
688
804
  var isPromiseLike2 = (x) => typeof (x == null ? void 0 : x.then) === "function";
689
- var use = import_react.default.use || ((promise) => {
690
- if (promise.status === "pending") {
691
- throw promise;
692
- } else if (promise.status === "fulfilled") {
693
- return promise.value;
694
- } else if (promise.status === "rejected") {
695
- throw promise.reason;
696
- } else {
805
+ var attachPromiseStatus = (promise) => {
806
+ if (!promise.status) {
697
807
  promise.status = "pending";
698
808
  promise.then(
699
809
  (v) => {
@@ -705,10 +815,61 @@ var use = import_react.default.use || ((promise) => {
705
815
  promise.reason = e;
706
816
  }
707
817
  );
818
+ }
819
+ };
820
+ var use = import_react.default.use || // A shim for older React versions
821
+ ((promise) => {
822
+ if (promise.status === "pending") {
823
+ throw promise;
824
+ } else if (promise.status === "fulfilled") {
825
+ return promise.value;
826
+ } else if (promise.status === "rejected") {
827
+ throw promise.reason;
828
+ } else {
829
+ attachPromiseStatus(promise);
708
830
  throw promise;
709
831
  }
710
832
  });
833
+ var continuablePromiseMap = /* @__PURE__ */ new WeakMap();
834
+ var createContinuablePromise = (promise, getValue) => {
835
+ let continuablePromise = continuablePromiseMap.get(promise);
836
+ if (!continuablePromise) {
837
+ continuablePromise = new Promise((resolve, reject) => {
838
+ let curr = promise;
839
+ const onFulfilled = (me) => (v) => {
840
+ if (curr === me) {
841
+ resolve(v);
842
+ }
843
+ };
844
+ const onRejected = (me) => (e) => {
845
+ if (curr === me) {
846
+ reject(e);
847
+ }
848
+ };
849
+ const onAbort = () => {
850
+ try {
851
+ const nextValue = getValue();
852
+ if (isPromiseLike2(nextValue)) {
853
+ continuablePromiseMap.set(nextValue, continuablePromise);
854
+ curr = nextValue;
855
+ nextValue.then(onFulfilled(nextValue), onRejected(nextValue));
856
+ registerAbortHandler(nextValue, onAbort);
857
+ } else {
858
+ resolve(nextValue);
859
+ }
860
+ } catch (e) {
861
+ reject(e);
862
+ }
863
+ };
864
+ promise.then(onFulfilled(promise), onRejected(promise));
865
+ registerAbortHandler(promise, onAbort);
866
+ });
867
+ continuablePromiseMap.set(promise, continuablePromise);
868
+ }
869
+ return continuablePromise;
870
+ };
711
871
  function useAtomValue(atom2, options) {
872
+ const { delay, unstable_promiseStatus: promiseStatus = !import_react.default.use } = options || {};
712
873
  const store = useStore(options);
713
874
  const [[valueFromReducer, storeFromReducer, atomFromReducer], rerender] = (0, import_react.useReducer)(
714
875
  (prev) => {
@@ -726,9 +887,19 @@ function useAtomValue(atom2, options) {
726
887
  rerender();
727
888
  value = store.get(atom2);
728
889
  }
729
- const delay = options == null ? void 0 : options.delay;
730
890
  (0, import_react.useEffect)(() => {
731
891
  const unsub = store.sub(atom2, () => {
892
+ if (promiseStatus) {
893
+ try {
894
+ const value2 = store.get(atom2);
895
+ if (isPromiseLike2(value2)) {
896
+ attachPromiseStatus(
897
+ createContinuablePromise(value2, () => store.get(atom2))
898
+ );
899
+ }
900
+ } catch (e) {
901
+ }
902
+ }
732
903
  if (typeof delay === "number") {
733
904
  setTimeout(rerender, delay);
734
905
  return;
@@ -737,15 +908,22 @@ function useAtomValue(atom2, options) {
737
908
  });
738
909
  rerender();
739
910
  return unsub;
740
- }, [store, atom2, delay]);
911
+ }, [store, atom2, delay, promiseStatus]);
741
912
  (0, import_react.useDebugValue)(value);
742
- return isPromiseLike2(value) ? use(value) : value;
913
+ if (isPromiseLike2(value)) {
914
+ const promise = createContinuablePromise(value, () => store.get(atom2));
915
+ if (promiseStatus) {
916
+ attachPromiseStatus(promise);
917
+ }
918
+ return use(promise);
919
+ }
920
+ return value;
743
921
  }
744
922
  function useSetAtom(atom2, options) {
745
923
  const store = useStore(options);
746
924
  const setAtom = (0, import_react.useCallback)(
747
925
  (...args) => {
748
- if ((import_meta2.env ? import_meta2.env.MODE : void 0) !== "production" && !("write" in atom2)) {
926
+ if ((import_meta3.env ? import_meta3.env.MODE : void 0) !== "production" && !("write" in atom2)) {
749
927
  throw new Error("not writable atom");
750
928
  }
751
929
  return store.set(atom2, ...args);