@converse/skeletor 3.1.2 → 3.1.4

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.
package/dist/skeletor.js CHANGED
@@ -8711,7 +8711,7 @@
8711
8711
  // eslint-disable-next-line @typescript-eslint/no-this-alias
8712
8712
  const that = this;
8713
8713
  async function localSync(method, model, options) {
8714
- let resp, errorMessage, promise, new_attributes;
8714
+ let resp, caughtError, promise, new_attributes;
8715
8715
  // We get the collection (and if necessary the model attribute.
8716
8716
  // Waiting for storeInitialized will cause another iteration of
8717
8717
  // the event loop, after which the collection reference will
@@ -8768,10 +8768,12 @@
8768
8768
  catch (error) {
8769
8769
  const storageSize = await that.getStorageSize();
8770
8770
  if (error.code === 22 && storageSize === 0) {
8771
- errorMessage = 'Private browsing is unsupported';
8771
+ caughtError = new Error('Private browsing is unsupported');
8772
8772
  }
8773
8773
  else {
8774
- errorMessage = error.message;
8774
+ // Preserve a real Error (stack/name); wrap anything else so the
8775
+ // error path always surfaces an Error, never a bare string.
8776
+ caughtError = error instanceof Error ? error : new Error(error?.message ?? String(error));
8775
8777
  }
8776
8778
  }
8777
8779
  if (resp) {
@@ -8785,10 +8787,21 @@
8785
8787
  options.success(data, options);
8786
8788
  }
8787
8789
  }
8790
+ else if (method === 'read' && !caughtError) {
8791
+ // A read that finds no record is a normal empty result (first run /
8792
+ // empty storage), not an error. Resolve with `null` so the callback
8793
+ // and promise forms of fetch() agree and a first-run read never
8794
+ // rejects.
8795
+ if (options && options.success) {
8796
+ options.success(null, options);
8797
+ }
8798
+ }
8788
8799
  else {
8789
- errorMessage = errorMessage ? errorMessage : 'Record Not Found';
8800
+ // A genuine failure (storage threw, or a write produced no result):
8801
+ // surface a real Error so consumers get a stack and `instanceof Error`
8802
+ // holds.
8790
8803
  if (options && options.error) {
8791
- options.error(errorMessage);
8804
+ options.error(caughtError ?? new Error('Record Not Found'));
8792
8805
  }
8793
8806
  }
8794
8807
  }
@@ -10900,18 +10913,18 @@
10900
10913
  this.initialize.apply(this, arguments);
10901
10914
  // Reset changed after initial set
10902
10915
  this.changed = {};
10903
- // When autoSync is on, `initialized` is always a Promise so callers can
10904
- // uniformly `await model.initialized`. It only hydrates from storage when
10916
+ // When autoSync is on, `hydrated` is always a Promise so callers can
10917
+ // uniformly `await model.hydrated`. It only hydrates from storage when
10905
10918
  // there's something to load (storage configured and the model isn't new);
10906
10919
  // otherwise it's already ready and resolves immediately.
10907
10920
  if (this.autoSync) {
10908
10921
  if (getStorage(this) && !this.isNew()) {
10909
10922
  __classPrivateFieldSet(this, _Model_state, 'hydrating', "f");
10910
- this.initialized = __classPrivateFieldGet(this, _Model_instances, "m", _Model_hydrate).call(this);
10923
+ this.hydrated = __classPrivateFieldGet(this, _Model_instances, "m", _Model_hydrate).call(this);
10911
10924
  }
10912
10925
  else {
10913
10926
  __classPrivateFieldSet(this, _Model_state, 'ready', "f");
10914
- this.initialized = Promise.resolve();
10927
+ this.hydrated = Promise.resolve();
10915
10928
  }
10916
10929
  }
10917
10930
  else {
@@ -11084,7 +11097,7 @@
11084
11097
  // Guard must come after arg normalization: when called as set(obj, opts),
11085
11098
  // fromStorage lives in `val` (second arg), not `options` (third arg).
11086
11099
  if (this.autoSync && __classPrivateFieldGet(this, _Model_state, "f") === 'hydrating' && !options.fromStorage) {
11087
- throw new Error(`Skeletor: set() called on an autoSync model before initialized resolved. Await model.initialized first.`);
11100
+ throw new Error(`Skeletor: set() called on an autoSync model before hydration resolved. Await model.hydrated first.`);
11088
11101
  }
11089
11102
  // Run validation.
11090
11103
  if (!this._validate(attrs, options))
@@ -11460,12 +11473,6 @@
11460
11473
  try {
11461
11474
  await this.fetch({ fromStorage: true, promise: true });
11462
11475
  }
11463
- catch (e) {
11464
- // 'Record Not Found' is a normal first-run state (nothing stored yet);
11465
- // keep the initial attributes and resolve. Re-throw anything else.
11466
- if (e !== 'Record Not Found')
11467
- throw e;
11468
- }
11469
11476
  finally {
11470
11477
  __classPrivateFieldSet(this, _Model_state, 'ready', "f");
11471
11478
  }
@@ -11530,11 +11537,11 @@
11530
11537
  if (models)
11531
11538
  this.reset(models, Object.assign({ silent: true }, options));
11532
11539
  this[Symbol.iterator] = this.values;
11533
- // When autoSync is on, `initialized` is always a Promise so callers can
11534
- // uniformly `await collection.initialized`; it resolves immediately when
11540
+ // When autoSync is on, `hydrated` is always a Promise so callers can
11541
+ // uniformly `await collection.hydrated`; it resolves immediately when
11535
11542
  // there's no storage to hydrate from.
11536
11543
  if (this.autoSync) {
11537
- this.initialized = getStorage(this) ? __classPrivateFieldGet(this, _Collection_instances, "m", _Collection_hydrate).call(this) : Promise.resolve();
11544
+ this.hydrated = getStorage(this) ? __classPrivateFieldGet(this, _Collection_instances, "m", _Collection_hydrate).call(this) : Promise.resolve();
11538
11545
  }
11539
11546
  }
11540
11547
  get comparator() {
@@ -11566,7 +11573,7 @@
11566
11573
  }
11567
11574
  /**
11568
11575
  * Override to return `true` to automatically load the collection from
11569
- * storage on construction. Await `initialized` to know when hydration
11576
+ * storage on construction. Await `hydrated` to know when hydration
11570
11577
  * is complete.
11571
11578
  *
11572
11579
  * Note: collection `autoSync` is hydrate-only. Unlike `Model`, it does
@@ -12240,8 +12247,9 @@
12240
12247
  }
12241
12248
  _Collection_instances = new WeakSet(), _Collection_hydrate = async function _Collection_hydrate() {
12242
12249
  ensureUnloadListener(PersistentStorage);
12243
- // findAll() returns [] for an empty store, so a first-run collection
12244
- // resolves rather than erroring the way a model fetch-by-id would.
12250
+ // An empty store is a normal first-run state: findAll() returns [] and a
12251
+ // missing model record resolves to null, so hydration resolves rather
12252
+ // than rejecting.
12245
12253
  await this.fetch({ fromStorage: true, promise: true });
12246
12254
  }, _a = Symbol.iterator;
12247
12255
  // This "enum" defines the three possible kinds of values which can be emitted