cross-state 0.33.2 → 0.33.5

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 (48) hide show
  1. package/dist/cjs/cache.cjs +94 -118
  2. package/dist/cjs/cache.cjs.map +1 -1
  3. package/dist/cjs/immer/index.cjs +3 -21
  4. package/dist/cjs/immer/index.cjs.map +1 -1
  5. package/dist/cjs/immer/register.cjs +2 -2
  6. package/dist/cjs/immer/register.cjs.map +1 -1
  7. package/dist/cjs/immerMethods.cjs +23 -0
  8. package/dist/cjs/immerMethods.cjs.map +1 -0
  9. package/dist/cjs/index.cjs +0 -114
  10. package/dist/cjs/index.cjs.map +1 -1
  11. package/dist/cjs/react/index.cjs +0 -11
  12. package/dist/cjs/react/index.cjs.map +1 -1
  13. package/dist/cjs/store.cjs +319 -306
  14. package/dist/cjs/store.cjs.map +1 -1
  15. package/dist/cjs/useCache.cjs +152 -142
  16. package/dist/cjs/useCache.cjs.map +1 -1
  17. package/dist/es/cache.mjs +92 -116
  18. package/dist/es/cache.mjs.map +1 -1
  19. package/dist/es/immer/index.mjs +3 -21
  20. package/dist/es/immer/index.mjs.map +1 -1
  21. package/dist/es/immer/register.mjs +1 -1
  22. package/dist/es/immerMethods.mjs +24 -0
  23. package/dist/es/immerMethods.mjs.map +1 -0
  24. package/dist/es/index.mjs +36 -151
  25. package/dist/es/index.mjs.map +1 -1
  26. package/dist/es/react/index.mjs +4 -15
  27. package/dist/es/react/index.mjs.map +1 -1
  28. package/dist/es/store.mjs +330 -317
  29. package/dist/es/store.mjs.map +1 -1
  30. package/dist/es/useCache.mjs +153 -143
  31. package/dist/es/useCache.mjs.map +1 -1
  32. package/dist/types/core/cache.d.ts +9 -18
  33. package/dist/types/core/commonTypes.d.ts +23 -6
  34. package/dist/types/core/index.d.ts +1 -2
  35. package/dist/types/core/store.d.ts +9 -16
  36. package/dist/types/lib/cacheState.d.ts +1 -0
  37. package/dist/types/lib/calculatedValue.d.ts +9 -0
  38. package/dist/types/lib/deferred.d.ts +6 -0
  39. package/dist/types/lib/disposable.d.ts +3 -0
  40. package/dist/types/lib/promiseWithState.d.ts +1 -0
  41. package/dist/types/react/index.d.ts +0 -1
  42. package/dist/types/react/register.d.ts +2 -2
  43. package/dist/types/react/scope.d.ts +2 -2
  44. package/dist/types/sync/sync.d.ts +1 -1
  45. package/package.json +22 -22
  46. package/dist/types/core/subscriptionCache.d.ts +0 -62
  47. package/dist/types/lib/calculationHelper.d.ts +0 -27
  48. package/dist/types/react/read.d.ts +0 -3
package/dist/es/store.mjs CHANGED
@@ -44,55 +44,20 @@ function calcDuration(t) {
44
44
  return t;
45
45
  return (t.milliseconds ?? 0) + (t.seconds ?? 0) * 1e3 + (t.minutes ?? 0) * 60 * 1e3 + (t.hours ?? 0) * 60 * 60 * 1e3 + (t.days ?? 0) * 24 * 60 * 60 * 1e3;
46
46
  }
47
- function defaultEqual(a, b) {
48
- return a === b;
49
- }
50
- function shallowEqual(a, b) {
51
- return internalEqual(defaultEqual)(a, b);
52
- }
53
- function deepEqual(a, b) {
54
- return internalEqual(deepEqual)(a, b);
55
- }
56
- const internalEqual = (comp) => (a, b) => {
57
- if (a === b) {
58
- return true;
59
- }
60
- if (a === null || b === null || typeof a !== "object" || typeof b !== "object") {
61
- return a !== a && b !== b;
62
- }
63
- if (a.constructor !== b.constructor) {
64
- return false;
65
- }
66
- if (a.constructor === Object) {
67
- const entries1 = Object.entries(a);
68
- const entries2 = Object.entries(b);
69
- return entries1.length === entries2.length && entries1.every(([key, value]) => comp(value, b[key]));
70
- }
71
- if (Array.isArray(a)) {
72
- return a.length === b.length && a.every((value, i) => comp(value, b[i]));
73
- }
74
- if (a instanceof Date) {
75
- return a.getTime() === b.getTime();
76
- }
77
- if (a instanceof RegExp) {
78
- return a.source === b.source && a.flags === b.flags;
79
- }
80
- if (a instanceof Map) {
81
- return a.size === b.size && [...a.entries()].every(([key, value]) => comp(value, b.get(key)));
47
+ class Deferred extends Promise {
48
+ constructor() {
49
+ const that = {};
50
+ super((resolve, reject) => {
51
+ Object.assign(that, { resolve, reject });
52
+ });
53
+ this.resolve = () => void 0;
54
+ this.reject = () => void 0;
55
+ Object.assign(this, that);
82
56
  }
83
- if (a instanceof Set) {
84
- return a.size === b.size && [...a.values()].every((value) => b.has(value));
57
+ static get [Symbol.species]() {
58
+ return Promise;
85
59
  }
86
- if (typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView(a)) {
87
- if (a.byteLength !== b.byteLength) {
88
- return false;
89
- }
90
- const a_ = new Int8Array(a.buffer);
91
- const b_ = new Int8Array(b.buffer);
92
- return a_.every((value, i) => value === b_[i]);
93
- }
94
- return false;
95
- };
60
+ }
96
61
  function queue() {
97
62
  const q = [];
98
63
  const completionListeners = /* @__PURE__ */ new Set();
@@ -150,219 +115,214 @@ function queue() {
150
115
  }
151
116
  );
152
117
  }
153
- class CalculationHelper {
154
- constructor(options) {
155
- this.options = options;
156
- options.addEffect(() => {
157
- if (this.current) {
158
- this.current.check();
159
- } else {
160
- this.execute();
161
- }
118
+ function defaultEqual(a, b) {
119
+ return a === b;
120
+ }
121
+ function shallowEqual(a, b) {
122
+ return internalEqual(defaultEqual)(a, b);
123
+ }
124
+ function deepEqual(a, b) {
125
+ return internalEqual(deepEqual)(a, b);
126
+ }
127
+ const internalEqual = (comp) => (a, b) => {
128
+ if (a === b) {
129
+ return true;
130
+ }
131
+ if (a === null || b === null || typeof a !== "object" || typeof b !== "object") {
132
+ return a !== a && b !== b;
133
+ }
134
+ if (a.constructor !== b.constructor) {
135
+ return false;
136
+ }
137
+ if (a.constructor === Object) {
138
+ const entries1 = Object.entries(a);
139
+ const entries2 = Object.entries(b);
140
+ return entries1.length === entries2.length && entries1.every(([key, value]) => comp(value, b[key]));
141
+ }
142
+ if (Array.isArray(a)) {
143
+ return a.length === b.length && a.every((value, i) => comp(value, b[i]));
144
+ }
145
+ if (a instanceof Date) {
146
+ return a.getTime() === b.getTime();
147
+ }
148
+ if (a instanceof RegExp) {
149
+ return a.source === b.source && a.flags === b.flags;
150
+ }
151
+ if (a instanceof Map) {
152
+ return a.size === b.size && [...a.entries()].every(([key, value]) => comp(value, b.get(key)));
153
+ }
154
+ if (a instanceof Set) {
155
+ return a.size === b.size && [...a.values()].every((value) => b.has(value));
156
+ }
157
+ if (typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView(a)) {
158
+ if (a.byteLength !== b.byteLength) {
159
+ return false;
160
+ }
161
+ const a_ = new Int8Array(a.buffer);
162
+ const b_ = new Int8Array(b.buffer);
163
+ return a_.every((value, i) => value === b_[i]);
164
+ }
165
+ return false;
166
+ };
167
+ class PromiseWithState extends Promise {
168
+ constructor(value, state = { status: "pending" }) {
169
+ super((resolve) => resolve(value));
170
+ this.state = state;
171
+ value.then((value2) => {
172
+ this.state = { status: "value", value: value2 };
173
+ }).catch((error) => {
174
+ this.state = { status: "error", error };
162
175
  });
163
176
  }
164
- execute() {
165
- this.stop();
166
- const { calculate, addEffect, getValue, onValue, onError, onConnectionState, onInvalidate } = this.options;
167
- const checks = new Array();
168
- const deps = /* @__PURE__ */ new Map();
169
- const q = queue();
170
- let isActive = false;
171
- let isCancled = false;
172
- const cancelEffect = addEffect(() => {
173
- isActive = true;
174
- for (const dep of deps.values()) {
175
- dep.on();
176
- }
177
- return () => {
178
- isActive = false;
179
- for (const dep of deps.values()) {
180
- dep.off();
181
- }
182
- if (cancelSubscription) {
183
- cancelSubscription();
184
- cancelSubscription = void 0;
185
- cancel();
186
- onInvalidate == null ? void 0 : onInvalidate();
187
- }
188
- };
177
+ static get [Symbol.species]() {
178
+ return Promise;
179
+ }
180
+ static resolve(value) {
181
+ return new PromiseWithState(Promise.resolve(value), {
182
+ status: "value",
183
+ value
189
184
  });
190
- const cancel = () => {
191
- isCancled = true;
192
- cancelSubscription == null ? void 0 : cancelSubscription();
193
- cancelEffect();
194
- delete this.current;
195
- };
196
- const checkAll = () => {
197
- if (!checks.every((check) => check())) {
198
- cancel();
199
- onInvalidate == null ? void 0 : onInvalidate();
185
+ }
186
+ static reject(error) {
187
+ return new PromiseWithState(Promise.reject(error), { status: "error", error });
188
+ }
189
+ }
190
+ function calculatedValue(store, notify) {
191
+ let active = false;
192
+ const deps = new Array();
193
+ let value;
194
+ const whenConnected = new Deferred();
195
+ const whenExecuted = new Deferred();
196
+ let cancelConnection;
197
+ const q = queue();
198
+ q(() => whenExecuted);
199
+ const cancelEffect = store.addEffect(() => {
200
+ if (cancelConnection) {
201
+ store.invalidate();
202
+ return;
203
+ }
204
+ active = true;
205
+ for (const dep of deps) {
206
+ dep.on();
207
+ }
208
+ return () => {
209
+ active = false;
210
+ for (const dep of deps) {
211
+ dep.off();
200
212
  }
201
- };
202
- const invalidateDependencies = () => {
203
- for (const dep of deps.values()) {
204
- dep.invalidate();
213
+ cancelConnection == null ? void 0 : cancelConnection();
214
+ if ("state" in store) {
215
+ store.state.set("isConnected", false);
205
216
  }
206
217
  };
207
- const use = (store) => {
208
- if (isCancled) {
209
- return store.get();
210
- }
211
- const value = store.get();
212
- const equals = (newValue) => {
213
- return deepEqual(newValue, value);
214
- };
215
- const check = () => equals(store.get());
216
- let sub;
217
- const dep = {
218
- on() {
219
- this.off();
220
- sub = store.subscribe(
221
- () => {
222
- if (sub && !check()) {
223
- cancel();
224
- onInvalidate == null ? void 0 : onInvalidate();
225
- }
226
- },
227
- { runNow: false }
228
- );
229
- },
230
- off() {
231
- sub == null ? void 0 : sub();
232
- sub = void 0;
233
- },
234
- invalidate() {
235
- if ("invalidate" in store && store.invalidate instanceof Function) {
236
- store.invalidate();
237
- }
238
- }
239
- };
240
- if (isActive) {
241
- dep.on();
242
- }
243
- checks.push(check);
244
- deps.set(store, dep);
245
- return value;
218
+ });
219
+ function use(dep) {
220
+ const value2 = dep.get();
221
+ let cancel;
222
+ const on = () => {
223
+ cancel || (cancel = dep.subscribe(() => store.invalidate(), { runNow: false }));
246
224
  };
247
- const updateValue = (update) => q(async () => {
248
- if (isCancled) {
249
- return;
250
- }
251
- if (update instanceof Function) {
252
- try {
253
- update = update(getValue());
254
- } catch (error) {
255
- onError == null ? void 0 : onError(error);
256
- return;
257
- }
258
- }
259
- if (update instanceof Promise) {
260
- try {
261
- update = await update;
262
- } catch (error) {
263
- if (!isCancled) {
264
- onError == null ? void 0 : onError(error);
225
+ const off = () => {
226
+ cancel == null ? void 0 : cancel();
227
+ cancel = void 0;
228
+ };
229
+ deps.push({ store: dep, value: value2, on, off });
230
+ if (active) {
231
+ on();
232
+ }
233
+ return value2;
234
+ }
235
+ async function connect(connection) {
236
+ if (!active) {
237
+ return;
238
+ }
239
+ const actions = {
240
+ set(value2) {
241
+ q(() => {
242
+ store.set(value2);
243
+ });
244
+ },
245
+ updateValue(update) {
246
+ q(async () => {
247
+ if (update instanceof Function) {
248
+ update = update(await value);
249
+ }
250
+ if (update instanceof Promise) {
251
+ update = await update;
265
252
  }
266
- return;
253
+ value = PromiseWithState.resolve(update);
254
+ notify();
255
+ });
256
+ },
257
+ updateError(error) {
258
+ q(() => {
259
+ store.set(Promise.reject(error));
260
+ });
261
+ },
262
+ updateIsConnected(isConnected) {
263
+ if (isConnected) {
264
+ whenConnected.resolve();
267
265
  }
266
+ q(() => {
267
+ if ("state" in store) {
268
+ store.state.set("isConnected", isConnected);
269
+ }
270
+ });
271
+ },
272
+ close() {
273
+ store.invalidate();
268
274
  }
269
- if (!isCancled) {
270
- onValue == null ? void 0 : onValue(update);
271
- }
272
- });
273
- const updateError = (error) => q(() => {
274
- if (!isCancled) {
275
- onError == null ? void 0 : onError(error);
276
- }
277
- });
278
- const updateConnectionState = (state) => q(() => {
279
- if (!isCancled) {
280
- onConnectionState == null ? void 0 : onConnectionState(state);
281
- }
282
- });
283
- let cancelSubscription;
284
- try {
285
- cancelSubscription = calculate({ use, updateValue, updateError, updateConnectionState });
286
- } catch (error) {
287
- onError == null ? void 0 : onError(error);
288
- }
289
- this.current = { cancel, check: checkAll, invalidateDependencies };
275
+ };
276
+ cancelConnection = connection(actions);
277
+ return whenConnected;
290
278
  }
291
- stop() {
292
- var _a;
293
- (_a = this.current) == null ? void 0 : _a.cancel();
279
+ value = store.getter instanceof Function ? store.getter({ use, connect }) : store.getter;
280
+ if (value instanceof Promise) {
281
+ value.finally(() => whenExecuted.resolve()).catch(() => void 0);
294
282
  }
295
- check() {
296
- var _a;
297
- (_a = this.current) == null ? void 0 : _a.check();
298
- }
299
- checkOrExecute() {
300
- if (this.current) {
301
- this.check();
302
- } else {
303
- this.execute();
283
+ function check() {
284
+ if (active) {
285
+ return;
304
286
  }
305
- }
306
- invalidateDependencies() {
307
- var _a;
308
- (_a = this.current) == null ? void 0 : _a.invalidateDependencies();
309
- }
310
- }
311
- class Callable extends Function {
312
- constructor(_call) {
313
- super("...args", "return this._call(...args)");
314
- this._call = _call;
315
- return this.bind(this);
316
- }
317
- }
318
- function debounce(action, options) {
319
- const wait = typeof options === "object" && "wait" in options ? calcDuration(options.wait) : calcDuration(options);
320
- const maxWait = typeof options === "object" && "maxWait" in options && options.maxWait !== void 0 ? calcDuration(options.maxWait) : void 0;
321
- let run;
322
- let timeout;
323
- let timeoutStarted;
324
- function flush() {
325
- if (timeout !== void 0) {
326
- clearTimeout(timeout);
287
+ for (const dep of deps) {
288
+ if (!deepEqual(dep.store.get(), dep.value)) {
289
+ store.invalidate();
290
+ return;
291
+ }
327
292
  }
328
- run == null ? void 0 : run();
329
293
  }
330
- function cancel() {
331
- if (timeout !== void 0) {
332
- clearTimeout(timeout);
294
+ function stop() {
295
+ cancelEffect();
296
+ cancelConnection == null ? void 0 : cancelConnection();
297
+ if (cancelConnection) {
298
+ whenConnected.reject();
299
+ whenExecuted.reject();
300
+ } else {
301
+ whenConnected.resolve();
302
+ whenExecuted.resolve();
333
303
  }
334
- run = void 0;
335
- timeout = void 0;
336
- timeoutStarted = void 0;
337
304
  }
338
- function isScheduled() {
339
- return timeout !== void 0;
340
- }
341
- function debounce2(...args) {
342
- const now = Date.now();
343
- timeoutStarted ?? (timeoutStarted = now);
344
- const deadline = Math.min(
345
- //
346
- now + wait,
347
- timeoutStarted + (maxWait ?? Number.POSITIVE_INFINITY)
348
- );
349
- if (timeout !== void 0) {
350
- clearTimeout(timeout);
305
+ function invalidateDependencies(recursive) {
306
+ for (const dep of deps) {
307
+ dep.store.invalidate(recursive);
351
308
  }
352
- run = () => {
353
- run = void 0;
354
- timeout = void 0;
355
- timeoutStarted = void 0;
356
- action(...args);
357
- };
358
- timeout = setTimeout(run, deadline - now);
359
309
  }
360
- return Object.assign(debounce2, { flush, cancel, isScheduled });
310
+ return {
311
+ get value() {
312
+ return value;
313
+ },
314
+ check,
315
+ stop,
316
+ invalidateDependencies
317
+ };
361
318
  }
362
- function forwardError(error) {
363
- setTimeout(() => {
364
- throw error;
365
- });
319
+ function staticValue(value) {
320
+ return {
321
+ value,
322
+ check: () => void 0,
323
+ stop: () => void 0,
324
+ invalidateDependencies: () => void 0
325
+ };
366
326
  }
367
327
  function flatClone(object) {
368
328
  if (object instanceof Map) {
@@ -468,6 +428,83 @@ function makeSelector(selector) {
468
428
  }
469
429
  return (x) => get(x, selector);
470
430
  }
431
+ class Callable extends Function {
432
+ constructor(_call) {
433
+ super("...args", "return this._call(...args)");
434
+ this._call = _call;
435
+ return this.bind(this);
436
+ }
437
+ }
438
+ function debounce(action, options) {
439
+ const wait = typeof options === "object" && "wait" in options ? calcDuration(options.wait) : calcDuration(options);
440
+ const maxWait = typeof options === "object" && "maxWait" in options && options.maxWait !== void 0 ? calcDuration(options.maxWait) : void 0;
441
+ let run;
442
+ let timeout;
443
+ let timeoutStarted;
444
+ function flush() {
445
+ if (timeout !== void 0) {
446
+ clearTimeout(timeout);
447
+ }
448
+ run == null ? void 0 : run();
449
+ }
450
+ function cancel() {
451
+ if (timeout !== void 0) {
452
+ clearTimeout(timeout);
453
+ }
454
+ run = void 0;
455
+ timeout = void 0;
456
+ timeoutStarted = void 0;
457
+ }
458
+ function isScheduled() {
459
+ return timeout !== void 0;
460
+ }
461
+ function debounce2(...args) {
462
+ const now = Date.now();
463
+ timeoutStarted ?? (timeoutStarted = now);
464
+ const deadline = Math.min(
465
+ //
466
+ now + wait,
467
+ timeoutStarted + (maxWait ?? Number.POSITIVE_INFINITY)
468
+ );
469
+ if (timeout !== void 0) {
470
+ clearTimeout(timeout);
471
+ }
472
+ run = () => {
473
+ run = void 0;
474
+ timeout = void 0;
475
+ timeoutStarted = void 0;
476
+ action(...args);
477
+ };
478
+ timeout = setTimeout(run, deadline - now);
479
+ }
480
+ return Object.assign(debounce2, { flush, cancel, isScheduled });
481
+ }
482
+ function forwardError(error) {
483
+ setTimeout(() => {
484
+ throw error;
485
+ });
486
+ }
487
+ class PromiseCancelError extends Error {
488
+ constructor() {
489
+ super("cancelled");
490
+ }
491
+ }
492
+ class PromiseWithCancel extends Promise {
493
+ constructor(executor) {
494
+ autobind(PromiseWithCancel);
495
+ const abortController = new AbortController();
496
+ super((resolve, reject) => {
497
+ executor(resolve, reject, abortController.signal);
498
+ abortController.signal.addEventListener("abort", (e) => {
499
+ reject(abortController.signal.reason);
500
+ });
501
+ });
502
+ this.abortController = abortController;
503
+ }
504
+ cancel(reason = new PromiseCancelError()) {
505
+ this.abortController.abort(reason);
506
+ }
507
+ }
471
508
  function createArrayAction(prop) {
472
509
  return function arrayAction(...args) {
473
510
  const newArray = this.get().slice();
@@ -541,26 +578,11 @@ function throttle(action, duration) {
541
578
  }, dt);
542
579
  };
543
580
  }
544
- class PromiseCancelError extends Error {
545
- constructor() {
546
- super("cancelled");
547
- }
548
- }
549
- class PromiseWithCancel extends Promise {
550
- constructor(executor) {
551
- autobind(PromiseWithCancel);
552
- const abortController = new AbortController();
553
- super((resolve, reject) => {
554
- executor(resolve, reject, abortController.signal);
555
- abortController.signal.addEventListener("abort", (e) => {
556
- reject(abortController.signal.reason);
557
- });
558
- });
559
- this.abortController = abortController;
560
- }
561
- cancel(reason = new PromiseCancelError()) {
562
- this.abortController.abort(reason);
563
- }
581
+ function disposable(dispose) {
582
+ return Object.assign(
583
+ dispose,
584
+ Symbol.dispose ? { [Symbol.dispose]: dispose } : {}
585
+ );
564
586
  }
565
587
  function noop() {
566
588
  return void 0;
@@ -575,35 +597,18 @@ class Store extends Callable {
575
597
  this.listeners = /* @__PURE__ */ new Map();
576
598
  this.effects = /* @__PURE__ */ new Map();
577
599
  this.notifyId = {};
578
- this.calculationHelper = new CalculationHelper({
579
- calculate: (helpers) => {
580
- if (this.getter instanceof Function) {
581
- const value = this.getter.apply(helpers, [helpers]);
582
- this._value = { v: value };
583
- this.notify();
584
- }
585
- },
586
- addEffect: (effect) => this.addEffect(effect, this.options.retain),
587
- getValue: () => {
588
- var _a;
589
- return (_a = this._value) == null ? void 0 : _a.v;
590
- },
591
- onInvalidate: () => this.reset()
592
- });
593
600
  autobind(Store);
594
- if (!(getter instanceof Function)) {
595
- this._value = { v: getter };
596
- }
597
601
  }
598
602
  get() {
599
- this.calculationHelper.check();
600
- if (!this._value) {
601
- this.calculationHelper.execute();
602
- return this.get();
603
+ var _a;
604
+ (_a = this.calculatedValue) == null ? void 0 : _a.check();
605
+ if (!this.calculatedValue) {
606
+ this.calculatedValue = calculatedValue(this, this.notify);
603
607
  }
604
- return this._value.v;
608
+ return this.calculatedValue.value;
605
609
  }
606
610
  set(...args) {
611
+ var _a;
607
612
  const path = args.length > 1 ? args[0] : [];
608
613
  let update = args.length > 1 ? args[1] : args[0];
609
614
  if (update instanceof Function) {
@@ -618,17 +623,20 @@ class Store extends Callable {
618
623
  this.derivedFrom.updater(update);
619
624
  return;
620
625
  }
621
- this._value = { v: update };
626
+ (_a = this.calculatedValue) == null ? void 0 : _a.stop();
627
+ this.calculatedValue = staticValue(update);
622
628
  this.notify();
623
629
  }
624
- reset() {
625
- this._value = void 0;
626
- if (this.isActive()) {
627
- this.calculationHelper.execute();
630
+ invalidate(recursive) {
631
+ var _a, _b;
632
+ if (recursive) {
633
+ (_a = this.calculatedValue) == null ? void 0 : _a.invalidateDependencies(recursive);
628
634
  }
635
+ (_b = this.calculatedValue) == null ? void 0 : _b.stop();
636
+ this.calculatedValue = void 0;
637
+ this.notify();
629
638
  }
630
639
  subscribe(listener, options) {
631
- var _a;
632
640
  const {
633
641
  passive,
634
642
  runNow = true,
@@ -636,22 +644,20 @@ class Store extends Callable {
636
644
  debounce: debounceOption,
637
645
  equals = deepEqual
638
646
  } = options ?? {};
639
- let compareToValue = (_a = this._value) == null ? void 0 : _a.v;
640
647
  let previousValue;
641
- let isInitializing = true;
642
- let innerListener = (force) => {
643
- if (!this._value) {
648
+ let innerListener = () => {
649
+ var _a;
650
+ const value = passive ? this.calculatedValue : { value: this.get() };
651
+ if (!value) {
644
652
  return;
645
653
  }
646
- const value = this._value.v;
647
- if (!force && (isInitializing || equals(value, compareToValue))) {
654
+ if (previousValue && equals(value.value, previousValue.value)) {
648
655
  return;
649
656
  }
650
- compareToValue = value;
651
- const _previousValue = previousValue;
652
- previousValue = value;
657
+ const _previousValue = previousValue == null ? void 0 : previousValue.value;
658
+ previousValue = this.calculatedValue && { value: (_a = this.calculatedValue) == null ? void 0 : _a.value };
653
659
  try {
654
- listener(value, _previousValue);
660
+ listener(value.value, _previousValue);
655
661
  } catch (error) {
656
662
  forwardError(error);
657
663
  }
@@ -666,15 +672,16 @@ class Store extends Callable {
666
672
  this.onSubscribe();
667
673
  }
668
674
  if (runNow) {
669
- innerListener(true);
675
+ innerListener();
676
+ } else {
677
+ previousValue = passive ? this.calculatedValue && { value: this.calculatedValue.value } : { value: this.get() };
670
678
  }
671
- isInitializing = false;
672
- return () => {
679
+ return disposable(() => {
673
680
  this.listeners.delete(innerListener);
674
681
  if (!passive) {
675
682
  this.onUnsubscribe();
676
683
  }
677
- };
684
+ });
678
685
  }
679
686
  once(...args) {
680
687
  const condition = args[0] instanceof Function ? args[0] : Boolean;
@@ -755,7 +762,7 @@ class Store extends Callable {
755
762
  * @returns
756
763
  * The effect can return a teardown callback, which will be executed when the last subscription is removed and potentially the ratain time has passed.
757
764
  */
758
- addEffect(effect, retain) {
765
+ addEffect(effect, retain = this.options.retain) {
759
766
  this.effects.set(effect, {
760
767
  handle: this.isActive() ? effect() ?? noop : void 0,
761
768
  retain: retain !== void 0 ? calcDuration(retain) : void 0
@@ -829,8 +836,8 @@ class Store extends Callable {
829
836
  }
830
837
  }
831
838
  }
832
- const defaultOptions = {};
833
839
  function create(initialState, options) {
840
+ options = { ...createStore.defaultOptions, ...options };
834
841
  const store = new Store(initialState, options);
835
842
  if (initialState instanceof Function) {
836
843
  return store;
@@ -850,23 +857,29 @@ function create(initialState, options) {
850
857
  );
851
858
  return Object.assign(store, boundMethods);
852
859
  }
853
- const createStore = /* @__PURE__ */ Object.assign(create, { defaultOptions });
860
+ const createStore = /* @__PURE__ */ Object.assign(create, {
861
+ defaultOptions: {
862
+ retain: { seconds: 1 }
863
+ }
864
+ });
854
865
  export {
866
+ PromiseWithState as P,
855
867
  Store as S,
856
868
  autobind as a,
857
- calcDuration as b,
869
+ calculatedValue as b,
858
870
  createStore as c,
859
- deepEqual as d,
860
- castArrayPath as e,
861
- shallowEqual as f,
862
- get as g,
863
- arrayMethods as h,
864
- mapMethods as i,
865
- recordMethods as j,
866
- setMethods as k,
867
- debounce as l,
871
+ calcDuration as d,
872
+ deepEqual as e,
873
+ castArrayPath as f,
874
+ shallowEqual as g,
875
+ get as h,
876
+ arrayMethods as i,
877
+ mapMethods as j,
878
+ recordMethods as k,
879
+ setMethods as l,
868
880
  makeSelector as m,
869
- join as n,
881
+ debounce as n,
882
+ join as o,
870
883
  queue as q,
871
884
  remove as r,
872
885
  set as s,