@ember-data/store 5.4.0-alpha.144 → 5.4.0-alpha.146

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/-private.js CHANGED
@@ -1 +1 @@
1
- export { A as ARRAY_SIGNAL, C as CacheHandler, j as CollectionRecordArray, I as LiveArray, M as MUTATE, R as RecordArrayManager, v as RelatedCollection, k as SOURCE, S as Store, q as StoreMap, _ as _clearCaches, u as _deprecatingNormalize, g as coerceId, f as constructResource, h as ensureStringId, l as fastPush, i as isStableIdentifier, w as log, x as logGroup, n as notifyArray, p as peekCache, r as recordIdentifierFor, m as removeRecordDataFor, t as setCacheFor, o as setRecordIdentifier, s as storeFor } from "./many-array-Ct9k0tSz.js";
1
+ export { A as ARRAY_SIGNAL, C as CacheHandler, j as CollectionRecordArray, I as LiveArray, M as MUTATE, R as RecordArrayManager, v as RelatedCollection, k as SOURCE, S as Store, q as StoreMap, _ as _clearCaches, u as _deprecatingNormalize, g as coerceId, f as constructResource, h as ensureStringId, l as fastPush, i as isStableIdentifier, w as log, x as logGroup, n as notifyArray, p as peekCache, r as recordIdentifierFor, m as removeRecordDataFor, t as setCacheFor, o as setRecordIdentifier, s as storeFor } from "./many-array-D-iDvWAq.js";
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export { C as CacheHandler, S as default, r as recordIdentifierFor, c as setIdentifierForgetMethod, a as setIdentifierGenerationMethod, d as setIdentifierResetMethod, b as setIdentifierUpdateMethod, e as setKeyInfoForResource, s as storeFor } from "./many-array-Ct9k0tSz.js";
1
+ export { C as CacheHandler, S as default, r as recordIdentifierFor, c as setIdentifierForgetMethod, a as setIdentifierGenerationMethod, d as setIdentifierResetMethod, b as setIdentifierUpdateMethod, e as setKeyInfoForResource, s as storeFor } from "./many-array-D-iDvWAq.js";
2
2
  import '@ember/debug';
3
3
  import '@embroider/macros';
4
4
  import '@ember-data/request-utils/string';
@@ -5,7 +5,7 @@ import { EnableHydration, SkipCache } from '@warp-drive/core-types/request';
5
5
  import { getOrSetGlobal, peekTransient, setTransient } from '@warp-drive/core-types/-private';
6
6
  import { _backburner } from '@ember/runloop';
7
7
  import { defineSignal, createSignal, subscribe, createArrayTags, addToTransaction, addTransactionCB } from '@ember-data/tracking/-private';
8
- import { CACHE_OWNER, DEBUG_STALE_CACHE_OWNER, DEBUG_CLIENT_ORIGINATED, DEBUG_IDENTIFIER_BUCKET } from '@warp-drive/core-types/identifier';
8
+ import { CACHE_OWNER, DEBUG_STALE_CACHE_OWNER, DEBUG_IDENTIFIER_BUCKET, DEBUG_CLIENT_ORIGINATED } from '@warp-drive/core-types/identifier';
9
9
  import { dasherize } from '@ember-data/request-utils/string';
10
10
  import { compat } from '@ember-data/tracking';
11
11
 
@@ -662,15 +662,13 @@ function makeStableRecordIdentifier(recordIdentifier, bucket, clientOriginated)
662
662
  // we enforce immutability in dev
663
663
  // but preserve our ability to do controlled updates to the reference
664
664
  let wrapper = {
665
- get lid() {
666
- return recordIdentifier.lid;
667
- },
665
+ type: recordIdentifier.type,
666
+ lid: recordIdentifier.lid,
668
667
  get id() {
669
668
  return recordIdentifier.id;
670
- },
671
- get type() {
672
- return recordIdentifier.type;
673
- },
669
+ }
670
+ };
671
+ const proto = {
674
672
  get [CACHE_OWNER]() {
675
673
  return recordIdentifier[CACHE_OWNER];
676
674
  },
@@ -682,9 +680,15 @@ function makeStableRecordIdentifier(recordIdentifier, bucket, clientOriginated)
682
680
  },
683
681
  set [DEBUG_STALE_CACHE_OWNER](value) {
684
682
  recordIdentifier[DEBUG_STALE_CACHE_OWNER] = value;
683
+ },
684
+ get [DEBUG_CLIENT_ORIGINATED]() {
685
+ return clientOriginated;
686
+ },
687
+ get [DEBUG_IDENTIFIER_BUCKET]() {
688
+ return bucket;
685
689
  }
686
690
  };
687
- Object.defineProperty(wrapper, 'toString', {
691
+ Object.defineProperty(proto, 'toString', {
688
692
  enumerable: false,
689
693
  value: () => {
690
694
  const {
@@ -695,7 +699,7 @@ function makeStableRecordIdentifier(recordIdentifier, bucket, clientOriginated)
695
699
  return `${clientOriginated ? '[CLIENT_ORIGINATED] ' : ''}${String(type)}:${String(id)} (${lid})`;
696
700
  }
697
701
  });
698
- Object.defineProperty(wrapper, 'toJSON', {
702
+ Object.defineProperty(proto, 'toJSON', {
699
703
  enumerable: false,
700
704
  value: () => {
701
705
  const {
@@ -710,8 +714,7 @@ function makeStableRecordIdentifier(recordIdentifier, bucket, clientOriginated)
710
714
  };
711
715
  }
712
716
  });
713
- wrapper[DEBUG_CLIENT_ORIGINATED] = clientOriginated;
714
- wrapper[DEBUG_IDENTIFIER_BUCKET] = bucket;
717
+ Object.setPrototypeOf(wrapper, proto);
715
718
  DEBUG_MAP.set(wrapper, recordIdentifier);
716
719
  wrapper = freeze(wrapper);
717
720
  return wrapper;
@@ -2306,10 +2309,8 @@ class CacheManager {
2306
2309
  * @module @ember-data/store
2307
2310
  */
2308
2311
 
2309
- let tokenId = 0;
2310
- const CacheOperations = new Set(['added', 'removed', 'state', 'updated', 'invalidated']);
2311
2312
  function isCacheOperationValue(value) {
2312
- return CacheOperations.has(value);
2313
+ return value === 'added' || value === 'state' || value === 'updated' || value === 'removed' || value === 'invalidated';
2313
2314
  }
2314
2315
  function runLoopIsFlushing() {
2315
2316
  //@ts-expect-error
@@ -2320,8 +2321,16 @@ function count(label) {
2320
2321
  // eslint-disable-next-line
2321
2322
  globalThis.__WarpDriveMetricCountData[label] = (globalThis.__WarpDriveMetricCountData[label] || 0) + 1;
2322
2323
  }
2323
- function _unsubscribe(tokens, token, cache) {
2324
- const identifier = tokens.get(token);
2324
+ function asInternalToken(token) {
2325
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
2326
+ if (!test) {
2327
+ throw new Error(`Expected a token with a 'for' property`);
2328
+ }
2329
+ })(token && typeof token === 'function' && 'for' in token) : {};
2330
+ }
2331
+ function _unsubscribe(token, cache) {
2332
+ asInternalToken(token);
2333
+ const identifier = token.for;
2325
2334
  if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_NOTIFICATIONS)) {
2326
2335
  if (getGlobalConfig().WarpDrive.debug.LOG_NOTIFICATIONS || globalThis.getWarpDriveRuntimeConfig().debug.LOG_NOTIFICATIONS) {
2327
2336
  if (!identifier) {
@@ -2331,9 +2340,20 @@ function _unsubscribe(tokens, token, cache) {
2331
2340
  }
2332
2341
  }
2333
2342
  if (identifier) {
2334
- tokens.delete(token);
2335
- const map = cache.get(identifier);
2336
- map?.delete(token);
2343
+ const callbacks = cache.get(identifier);
2344
+ if (!callbacks) {
2345
+ return;
2346
+ }
2347
+ const index = callbacks.indexOf(token);
2348
+ if (index === -1) {
2349
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
2350
+ if (!test) {
2351
+ throw new Error(`Cannot unsubscribe a token that is not subscribed`);
2352
+ }
2353
+ })(index !== -1) : {};
2354
+ return;
2355
+ }
2356
+ callbacks.splice(index, 1);
2337
2357
  }
2338
2358
  }
2339
2359
 
@@ -2354,7 +2374,6 @@ class NotificationManager {
2354
2374
  this._buffered = new Map();
2355
2375
  this._hasFlush = false;
2356
2376
  this._cache = new Map();
2357
- this._tokens = new Map();
2358
2377
  }
2359
2378
 
2360
2379
  /**
@@ -2391,17 +2410,26 @@ class NotificationManager {
2391
2410
  throw new Error(`Expected to receive a stable Identifier to subscribe to`);
2392
2411
  }
2393
2412
  })(identifier === 'resource' || identifier === 'document' || isStableIdentifier(identifier) || isDocumentIdentifier(identifier)) : {};
2394
- let map = this._cache.get(identifier);
2395
- if (!map) {
2396
- map = new Map();
2397
- this._cache.set(identifier, map);
2413
+ let callbacks = this._cache.get(identifier);
2414
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
2415
+ if (!test) {
2416
+ throw new Error(`expected to receive a valid callback`);
2417
+ }
2418
+ })(typeof callback === 'function') : {};
2419
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
2420
+ if (!test) {
2421
+ throw new Error(`cannot subscribe with the same callback twice`);
2422
+ }
2423
+ })(!callbacks || !callbacks.includes(callback)) : {};
2424
+ // we use the callback as the cancellation token
2425
+ //@ts-expect-error
2426
+ callback.for = identifier;
2427
+ if (!callbacks) {
2428
+ callbacks = [];
2429
+ this._cache.set(identifier, callbacks);
2398
2430
  }
2399
- const unsubToken = macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? {
2400
- _tokenRef: tokenId++
2401
- } : {};
2402
- map.set(unsubToken, callback);
2403
- this._tokens.set(unsubToken, identifier);
2404
- return unsubToken;
2431
+ callbacks.push(callback);
2432
+ return callback;
2405
2433
  }
2406
2434
 
2407
2435
  /**
@@ -2413,7 +2441,7 @@ class NotificationManager {
2413
2441
  */
2414
2442
  unsubscribe(token) {
2415
2443
  if (!this.isDestroyed) {
2416
- _unsubscribe(this._tokens, token, this._cache);
2444
+ _unsubscribe(token, this._cache);
2417
2445
  }
2418
2446
  }
2419
2447
 
@@ -2443,7 +2471,7 @@ class NotificationManager {
2443
2471
  }
2444
2472
  return false;
2445
2473
  }
2446
- const hasSubscribers = Boolean(this._cache.get(identifier)?.size);
2474
+ const hasSubscribers = Boolean(this._cache.get(identifier)?.length);
2447
2475
  if (isCacheOperationValue(value) || hasSubscribers) {
2448
2476
  let buffer = this._buffered.get(identifier);
2449
2477
  if (!buffer) {
@@ -2518,11 +2546,11 @@ class NotificationManager {
2518
2546
  });
2519
2547
  }
2520
2548
  }
2521
- const callbackMap = this._cache.get(identifier);
2522
- if (!callbackMap || !callbackMap.size) {
2549
+ const callbacks = this._cache.get(identifier);
2550
+ if (!callbacks || !callbacks.length) {
2523
2551
  return false;
2524
2552
  }
2525
- callbackMap.forEach(cb => {
2553
+ callbacks.forEach(cb => {
2526
2554
  // @ts-expect-error overload doesn't narrow within body
2527
2555
  cb(identifier, value, key);
2528
2556
  });
@@ -2530,7 +2558,6 @@ class NotificationManager {
2530
2558
  }
2531
2559
  destroy() {
2532
2560
  this.isDestroyed = true;
2533
- this._tokens.clear();
2534
2561
  this._cache.clear();
2535
2562
  }
2536
2563
  }
@@ -2896,6 +2923,15 @@ class IdentifierArray {
2896
2923
  return Array.prototype;
2897
2924
  }
2898
2925
  });
2926
+ if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
2927
+ Object.defineProperty(this, '__SHOW_ME_THE_DATA_(debug mode only)__', {
2928
+ enumerable: false,
2929
+ configurable: true,
2930
+ get() {
2931
+ return proxy.slice();
2932
+ }
2933
+ });
2934
+ }
2899
2935
  createArrayTags(proxy, _SIGNAL);
2900
2936
  this[NOTIFY] = this[NOTIFY].bind(proxy);
2901
2937
  return proxy;
@@ -3675,16 +3711,108 @@ globalThis.setWarpDriveLogging = setLogging;
3675
3711
  globalThis.getWarpDriveRuntimeConfig = getRuntimeConfig;
3676
3712
  if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_METRIC_COUNTS)) {
3677
3713
  if (getGlobalConfig().WarpDrive.debug.LOG_METRIC_COUNTS || globalThis.getWarpDriveRuntimeConfig().debug.LOG_METRIC_COUNTS) {
3678
- // @ts-expect-error
3714
+ // @ts-expect-error adding to globalThis
3679
3715
  // eslint-disable-next-line
3680
3716
  globalThis.__WarpDriveMetricCountData = globalThis.__WarpDriveMetricCountData || {};
3681
3717
 
3682
- // @ts-expect-error
3718
+ // @ts-expect-error adding to globalThis
3683
3719
  globalThis.getWarpDriveMetricCounts = () => {
3684
3720
  // @ts-expect-error
3685
3721
  // eslint-disable-next-line
3686
- return globalThis.__WarpDriveMetricCountData;
3722
+ return structuredClone(globalThis.__WarpDriveMetricCountData);
3687
3723
  };
3724
+
3725
+ // @ts-expect-error adding to globalThis
3726
+ globalThis.resetWarpDriveMetricCounts = () => {
3727
+ // @ts-expect-error
3728
+ globalThis.__WarpDriveMetricCountData = {};
3729
+ };
3730
+ if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.__INTERNAL_LOG_NATIVE_MAP_SET_COUNTS)) {
3731
+ if (getGlobalConfig().WarpDrive.debug.__INTERNAL_LOG_NATIVE_MAP_SET_COUNTS || globalThis.getWarpDriveRuntimeConfig().debug.__INTERNAL_LOG_NATIVE_MAP_SET_COUNTS) {
3732
+ // @ts-expect-error adding to globalThis
3733
+ globalThis.__primitiveInstanceId = 0;
3734
+ function interceptAndLog(klassName, methodName) {
3735
+ const klass = globalThis[klassName];
3736
+ if (methodName === 'constructor') {
3737
+ const instantiationLabel = `new ${klassName}()`;
3738
+ // @ts-expect-error
3739
+ globalThis[klassName] = class extends klass {
3740
+ // @ts-expect-error
3741
+ constructor(...args) {
3742
+ // eslint-disable-next-line
3743
+ super(...args);
3744
+ // @ts-expect-error
3745
+
3746
+ const instanceId = globalThis.__primitiveInstanceId++;
3747
+ // @ts-expect-error
3748
+ // eslint-disable-next-line
3749
+ globalThis.__WarpDriveMetricCountData[instantiationLabel] =
3750
+ // @ts-expect-error
3751
+ // eslint-disable-next-line
3752
+ (globalThis.__WarpDriveMetricCountData[instantiationLabel] || 0) + 1;
3753
+ // @ts-expect-error
3754
+ this.instanceName = `${klassName}:${instanceId} - ${new Error().stack?.split('\n')[2]}`;
3755
+ }
3756
+ };
3757
+ } else {
3758
+ // @ts-expect-error
3759
+ // eslint-disable-next-line
3760
+ const original = klass.prototype[methodName];
3761
+ const logName = `${klassName}.${methodName}`;
3762
+
3763
+ // @ts-expect-error
3764
+ klass.prototype[methodName] = function (...args) {
3765
+ // @ts-expect-error
3766
+ // eslint-disable-next-line
3767
+ globalThis.__WarpDriveMetricCountData[logName] = (globalThis.__WarpDriveMetricCountData[logName] || 0) + 1;
3768
+ // @ts-expect-error
3769
+ const {
3770
+ instanceName
3771
+ } = this;
3772
+ if (!instanceName) {
3773
+ // @ts-expect-error
3774
+ const instanceId = globalThis.__primitiveInstanceId++;
3775
+ // @ts-expect-error
3776
+ this.instanceName = `${klassName}.${methodName}:${instanceId} - ${new Error().stack?.split('\n')[2]}`;
3777
+ }
3778
+ const instanceLogName = `${logName} (${instanceName})`;
3779
+ // @ts-expect-error
3780
+ // eslint-disable-next-line
3781
+ globalThis.__WarpDriveMetricCountData[instanceLogName] =
3782
+ // @ts-expect-error
3783
+ // eslint-disable-next-line
3784
+ (globalThis.__WarpDriveMetricCountData[instanceLogName] || 0) + 1;
3785
+ // eslint-disable-next-line
3786
+ return original.apply(this, args);
3787
+ };
3788
+ }
3789
+ }
3790
+ interceptAndLog('Set', 'constructor');
3791
+ interceptAndLog('Set', 'add');
3792
+ interceptAndLog('Set', 'delete');
3793
+ interceptAndLog('Set', 'has');
3794
+ interceptAndLog('Set', 'set');
3795
+ interceptAndLog('Set', 'get');
3796
+ interceptAndLog('Map', 'constructor');
3797
+ interceptAndLog('Map', 'set');
3798
+ interceptAndLog('Map', 'delete');
3799
+ interceptAndLog('Map', 'has');
3800
+ interceptAndLog('Map', 'add');
3801
+ interceptAndLog('Map', 'get');
3802
+ interceptAndLog('WeakSet', 'constructor');
3803
+ interceptAndLog('WeakSet', 'add');
3804
+ interceptAndLog('WeakSet', 'delete');
3805
+ interceptAndLog('WeakSet', 'has');
3806
+ interceptAndLog('WeakSet', 'set');
3807
+ interceptAndLog('WeakSet', 'get');
3808
+ interceptAndLog('WeakMap', 'constructor');
3809
+ interceptAndLog('WeakMap', 'set');
3810
+ interceptAndLog('WeakMap', 'delete');
3811
+ interceptAndLog('WeakMap', 'has');
3812
+ interceptAndLog('WeakMap', 'add');
3813
+ interceptAndLog('WeakMap', 'get');
3814
+ }
3815
+ }
3688
3816
  }
3689
3817
  }
3690
3818