@osdk/client 2.5.0-beta.5 → 2.5.0-beta.7

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 (211) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/build/browser/MinimalClientContext.js.map +1 -1
  3. package/build/browser/actions/applyAction.js +4 -0
  4. package/build/browser/actions/applyAction.js.map +1 -1
  5. package/build/browser/createClient.js +2 -1
  6. package/build/browser/createClient.js.map +1 -1
  7. package/build/browser/createMinimalClient.js +2 -1
  8. package/build/browser/createMinimalClient.js.map +1 -1
  9. package/build/browser/object/aggregate.js +2 -0
  10. package/build/browser/object/aggregate.js.map +1 -1
  11. package/build/browser/object/fetchPage.js +5 -2
  12. package/build/browser/object/fetchPage.js.map +1 -1
  13. package/build/browser/observable/internal/AbstractHelper.js +8 -4
  14. package/build/browser/observable/internal/AbstractHelper.js.map +1 -1
  15. package/build/browser/observable/internal/BatchContext.js +2 -0
  16. package/build/browser/observable/internal/BatchContext.js.map +1 -0
  17. package/build/browser/observable/internal/CacheKeys.js +61 -36
  18. package/build/browser/observable/internal/CacheKeys.js.map +1 -1
  19. package/build/browser/observable/internal/Layer.js +0 -8
  20. package/build/browser/observable/internal/Layer.js.map +1 -1
  21. package/build/browser/observable/internal/Layers.js +151 -0
  22. package/build/browser/observable/internal/Layers.js.map +1 -0
  23. package/build/browser/observable/internal/ObservableClientImpl.js +3 -1
  24. package/build/browser/observable/internal/ObservableClientImpl.js.map +1 -1
  25. package/build/browser/observable/internal/Queries.js +40 -0
  26. package/build/browser/observable/internal/Queries.js.map +1 -0
  27. package/build/browser/observable/internal/Query.js +30 -3
  28. package/build/browser/observable/internal/Query.js.map +1 -1
  29. package/build/browser/observable/internal/QuerySubscription.js +7 -0
  30. package/build/browser/observable/internal/QuerySubscription.js.map +1 -1
  31. package/build/browser/observable/internal/Store.js +36 -225
  32. package/build/browser/observable/internal/Store.js.map +1 -1
  33. package/build/browser/observable/internal/Store.test.js +11 -9
  34. package/build/browser/observable/internal/Store.test.js.map +1 -1
  35. package/build/browser/observable/internal/SubjectPayload.js +2 -0
  36. package/build/browser/observable/internal/SubjectPayload.js.map +1 -0
  37. package/build/browser/observable/internal/Subjects.js +55 -0
  38. package/build/browser/observable/internal/Subjects.js.map +1 -0
  39. package/build/browser/observable/internal/actions/ActionApplication.js +2 -2
  40. package/build/browser/observable/internal/actions/ActionApplication.js.map +1 -1
  41. package/build/browser/observable/internal/actions/OptimisticJob.js +1 -1
  42. package/build/browser/observable/internal/actions/OptimisticJob.js.map +1 -1
  43. package/build/browser/observable/internal/base-list/BaseListQuery.js +6 -9
  44. package/build/browser/observable/internal/base-list/BaseListQuery.js.map +1 -1
  45. package/build/browser/observable/internal/base-list/createCollectionConnectable.js +2 -2
  46. package/build/browser/observable/internal/base-list/createCollectionConnectable.js.map +1 -1
  47. package/build/browser/observable/internal/base-list/createCollectionConnectable.test.js +23 -25
  48. package/build/browser/observable/internal/base-list/createCollectionConnectable.test.js.map +1 -1
  49. package/build/browser/observable/internal/base-list/removeDuplicates.js.map +1 -1
  50. package/build/browser/observable/internal/createInitEntry.js +25 -0
  51. package/build/browser/observable/internal/createInitEntry.js.map +1 -0
  52. package/build/browser/observable/internal/links/LinksHelper.js +5 -5
  53. package/build/browser/observable/internal/links/LinksHelper.js.map +1 -1
  54. package/build/browser/observable/internal/links/SpecificLinkQuery.js.map +1 -1
  55. package/build/browser/observable/internal/list/ListQuery.js +5 -5
  56. package/build/browser/observable/internal/list/ListQuery.js.map +1 -1
  57. package/build/browser/observable/internal/list/ListsHelper.js +5 -5
  58. package/build/browser/observable/internal/list/ListsHelper.js.map +1 -1
  59. package/build/browser/observable/internal/object/ObjectQuery.js.map +1 -1
  60. package/build/browser/observable/internal/object/ObjectsHelper.js +2 -2
  61. package/build/browser/observable/internal/object/ObjectsHelper.js.map +1 -1
  62. package/build/browser/observable/internal/sorting/SortingStrategy.js.map +1 -1
  63. package/build/browser/observable/internal/testUtils/invalidateList.js +2 -2
  64. package/build/browser/observable/internal/testUtils/invalidateList.js.map +1 -1
  65. package/build/browser/observable/internal/testUtils.js +1 -1
  66. package/build/browser/observable/internal/testUtils.js.map +1 -1
  67. package/build/browser/ontology/loadActionMetadata.js +3 -1
  68. package/build/browser/ontology/loadActionMetadata.js.map +1 -1
  69. package/build/browser/ontology/loadFullObjectMetadata.js +2 -1
  70. package/build/browser/ontology/loadFullObjectMetadata.js.map +1 -1
  71. package/build/browser/ontology/loadInterfaceMetadata.js +2 -1
  72. package/build/browser/ontology/loadInterfaceMetadata.js.map +1 -1
  73. package/build/browser/queries/applyQuery.js +2 -2
  74. package/build/browser/queries/applyQuery.js.map +1 -1
  75. package/build/browser/util/UserAgent.js +2 -2
  76. package/build/cjs/{chunk-ELAA4C2C.cjs → chunk-5KDG5ZET.cjs} +9 -4
  77. package/build/cjs/chunk-5KDG5ZET.cjs.map +1 -0
  78. package/build/cjs/{chunk-KCMGIMVC.cjs → chunk-BY2PQEYA.cjs} +70 -60
  79. package/build/cjs/chunk-BY2PQEYA.cjs.map +1 -0
  80. package/build/cjs/{createClient-BJo8T7Js.d.cts → createClient-mOlFts15.d.cts} +1 -0
  81. package/build/cjs/index.cjs +7 -7
  82. package/build/cjs/index.d.cts +1 -1
  83. package/build/cjs/public/internal.cjs +8 -8
  84. package/build/cjs/public/unstable-do-not-use.cjs +396 -302
  85. package/build/cjs/public/unstable-do-not-use.cjs.map +1 -1
  86. package/build/cjs/public/unstable-do-not-use.d.cts +1 -1
  87. package/build/esm/MinimalClientContext.js.map +1 -1
  88. package/build/esm/actions/applyAction.js +4 -0
  89. package/build/esm/actions/applyAction.js.map +1 -1
  90. package/build/esm/createClient.js +2 -1
  91. package/build/esm/createClient.js.map +1 -1
  92. package/build/esm/createMinimalClient.js +2 -1
  93. package/build/esm/createMinimalClient.js.map +1 -1
  94. package/build/esm/object/aggregate.js +2 -0
  95. package/build/esm/object/aggregate.js.map +1 -1
  96. package/build/esm/object/fetchPage.js +5 -2
  97. package/build/esm/object/fetchPage.js.map +1 -1
  98. package/build/esm/observable/internal/AbstractHelper.js +8 -4
  99. package/build/esm/observable/internal/AbstractHelper.js.map +1 -1
  100. package/build/esm/observable/internal/BatchContext.js +2 -0
  101. package/build/esm/observable/internal/BatchContext.js.map +1 -0
  102. package/build/esm/observable/internal/CacheKeys.js +61 -36
  103. package/build/esm/observable/internal/CacheKeys.js.map +1 -1
  104. package/build/esm/observable/internal/Layer.js +0 -8
  105. package/build/esm/observable/internal/Layer.js.map +1 -1
  106. package/build/esm/observable/internal/Layers.js +151 -0
  107. package/build/esm/observable/internal/Layers.js.map +1 -0
  108. package/build/esm/observable/internal/ObservableClientImpl.js +3 -1
  109. package/build/esm/observable/internal/ObservableClientImpl.js.map +1 -1
  110. package/build/esm/observable/internal/Queries.js +40 -0
  111. package/build/esm/observable/internal/Queries.js.map +1 -0
  112. package/build/esm/observable/internal/Query.js +30 -3
  113. package/build/esm/observable/internal/Query.js.map +1 -1
  114. package/build/esm/observable/internal/QuerySubscription.js +7 -0
  115. package/build/esm/observable/internal/QuerySubscription.js.map +1 -1
  116. package/build/esm/observable/internal/Store.js +36 -225
  117. package/build/esm/observable/internal/Store.js.map +1 -1
  118. package/build/esm/observable/internal/Store.test.js +11 -9
  119. package/build/esm/observable/internal/Store.test.js.map +1 -1
  120. package/build/esm/observable/internal/SubjectPayload.js +2 -0
  121. package/build/esm/observable/internal/SubjectPayload.js.map +1 -0
  122. package/build/esm/observable/internal/Subjects.js +55 -0
  123. package/build/esm/observable/internal/Subjects.js.map +1 -0
  124. package/build/esm/observable/internal/actions/ActionApplication.js +2 -2
  125. package/build/esm/observable/internal/actions/ActionApplication.js.map +1 -1
  126. package/build/esm/observable/internal/actions/OptimisticJob.js +1 -1
  127. package/build/esm/observable/internal/actions/OptimisticJob.js.map +1 -1
  128. package/build/esm/observable/internal/base-list/BaseListQuery.js +6 -9
  129. package/build/esm/observable/internal/base-list/BaseListQuery.js.map +1 -1
  130. package/build/esm/observable/internal/base-list/createCollectionConnectable.js +2 -2
  131. package/build/esm/observable/internal/base-list/createCollectionConnectable.js.map +1 -1
  132. package/build/esm/observable/internal/base-list/createCollectionConnectable.test.js +23 -25
  133. package/build/esm/observable/internal/base-list/createCollectionConnectable.test.js.map +1 -1
  134. package/build/esm/observable/internal/base-list/removeDuplicates.js.map +1 -1
  135. package/build/esm/observable/internal/createInitEntry.js +25 -0
  136. package/build/esm/observable/internal/createInitEntry.js.map +1 -0
  137. package/build/esm/observable/internal/links/LinksHelper.js +5 -5
  138. package/build/esm/observable/internal/links/LinksHelper.js.map +1 -1
  139. package/build/esm/observable/internal/links/SpecificLinkQuery.js.map +1 -1
  140. package/build/esm/observable/internal/list/ListQuery.js +5 -5
  141. package/build/esm/observable/internal/list/ListQuery.js.map +1 -1
  142. package/build/esm/observable/internal/list/ListsHelper.js +5 -5
  143. package/build/esm/observable/internal/list/ListsHelper.js.map +1 -1
  144. package/build/esm/observable/internal/object/ObjectQuery.js.map +1 -1
  145. package/build/esm/observable/internal/object/ObjectsHelper.js +2 -2
  146. package/build/esm/observable/internal/object/ObjectsHelper.js.map +1 -1
  147. package/build/esm/observable/internal/sorting/SortingStrategy.js.map +1 -1
  148. package/build/esm/observable/internal/testUtils/invalidateList.js +2 -2
  149. package/build/esm/observable/internal/testUtils/invalidateList.js.map +1 -1
  150. package/build/esm/observable/internal/testUtils.js +1 -1
  151. package/build/esm/observable/internal/testUtils.js.map +1 -1
  152. package/build/esm/ontology/loadActionMetadata.js +3 -1
  153. package/build/esm/ontology/loadActionMetadata.js.map +1 -1
  154. package/build/esm/ontology/loadFullObjectMetadata.js +2 -1
  155. package/build/esm/ontology/loadFullObjectMetadata.js.map +1 -1
  156. package/build/esm/ontology/loadInterfaceMetadata.js +2 -1
  157. package/build/esm/ontology/loadInterfaceMetadata.js.map +1 -1
  158. package/build/esm/queries/applyQuery.js +2 -2
  159. package/build/esm/queries/applyQuery.js.map +1 -1
  160. package/build/esm/util/UserAgent.js +2 -2
  161. package/build/types/MinimalClientContext.d.ts +1 -0
  162. package/build/types/MinimalClientContext.d.ts.map +1 -1
  163. package/build/types/actions/applyAction.d.ts.map +1 -1
  164. package/build/types/createClient.d.ts +1 -0
  165. package/build/types/createClient.d.ts.map +1 -1
  166. package/build/types/observable/internal/AbstractHelper.d.ts +3 -1
  167. package/build/types/observable/internal/AbstractHelper.d.ts.map +1 -1
  168. package/build/types/observable/internal/BatchContext.d.ts +11 -0
  169. package/build/types/observable/internal/BatchContext.d.ts.map +1 -0
  170. package/build/types/observable/internal/CacheKeys.d.ts +9 -9
  171. package/build/types/observable/internal/CacheKeys.d.ts.map +1 -1
  172. package/build/types/observable/internal/Layer.d.ts +1 -2
  173. package/build/types/observable/internal/Layer.d.ts.map +1 -1
  174. package/build/types/observable/internal/Layers.d.ts +26 -0
  175. package/build/types/observable/internal/Layers.d.ts.map +1 -0
  176. package/build/types/observable/internal/Queries.d.ts +9 -0
  177. package/build/types/observable/internal/Queries.d.ts.map +1 -0
  178. package/build/types/observable/internal/Query.d.ts +17 -1
  179. package/build/types/observable/internal/Query.d.ts.map +1 -1
  180. package/build/types/observable/internal/Store.d.ts +19 -34
  181. package/build/types/observable/internal/Store.d.ts.map +1 -1
  182. package/build/types/observable/internal/Store.test.d.ts.map +1 -1
  183. package/build/types/observable/internal/SubjectPayload.d.ts +5 -0
  184. package/build/types/observable/internal/SubjectPayload.d.ts.map +1 -0
  185. package/build/types/observable/internal/Subjects.d.ts +16 -0
  186. package/build/types/observable/internal/Subjects.d.ts.map +1 -0
  187. package/build/types/observable/internal/base-list/BaseListQuery.d.ts +2 -1
  188. package/build/types/observable/internal/base-list/BaseListQuery.d.ts.map +1 -1
  189. package/build/types/observable/internal/base-list/createCollectionConnectable.d.ts +3 -2
  190. package/build/types/observable/internal/base-list/createCollectionConnectable.d.ts.map +1 -1
  191. package/build/types/observable/internal/base-list/removeDuplicates.d.ts +1 -1
  192. package/build/types/observable/internal/base-list/removeDuplicates.d.ts.map +1 -1
  193. package/build/types/observable/internal/createInitEntry.d.ts +3 -0
  194. package/build/types/observable/internal/createInitEntry.d.ts.map +1 -0
  195. package/build/types/observable/internal/links/LinksHelper.d.ts +3 -1
  196. package/build/types/observable/internal/links/LinksHelper.d.ts.map +1 -1
  197. package/build/types/observable/internal/links/SpecificLinkQuery.d.ts +3 -1
  198. package/build/types/observable/internal/links/SpecificLinkQuery.d.ts.map +1 -1
  199. package/build/types/observable/internal/list/ListQuery.d.ts +3 -1
  200. package/build/types/observable/internal/list/ListQuery.d.ts.map +1 -1
  201. package/build/types/observable/internal/list/ListsHelper.d.ts +3 -1
  202. package/build/types/observable/internal/list/ListsHelper.d.ts.map +1 -1
  203. package/build/types/observable/internal/object/ObjectQuery.d.ts +3 -1
  204. package/build/types/observable/internal/object/ObjectQuery.d.ts.map +1 -1
  205. package/build/types/observable/internal/object/ObjectsHelper.d.ts.map +1 -1
  206. package/build/types/observable/internal/sorting/SortingStrategy.d.ts +1 -1
  207. package/build/types/observable/internal/sorting/SortingStrategy.d.ts.map +1 -1
  208. package/build/types/observable/internal/testUtils.d.ts.map +1 -1
  209. package/package.json +9 -9
  210. package/build/cjs/chunk-ELAA4C2C.cjs.map +0 -1
  211. package/build/cjs/chunk-KCMGIMVC.cjs.map +0 -1
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var chunkKCMGIMVC_cjs = require('../chunk-KCMGIMVC.cjs');
4
- var chunkELAA4C2C_cjs = require('../chunk-ELAA4C2C.cjs');
3
+ var chunkBY2PQEYA_cjs = require('../chunk-BY2PQEYA.cjs');
4
+ var chunk5KDG5ZET_cjs = require('../chunk-5KDG5ZET.cjs');
5
5
  require('../chunk-Q7SFCCGT.cjs');
6
6
  var shared_net_fetch = require('@osdk/shared.net.fetch');
7
7
  var rxjs = require('rxjs');
@@ -35,7 +35,6 @@ var ObservableClientImpl = class {
35
35
  this.__experimentalStore = store;
36
36
  this.applyAction = store.applyAction.bind(store);
37
37
  this.validateAction = store.validateAction.bind(store);
38
- this.canonicalizeWhereClause = store.canonicalizeWhereClause.bind(store);
39
38
  }
40
39
  observeObject = (apiName, pk, options, subFn) => {
41
40
  return this.__experimentalStore.objects.observe(
@@ -76,6 +75,9 @@ var ObservableClientImpl = class {
76
75
  }
77
76
  return new UnsubscribableWrapper(parentSub);
78
77
  };
78
+ canonicalizeWhereClause(where) {
79
+ return this.__experimentalStore.whereCanonicalizer.canonicalize(where);
80
+ }
79
81
  };
80
82
 
81
83
  // src/observable/DebugFlags.ts
@@ -142,7 +144,7 @@ var OptimisticJob = class {
142
144
  return this;
143
145
  },
144
146
  createObject(type, pk, properties) {
145
- const create = store.client[chunkELAA4C2C_cjs.additionalContext].objectFactory2(store.client[chunkELAA4C2C_cjs.additionalContext], [{
147
+ const create = store.client[chunk5KDG5ZET_cjs.additionalContext].objectFactory2(store.client[chunk5KDG5ZET_cjs.additionalContext], [{
146
148
  $primaryKey: pk,
147
149
  $apiName: type.apiName,
148
150
  $objectType: type.apiName,
@@ -173,7 +175,7 @@ function runOptimisticJob(store, optimisticUpdate) {
173
175
  // we don't want to leak the result
174
176
  () => void 0
175
177
  ).finally(() => {
176
- store.removeLayer(optimisticId);
178
+ store.layers.remove(optimisticId);
177
179
  });
178
180
  };
179
181
  }
@@ -244,8 +246,8 @@ var ActionApplication = class {
244
246
  objectType,
245
247
  primaryKey
246
248
  } of deletedObjects ?? []) {
247
- const cacheKey = this.store.getCacheKey("object", objectType, primaryKey);
248
- this.store.peekQuery(cacheKey)?.deleteFromStore(
249
+ const cacheKey = this.store.cacheKeys.get("object", objectType, primaryKey);
250
+ this.store.queries.peek(cacheKey)?.deleteFromStore(
249
251
  "loaded",
250
252
  // this is probably not the best value to use
251
253
  batch
@@ -260,45 +262,110 @@ var ActionApplication = class {
260
262
  }
261
263
  };
262
264
  };
265
+
266
+ // src/observable/internal/RefCounts.ts
267
+ var RefCounts = class {
268
+ refCounts = /* @__PURE__ */ new Map();
269
+ // keeps our objects around for some extended duration after they are no longer
270
+ // needed which is good for quick clicks across tabs.
271
+ gcMap = /* @__PURE__ */ new Map();
272
+ constructor(keepAlive, cleanup) {
273
+ this.keepAlive = keepAlive;
274
+ this.cleanup = cleanup;
275
+ }
276
+ register(key) {
277
+ if (!this.refCounts.has(key)) {
278
+ this.gcMap.set(key, Date.now() + this.keepAlive);
279
+ }
280
+ return key;
281
+ }
282
+ retain(key) {
283
+ const count = this.refCounts.get(key) ?? 0;
284
+ this.refCounts.set(key, count + 1);
285
+ if (this.gcMap.has(key)) {
286
+ this.gcMap.delete(key);
287
+ }
288
+ }
289
+ release(key) {
290
+ const count = this.refCounts.get(key);
291
+ if (count === void 0) ; else if (count === 1) {
292
+ this.refCounts.delete(key);
293
+ this.gcMap.set(key, Date.now() + this.keepAlive);
294
+ } else {
295
+ this.refCounts.set(key, count - 1);
296
+ }
297
+ }
298
+ has(key) {
299
+ return this.refCounts.has(key);
300
+ }
301
+ gc() {
302
+ const now = Date.now();
303
+ for (const [key, deathTime] of this.gcMap) {
304
+ if (deathTime < now) {
305
+ this.gcMap.delete(key);
306
+ this.cleanup(key);
307
+ }
308
+ }
309
+ }
310
+ };
311
+
312
+ // src/observable/internal/CacheKeys.ts
263
313
  var CacheKeys = class {
264
314
  #cacheKeys = new trie.Trie(false, (keys) => {
265
- const ret = {
315
+ const cacheKey = {
266
316
  type: keys[0],
267
317
  otherKeys: keys.slice(1)
268
318
  };
269
- this.#onCreate(ret);
270
- return ret;
319
+ this.#onCreate?.(cacheKey);
320
+ return cacheKey;
271
321
  });
272
- #cacheKeyFactories = /* @__PURE__ */ new Map();
322
+ #refCounts = new RefCounts(6e4, (k) => this.#cleanupCacheKey(k));
323
+ // we are currently only using this for debug logging and should just remove it in the future if that
324
+ // continues to be true
325
+ #finalizationRegistry;
273
326
  #onCreate;
274
- constructor(whereCanonicalizer, orderByCanonicalizer, onCreate) {
327
+ #onDestroy;
328
+ constructor({
329
+ onCreate,
330
+ onDestroy
331
+ }) {
275
332
  this.#onCreate = onCreate;
276
- this.#registerCacheKeyFactory("object", (apiName, pk) => {
277
- if (process.env.NODE_ENV !== "production" && DEBUG_CACHE_KEYS) ;
278
- return this.#cacheKeys.lookupArray(["object", apiName, pk]);
279
- });
280
- this.#registerCacheKeyFactory("list", (type, apiName, where, orderBy) => {
281
- const cacheKeyArgs = ["list", type, apiName, whereCanonicalizer.canonicalize(where), orderByCanonicalizer.canonicalize(orderBy)];
282
- if (process.env.NODE_ENV !== "production" && DEBUG_CACHE_KEYS) ;
283
- return this.#cacheKeys.lookupArray(cacheKeyArgs);
284
- });
285
- this.#registerCacheKeyFactory("specificLink", (sourceObjectType, sourcePk, linkName, where, orderBy) => {
286
- const cacheKeyArgs = ["specificLink", sourceObjectType, sourcePk, linkName, whereCanonicalizer.canonicalize(where), orderByCanonicalizer.canonicalize(orderBy)];
287
- if (process.env.NODE_ENV !== "production" && DEBUG_CACHE_KEYS) ;
288
- return this.#cacheKeys.lookupArray(cacheKeyArgs);
333
+ this.#onDestroy = onDestroy;
334
+ setInterval(() => {
335
+ this.#refCounts.gc();
336
+ }, 1e3);
337
+ this.#finalizationRegistry = new FinalizationRegistry((cleanupCallback) => {
338
+ try {
339
+ cleanupCallback();
340
+ } catch (e) {
341
+ console.error("Caught an error while running a finalization callback", e);
342
+ }
289
343
  });
290
344
  }
291
- #registerCacheKeyFactory(type, factory) {
292
- this.#cacheKeyFactories.set(type, factory);
293
- }
294
345
  get(type, ...args) {
295
- const factory = this.#cacheKeyFactories.get(type);
296
- !factory ? process.env.NODE_ENV !== "production" ? invariant2__default.default(false, `no cache key factory for type "${type}"`) : invariant2__default.default(false) : void 0;
297
- return factory(...args);
346
+ const cacheKeyArgs = [type, ...args];
347
+ if (process.env.NODE_ENV !== "production" && DEBUG_CACHE_KEYS) ;
348
+ const cacheKey = this.#cacheKeys.lookupArray(cacheKeyArgs);
349
+ this.#refCounts.register(cacheKey);
350
+ return cacheKey;
351
+ }
352
+ retain(cacheKey) {
353
+ this.#refCounts.retain(cacheKey);
354
+ }
355
+ release(cacheKey) {
356
+ this.#refCounts.release(cacheKey);
298
357
  }
299
- remove(cacheKey) {
358
+ #remove(cacheKey) {
300
359
  this.#cacheKeys.remove(cacheKey.type, ...cacheKey.otherKeys);
301
360
  }
361
+ /**
362
+ * Called after a key is no longer retained and the timeout has elapsed
363
+ * @param key
364
+ */
365
+ #cleanupCacheKey = (key) => {
366
+ this.#onDestroy?.(key);
367
+ this.#remove(key);
368
+ };
302
369
  };
303
370
 
304
371
  // src/observable/internal/CacheKey.ts
@@ -368,6 +435,16 @@ function multimapHelper(multimap) {
368
435
  }));
369
436
  }
370
437
 
438
+ // src/observable/internal/createInitEntry.ts
439
+ function createInitEntry(cacheKey) {
440
+ return {
441
+ cacheKey,
442
+ status: "init",
443
+ value: void 0,
444
+ lastUpdated: 0
445
+ };
446
+ }
447
+
371
448
  // src/observable/internal/WeakMapWithEntries.ts
372
449
  var WeakMapWithEntries = class {
373
450
  #map = /* @__PURE__ */ new WeakMap();
@@ -515,29 +592,182 @@ var Layer = class _Layer {
515
592
  this.#cache.set(cacheKey, value);
516
593
  }
517
594
  };
518
- var Entry = class {
519
- constructor(cacheKey, value, lastUpdated, status = "init") {
520
- this.cacheKey = cacheKey;
521
- this.value = value;
522
- this.lastUpdated = lastUpdated;
523
- this.status = status;
595
+ var Subjects = class {
596
+ #layers;
597
+ // we can use a regular Map here because the refCounting will
598
+ // handle cleanup.
599
+ #cacheKeyToSubject = /* @__PURE__ */ new WeakMap();
600
+ constructor({
601
+ logger,
602
+ layers
603
+ }) {
604
+ this.logger = logger;
605
+ this.#layers = layers;
606
+ }
607
+ peek = (cacheKey) => {
608
+ return this.#cacheKeyToSubject.get(cacheKey);
609
+ };
610
+ get = (cacheKey) => {
611
+ let subject = this.#cacheKeyToSubject.get(cacheKey);
612
+ if (!subject) {
613
+ const initialValue = this.#layers.top.get(cacheKey) ?? createInitEntry(cacheKey);
614
+ subject = new rxjs.BehaviorSubject({
615
+ ...initialValue,
616
+ isOptimistic: initialValue.value !== this.#layers.truth.get(cacheKey)?.value
617
+ });
618
+ this.#cacheKeyToSubject.set(cacheKey, subject);
619
+ }
620
+ return subject;
621
+ };
622
+ delete = (cacheKey) => {
623
+ const subject = this.peek(cacheKey);
624
+ if (subject) {
625
+ subject.complete();
626
+ this.#cacheKeyToSubject.delete(cacheKey);
627
+ }
628
+ };
629
+ };
630
+
631
+ // src/observable/internal/tombstone.ts
632
+ var tombstone = void 0;
633
+
634
+ // src/observable/internal/Layers.ts
635
+ var Layers = class {
636
+ #truthLayer = new Layer(void 0, void 0);
637
+ #topLayer;
638
+ #onRevalidate;
639
+ constructor({
640
+ logger,
641
+ onRevalidate
642
+ }) {
643
+ this.logger = logger;
644
+ this.#topLayer = this.#truthLayer;
645
+ this.subjects = new Subjects({
646
+ logger,
647
+ layers: this
648
+ });
649
+ this.#onRevalidate = onRevalidate;
650
+ }
651
+ get top() {
652
+ return this.#topLayer;
653
+ }
654
+ get truth() {
655
+ return this.#truthLayer;
656
+ }
657
+ remove(layerId) {
658
+ !(layerId != null) ? process.env.NODE_ENV !== "production" ? invariant2__default.default(false, "undefined is the reserved layerId for the truth layer") : invariant2__default.default(false) : void 0;
659
+ let currentLayer = this.#topLayer;
660
+ const cacheKeys = /* @__PURE__ */ new Map();
661
+ while (currentLayer != null && currentLayer.parentLayer != null) {
662
+ if (currentLayer.layerId === layerId) {
663
+ for (const [k, v] of currentLayer.entries()) {
664
+ if (cacheKeys.has(k)) continue;
665
+ cacheKeys.set(k, v);
666
+ }
667
+ }
668
+ currentLayer = currentLayer.parentLayer;
669
+ }
670
+ this.#topLayer = this.#topLayer.removeLayer(layerId);
671
+ for (const [k, oldEntry] of cacheKeys) {
672
+ const currentEntry = this.#topLayer.get(k);
673
+ if (oldEntry !== currentEntry) {
674
+ const newEntry = currentEntry ?? createInitEntry(k);
675
+ this.subjects.peek(k)?.next({
676
+ ...newEntry,
677
+ isOptimistic: currentEntry?.value !== this.#truthLayer.get(k)?.value
678
+ });
679
+ }
680
+ }
681
+ }
682
+ batch({
683
+ optimisticId,
684
+ changes
685
+ }, batchFn) {
686
+ !(optimisticId === void 0 || !!optimisticId) ? process.env.NODE_ENV !== "production" ? invariant2__default.default(false, "optimistic must be undefined or not falsy") : invariant2__default.default(false) : void 0;
687
+ const batchContext = this.#createBatchContext({
688
+ optimisticId,
689
+ changes
690
+ });
691
+ const retVal = batchFn(batchContext);
692
+ this.#onRevalidate(changes, optimisticId).catch((e) => {
693
+ if (this.logger) {
694
+ this.logger.error("Unhandled error in batch", e);
695
+ } else {
696
+ console.error("Unhandled error in batch", e);
697
+ throw e;
698
+ }
699
+ });
700
+ return {
701
+ batchResult: batchContext,
702
+ retVal,
703
+ changes: batchContext.changes
704
+ };
705
+ }
706
+ #createBatchContext({
707
+ optimisticId,
708
+ changes
709
+ }) {
710
+ let needsLayer = optimisticId !== void 0;
711
+ const batchContext = {
712
+ changes,
713
+ createLayerIfNeeded: () => {
714
+ if (needsLayer) {
715
+ this.#topLayer = this.#topLayer.addLayer(optimisticId);
716
+ needsLayer = false;
717
+ }
718
+ },
719
+ optimisticWrite: !!optimisticId,
720
+ write: (cacheKey, value, status) => {
721
+ const oldTopValue = this.#topLayer.get(cacheKey);
722
+ if (optimisticId) batchContext.createLayerIfNeeded();
723
+ const writeLayer = optimisticId ? this.#topLayer : this.#truthLayer;
724
+ const newValue = {
725
+ cacheKey,
726
+ value,
727
+ lastUpdated: Date.now(),
728
+ status
729
+ };
730
+ writeLayer.set(cacheKey, newValue);
731
+ const newTopValue = this.#topLayer.get(cacheKey);
732
+ if (oldTopValue !== newTopValue) {
733
+ this.subjects.get(cacheKey)?.next({
734
+ ...newValue,
735
+ isOptimistic: newTopValue?.value !== this.#truthLayer.get(cacheKey)?.value
736
+ });
737
+ }
738
+ return newValue;
739
+ },
740
+ delete: (cacheKey, status) => {
741
+ return batchContext.write(cacheKey, tombstone, status);
742
+ },
743
+ read: (cacheKey) => {
744
+ return optimisticId ? this.#topLayer.get(cacheKey) : this.#truthLayer.get(cacheKey);
745
+ }
746
+ };
747
+ return batchContext;
524
748
  }
525
749
  };
526
750
 
527
751
  // src/observable/internal/QuerySubscription.ts
752
+ var subscriptionIdCounter = 0;
528
753
  var QuerySubscription = class extends UnsubscribableWrapper {
754
+ /** @internal */
529
755
  /** @internal */
530
756
  /** @internal */
531
757
  constructor(query, subscription) {
532
758
  super(subscription);
533
759
  this.query = query;
534
760
  this.subscription = subscription;
761
+ this.subscriptionId = `sub_${++subscriptionIdCounter}`;
535
762
  Object.defineProperties(this, {
536
763
  query: {
537
764
  enumerable: false
538
765
  },
539
766
  subscription: {
540
767
  enumerable: false
768
+ },
769
+ subscriptionId: {
770
+ enumerable: false
541
771
  }
542
772
  });
543
773
  }
@@ -545,15 +775,16 @@ var QuerySubscription = class extends UnsubscribableWrapper {
545
775
 
546
776
  // src/observable/internal/AbstractHelper.ts
547
777
  var AbstractHelper = class {
548
- constructor(store) {
778
+ constructor(store, cacheKeys) {
549
779
  this.store = store;
780
+ this.cacheKeys = cacheKeys;
550
781
  }
551
782
  observe(options, subFn) {
552
783
  const query = this.getQuery(options);
553
784
  return this._subscribe(query, options, subFn);
554
785
  }
555
786
  _subscribe(query, options, subFn) {
556
- this.store.retain(query.cacheKey);
787
+ this.store.cacheKeys.retain(query.cacheKey);
557
788
  if (options.mode !== "offline") {
558
789
  query.revalidate(options.mode === "force").catch((e) => {
559
790
  subFn.error(e);
@@ -565,10 +796,13 @@ var AbstractHelper = class {
565
796
  });
566
797
  }
567
798
  const sub = query.subscribe(subFn);
799
+ const querySub = new QuerySubscription(query, sub);
800
+ query.registerSubscriptionDedupeInterval(querySub.subscriptionId, options.dedupeInterval);
568
801
  sub.add(() => {
569
- this.store.release(query.cacheKey);
802
+ query.unregisterSubscriptionDedupeInterval(querySub.subscriptionId);
803
+ this.store.cacheKeys.release(query.cacheKey);
570
804
  });
571
- return new QuerySubscription(query, sub);
805
+ return querySub;
572
806
  }
573
807
  };
574
808
 
@@ -583,13 +817,15 @@ var Query = class {
583
817
  #connectable;
584
818
  #subscription;
585
819
  #subject;
820
+ #subscriptionDedupeIntervals = /* @__PURE__ */ new Map();
586
821
  /** @internal */
587
822
  constructor(store, observable, opts, cacheKey, logger) {
588
823
  this.options = opts;
589
824
  this.cacheKey = cacheKey;
590
825
  this.store = store;
826
+ this.cacheKeys = store.cacheKeys;
591
827
  this.#subject = observable;
592
- this.logger = logger ?? (process.env.NODE_ENV === "production" ? store.client[chunkELAA4C2C_cjs.additionalContext].logger : store.client[chunkELAA4C2C_cjs.additionalContext].logger?.child({}, {
828
+ this.logger = logger ?? (process.env.NODE_ENV === "production" ? store.client[chunk5KDG5ZET_cjs.additionalContext].logger : store.client[chunk5KDG5ZET_cjs.additionalContext].logger?.child({}, {
593
829
  msgPrefix: process.env.NODE_ENV !== "production" ? `Query<${cacheKey.type}, ${cacheKey.otherKeys.map((x) => JSON.stringify(x)).join(", ")}>` : "Query"
594
830
  }));
595
831
  }
@@ -598,6 +834,29 @@ var Query = class {
598
834
  this.#subscription = this.#connectable.connect();
599
835
  return this.#connectable.subscribe(observer);
600
836
  }
837
+ /**
838
+ * Register a subscription's dedupeInterval value
839
+ */
840
+ registerSubscriptionDedupeInterval(subscriptionId, dedupeInterval) {
841
+ if (dedupeInterval != null && dedupeInterval > 0) {
842
+ this.#subscriptionDedupeIntervals.set(subscriptionId, dedupeInterval);
843
+ }
844
+ }
845
+ /**
846
+ * Unregister a subscription's dedupeInterval value
847
+ */
848
+ unregisterSubscriptionDedupeInterval(subscriptionId) {
849
+ this.#subscriptionDedupeIntervals.delete(subscriptionId);
850
+ }
851
+ /**
852
+ * Get the minimum dedupeInterval from all active subscriptions
853
+ */
854
+ getMinimumDedupeInterval() {
855
+ if (this.#subscriptionDedupeIntervals.size === 0) {
856
+ return this.options.dedupeInterval ?? 0;
857
+ }
858
+ return Math.min(...this.#subscriptionDedupeIntervals.values());
859
+ }
601
860
  /**
602
861
  * Causes the query to revalidate. This will cause the query to fetch
603
862
  * the latest data from the server and update the store if it is deemed
@@ -620,7 +879,8 @@ var Query = class {
620
879
  await this.pendingFetch;
621
880
  return;
622
881
  }
623
- if ((this.options.dedupeInterval ?? 0) > 0 && this.lastFetchStarted != null && Date.now() - this.lastFetchStarted < (this.options.dedupeInterval ?? 0)) {
882
+ const minDedupeInterval = this.getMinimumDedupeInterval();
883
+ if (minDedupeInterval > 0 && this.lastFetchStarted != null && Date.now() - this.lastFetchStarted < minDedupeInterval) {
624
884
  if (process.env.NODE_ENV !== "production") {
625
885
  logger?.debug("Within dupeInterval, aborting revalidate");
626
886
  }
@@ -749,9 +1009,9 @@ function createOrderBySortFns(orderBy) {
749
1009
  };
750
1010
  });
751
1011
  }
752
- function createCollectionConnectable(subject, store, createPayload) {
1012
+ function createCollectionConnectable(subject, subjects, createPayload) {
753
1013
  return rxjs.connectable(subject.pipe(rxjs.switchMap((listEntry) => {
754
- const resolvedData = listEntry?.value?.data == null || listEntry.value.data.length === 0 ? rxjs.of([]) : rxjs.combineLatest(listEntry.value.data.map((cacheKey) => store.getSubject(cacheKey).pipe(rxjs.map((objectEntry) => objectEntry?.value), rxjs.distinctUntilChanged())));
1014
+ const resolvedData = listEntry?.value?.data == null || listEntry.value.data.length === 0 ? rxjs.of([]) : rxjs.combineLatest(listEntry.value.data.map((cacheKey) => subjects.get(cacheKey).pipe(rxjs.map((objectEntry) => objectEntry?.value), rxjs.distinctUntilChanged())));
755
1015
  return rxjs.scheduled(rxjs.combineLatest({
756
1016
  resolvedData,
757
1017
  isOptimistic: rxjs.of(listEntry.isOptimistic),
@@ -813,10 +1073,9 @@ var BaseListQuery = class extends Query {
813
1073
  */
814
1074
  _updateList(items, status, batch, append = false) {
815
1075
  if (process.env.NODE_ENV !== "production") {
816
- const logger = process.env.NODE_ENV !== "production" ? this.logger?.child({
1076
+ this.logger?.child({
817
1077
  methodName: "updateList"
818
- }) : this.logger;
819
- logger?.debug(`{status: ${status}, append: ${append}}`, JSON.stringify(items, null, 2));
1078
+ }).debug(`{status: ${status}, append: ${append}}`, JSON.stringify(items, null, 2));
820
1079
  }
821
1080
  let objectCacheKeys;
822
1081
  if (items.length === 0) {
@@ -884,11 +1143,11 @@ var BaseListQuery = class extends Query {
884
1143
  if (!batch.optimisticWrite) {
885
1144
  if (!append) {
886
1145
  for (const objectCacheKey of existingList?.value?.data ?? []) {
887
- this.store.release(objectCacheKey);
1146
+ this.store.cacheKeys.release(objectCacheKey);
888
1147
  }
889
1148
  }
890
1149
  for (const objectCacheKey of objectCacheKeys) {
891
- this.store.retain(objectCacheKey);
1150
+ this.store.cacheKeys.retain(objectCacheKey);
892
1151
  }
893
1152
  }
894
1153
  if (append) {
@@ -897,12 +1156,11 @@ var BaseListQuery = class extends Query {
897
1156
  return objectCacheKeys;
898
1157
  }
899
1158
  _dispose() {
900
- console.log("DISPOSE LIST QUERY");
901
1159
  this.store.batch({}, (batch) => {
902
1160
  const entry = batch.read(this.cacheKey);
903
1161
  if (entry) {
904
1162
  for (const objectCacheKey of entry.value?.data ?? []) {
905
- this.store.release(objectCacheKey);
1163
+ this.store.cacheKeys.release(objectCacheKey);
906
1164
  }
907
1165
  }
908
1166
  });
@@ -933,7 +1191,7 @@ var BaseListQuery = class extends Query {
933
1191
  * @returns A connectable observable of the collection's payload type
934
1192
  */
935
1193
  _createConnectable(subject) {
936
- return createCollectionConnectable(subject, this.store, (params) => this.createPayload(params));
1194
+ return createCollectionConnectable(subject, this.store.subjects, (params) => this.createPayload(params));
937
1195
  }
938
1196
  /**
939
1197
  * @override Reset pagination state before a fetch
@@ -1109,9 +1367,6 @@ var BaseListQuery = class extends Query {
1109
1367
  }
1110
1368
  };
1111
1369
 
1112
- // src/observable/internal/tombstone.ts
1113
- var tombstone = void 0;
1114
-
1115
1370
  // src/observable/internal/links/SpecificLinkQuery.ts
1116
1371
  var SpecificLinkQuery = class extends BaseListQuery {
1117
1372
  #sourceApiName;
@@ -1126,7 +1381,7 @@ var SpecificLinkQuery = class extends BaseListQuery {
1126
1381
  batch.changes.modified.add(this.cacheKey);
1127
1382
  }
1128
1383
  constructor(store, subject, cacheKey, opts) {
1129
- super(store, subject, opts, cacheKey, process.env.NODE_ENV !== "production" ? store.client[chunkELAA4C2C_cjs.additionalContext].logger?.child({}, {
1384
+ super(store, subject, opts, cacheKey, process.env.NODE_ENV !== "production" ? store.client[chunk5KDG5ZET_cjs.additionalContext].logger?.child({}, {
1130
1385
  msgPrefix: `SpecificLinkQuery<${cacheKey.otherKeys.map((x) => JSON.stringify(x)).join(", ")}>`
1131
1386
  }) : void 0);
1132
1387
  [this.#sourceApiName, this.#sourcePk, this.#linkName, this.#whereClause, this.#orderBy] = cacheKey.otherKeys;
@@ -1143,7 +1398,7 @@ var SpecificLinkQuery = class extends BaseListQuery {
1143
1398
  type: "object",
1144
1399
  apiName: this.#sourceApiName
1145
1400
  };
1146
- const sourceMetadata = await client[chunkELAA4C2C_cjs.additionalContext].ontologyProvider.getObjectDefinition(this.#sourceApiName);
1401
+ const sourceMetadata = await client[chunk5KDG5ZET_cjs.additionalContext].ontologyProvider.getObjectDefinition(this.#sourceApiName);
1147
1402
  const sourceQuery = client(sourceObjectDef).where({
1148
1403
  [sourceMetadata.primaryKeyApiName]: this.#sourcePk
1149
1404
  });
@@ -1207,7 +1462,7 @@ var SpecificLinkQuery = class extends BaseListQuery {
1207
1462
  return this.revalidate(true);
1208
1463
  } else {
1209
1464
  return (async () => {
1210
- const sourceMetadata = await this.store.client[chunkELAA4C2C_cjs.additionalContext].ontologyProvider.getObjectDefinition(this.#sourceApiName);
1465
+ const sourceMetadata = await this.store.client[chunk5KDG5ZET_cjs.additionalContext].ontologyProvider.getObjectDefinition(this.#sourceApiName);
1211
1466
  const linkDef = sourceMetadata.links?.[this.#linkName];
1212
1467
  if (!linkDef || linkDef.targetType !== objectType) return;
1213
1468
  const promise = this.revalidate(true);
@@ -1220,8 +1475,8 @@ var SpecificLinkQuery = class extends BaseListQuery {
1220
1475
 
1221
1476
  // src/observable/internal/links/LinksHelper.ts
1222
1477
  var LinksHelper = class extends AbstractHelper {
1223
- constructor(store, whereCanonicalizer, orderByCanonicalizer) {
1224
- super(store);
1478
+ constructor(store, cacheKeys, whereCanonicalizer, orderByCanonicalizer) {
1479
+ super(store, cacheKeys);
1225
1480
  this.whereCanonicalizer = whereCanonicalizer;
1226
1481
  this.orderByCanonicalizer = orderByCanonicalizer;
1227
1482
  }
@@ -1231,9 +1486,9 @@ var LinksHelper = class extends AbstractHelper {
1231
1486
  } = options.srcType;
1232
1487
  const canonWhere = this.whereCanonicalizer.canonicalize(options.where ?? {});
1233
1488
  const canonOrderBy = this.orderByCanonicalizer.canonicalize(options.orderBy ?? {});
1234
- const linkCacheKey = this.store.getCacheKey("specificLink", apiName, options.pk, options.linkName, canonWhere, canonOrderBy);
1235
- return this.store.getQuery(linkCacheKey, () => {
1236
- return new SpecificLinkQuery(this.store, this.store.getSubject(linkCacheKey), linkCacheKey, options);
1489
+ const linkCacheKey = this.cacheKeys.get("specificLink", apiName, options.pk, options.linkName, canonWhere, canonOrderBy);
1490
+ return this.store.queries.get(linkCacheKey, () => {
1491
+ return new SpecificLinkQuery(this.store, this.store.subjects.get(linkCacheKey), linkCacheKey, options);
1237
1492
  });
1238
1493
  }
1239
1494
  };
@@ -1340,7 +1595,7 @@ var ListQuery = class extends BaseListQuery {
1340
1595
  batch.changes.registerList(this.cacheKey);
1341
1596
  }
1342
1597
  constructor(store, subject, apiType, apiName, whereClause, orderBy, cacheKey, opts) {
1343
- super(store, subject, opts, cacheKey, process.env.NODE_ENV !== "production" ? store.client[chunkELAA4C2C_cjs.additionalContext].logger?.child({}, {
1598
+ super(store, subject, opts, cacheKey, process.env.NODE_ENV !== "production" ? store.client[chunk5KDG5ZET_cjs.additionalContext].logger?.child({}, {
1344
1599
  msgPrefix: `ListQuery<${cacheKey.otherKeys.map((x) => JSON.stringify(x)).join(", ")}>`
1345
1600
  }) : void 0);
1346
1601
  this.#type = apiType;
@@ -1389,7 +1644,7 @@ var ListQuery = class extends BaseListQuery {
1389
1644
  */
1390
1645
  handleFetchError(error, _status, batch) {
1391
1646
  this.logger?.error("error", error);
1392
- this.store.getSubject(this.cacheKey).error(error);
1647
+ this.store.subjects.get(this.cacheKey).error(error);
1393
1648
  return this.writeToStore({
1394
1649
  data: []
1395
1650
  }, "error", batch);
@@ -1468,7 +1723,7 @@ var ListQuery = class extends BaseListQuery {
1468
1723
  const toRemove = new Set(changes.deleted);
1469
1724
  for (const obj of relevantObjects.modified.all) {
1470
1725
  if (relevantObjects.modified.strictMatches.has(obj)) {
1471
- const objectCacheKey = this.store.getCacheKey("object", obj.$objectType, obj.$primaryKey);
1726
+ const objectCacheKey = this.cacheKeys.get("object", obj.$objectType, obj.$primaryKey);
1472
1727
  if (!existingList.has(objectCacheKey)) {
1473
1728
  toAdd.add(obj);
1474
1729
  }
@@ -1476,7 +1731,7 @@ var ListQuery = class extends BaseListQuery {
1476
1731
  } else if (batch.optimisticWrite) {
1477
1732
  continue;
1478
1733
  } else {
1479
- const existingObjectCacheKey = this.store.getCacheKey("object", obj.$objectType, obj.$primaryKey);
1734
+ const existingObjectCacheKey = this.cacheKeys.get("object", obj.$objectType, obj.$primaryKey);
1480
1735
  toRemove.add(existingObjectCacheKey);
1481
1736
  if (relevantObjects.modified.sortaMatches.has(obj)) {
1482
1737
  needsRevalidation = true;
@@ -1488,7 +1743,7 @@ var ListQuery = class extends BaseListQuery {
1488
1743
  newList.push(key);
1489
1744
  }
1490
1745
  for (const obj of toAdd) {
1491
- newList.push(this.store.getCacheKey("object", obj.$objectType, obj.$primaryKey));
1746
+ newList.push(this.cacheKeys.get("object", obj.$objectType, obj.$primaryKey));
1492
1747
  }
1493
1748
  this._updateList(
1494
1749
  newList,
@@ -1533,7 +1788,7 @@ var ListQuery = class extends BaseListQuery {
1533
1788
  }
1534
1789
  #extractRelevantObjectsForTypeInterface(changes) {
1535
1790
  const matchesApiName = ([, object]) => {
1536
- return this.#apiName in object[chunkKCMGIMVC_cjs.ObjectDefRef].interfaceMap;
1791
+ return this.#apiName in object[chunkBY2PQEYA_cjs.ObjectDefRef].interfaceMap;
1537
1792
  };
1538
1793
  const added = Array.from(changes.addedObjects).filter(matchesApiName).map(([, object]) => object.$as(this.#apiName));
1539
1794
  const modified = Array.from(changes.modifiedObjects).filter(matchesApiName).map(([, object]) => object.$as(this.#apiName));
@@ -1635,7 +1890,7 @@ var ListQuery = class extends BaseListQuery {
1635
1890
  const existing = batch.read(this.cacheKey);
1636
1891
  !existing ? process.env.NODE_ENV !== "production" ? invariant2__default.default(false, "the truth value for our list should exist as we already subscribed") : invariant2__default.default(false) : void 0;
1637
1892
  if (existing.status === "loaded") {
1638
- const objectCacheKey = this.store.getCacheKey("object", objOrIface.$objectType, objOrIface.$primaryKey);
1893
+ const objectCacheKey = this.cacheKeys.get("object", objOrIface.$objectType, objOrIface.$primaryKey);
1639
1894
  const newObjects = existing.value?.data.filter((o) => o !== objectCacheKey);
1640
1895
  if (newObjects?.length !== existing.value?.data.length) {
1641
1896
  batch.changes.registerList(this.cacheKey);
@@ -1664,7 +1919,7 @@ var ListQuery = class extends BaseListQuery {
1664
1919
  async function reloadDataAsFullObjects(client, data) {
1665
1920
  const groups = groupBy__default.default(data, (x) => x.$objectType);
1666
1921
  const objectTypeToPrimaryKeyToObject = Object.fromEntries(await Promise.all(Object.entries(groups).map(async ([apiName, objects]) => {
1667
- const objectDef = objects[0][chunkKCMGIMVC_cjs.UnderlyingOsdkObject][chunkKCMGIMVC_cjs.ObjectDefRef];
1922
+ const objectDef = objects[0][chunkBY2PQEYA_cjs.UnderlyingOsdkObject][chunkBY2PQEYA_cjs.ObjectDefRef];
1668
1923
  const where = {
1669
1924
  [objectDef.primaryKeyApiName]: {
1670
1925
  $in: objects.map((x) => x.$primaryKey)
@@ -1682,8 +1937,8 @@ async function reloadDataAsFullObjects(client, data) {
1682
1937
 
1683
1938
  // src/observable/internal/list/ListsHelper.ts
1684
1939
  var ListsHelper = class extends AbstractHelper {
1685
- constructor(store, whereCanonicalizer, orderByCanonicalizer) {
1686
- super(store);
1940
+ constructor(store, cacheKeys, whereCanonicalizer, orderByCanonicalizer) {
1941
+ super(store, cacheKeys);
1687
1942
  this.whereCanonicalizer = whereCanonicalizer;
1688
1943
  this.orderByCanonicalizer = orderByCanonicalizer;
1689
1944
  }
@@ -1705,9 +1960,9 @@ var ListsHelper = class extends AbstractHelper {
1705
1960
  } = options;
1706
1961
  const canonWhere = this.whereCanonicalizer.canonicalize(where ?? {});
1707
1962
  const canonOrderBy = this.orderByCanonicalizer.canonicalize(orderBy ?? {});
1708
- const listCacheKey = this.store.getCacheKey("list", type, apiName, canonWhere, canonOrderBy);
1709
- return this.store.getQuery(listCacheKey, () => {
1710
- return new ListQuery(this.store, this.store.getSubject(listCacheKey), type, apiName, canonWhere, canonOrderBy, listCacheKey, options);
1963
+ const listCacheKey = this.cacheKeys.get("list", type, apiName, canonWhere, canonOrderBy);
1964
+ return this.store.queries.get(listCacheKey, () => {
1965
+ return new ListQuery(this.store, this.store.subjects.get(listCacheKey), type, apiName, canonWhere, canonOrderBy, listCacheKey, options);
1711
1966
  });
1712
1967
  }
1713
1968
  };
@@ -1738,7 +1993,7 @@ var BulkObjectLoader = class {
1738
1993
  #maxEntries;
1739
1994
  constructor(client, maxWait = 25, maxEntries = 100) {
1740
1995
  this.#client = client;
1741
- this.#logger = client[chunkELAA4C2C_cjs.additionalContext].logger;
1996
+ this.#logger = client[chunk5KDG5ZET_cjs.additionalContext].logger;
1742
1997
  this.#maxWait = maxWait;
1743
1998
  this.#maxEntries = maxEntries;
1744
1999
  }
@@ -1801,7 +2056,7 @@ var ObjectQuery = class extends Query {
1801
2056
  #apiName;
1802
2057
  #pk;
1803
2058
  constructor(store, subject, type, pk, cacheKey, opts) {
1804
- super(store, subject, opts, cacheKey, process.env.NODE_ENV !== "production" ? store.client[chunkELAA4C2C_cjs.additionalContext].logger?.child({}, {
2059
+ super(store, subject, opts, cacheKey, process.env.NODE_ENV !== "production" ? store.client[chunk5KDG5ZET_cjs.additionalContext].logger?.child({}, {
1805
2060
  msgPrefix: `ObjectQuery<${cacheKey.otherKeys.map((x) => JSON.stringify(x)).join(", ")}>`
1806
2061
  }) : void 0);
1807
2062
  this.#apiName = type;
@@ -1912,8 +2167,8 @@ var ObjectsHelper = class extends AbstractHelper {
1912
2167
  const {
1913
2168
  pk
1914
2169
  } = options;
1915
- const objectCacheKey = this.store.getCacheKey("object", apiName, pk);
1916
- return this.store.getQuery(objectCacheKey, () => new ObjectQuery(this.store, this.store.getSubject(objectCacheKey), apiName, pk, objectCacheKey, {
2170
+ const objectCacheKey = this.cacheKeys.get("object", apiName, pk);
2171
+ return this.store.queries.get(objectCacheKey, () => new ObjectQuery(this.store, this.store.subjects.get(objectCacheKey), apiName, pk, objectCacheKey, {
1917
2172
  dedupeInterval: 0
1918
2173
  }));
1919
2174
  }
@@ -1985,49 +2240,28 @@ var OrderByCanonicalizer = class {
1985
2240
  };
1986
2241
  };
1987
2242
 
1988
- // src/observable/internal/RefCounts.ts
1989
- var RefCounts = class {
1990
- refCounts = /* @__PURE__ */ new Map();
1991
- // keeps our objects around for some extended duration after they are no longer
1992
- // needed which is good for quick clicks across tabs.
1993
- gcMap = /* @__PURE__ */ new Map();
1994
- constructor(keepAlive, cleanup) {
1995
- this.keepAlive = keepAlive;
1996
- this.cleanup = cleanup;
1997
- }
1998
- register(key) {
1999
- if (!this.refCounts.has(key)) {
2000
- this.gcMap.set(key, Date.now() + this.keepAlive);
2001
- }
2002
- return key;
2003
- }
2004
- retain(key) {
2005
- const count = this.refCounts.get(key) ?? 0;
2006
- this.refCounts.set(key, count + 1);
2007
- if (this.gcMap.has(key)) {
2008
- this.gcMap.delete(key);
2009
- }
2243
+ // src/observable/internal/Queries.ts
2244
+ var Queries = class {
2245
+ // we can use a regular Map here because the refCounting will
2246
+ // handle cleanup.
2247
+ map = /* @__PURE__ */ new Map();
2248
+ peek(cacheKey) {
2249
+ return this.map.get(cacheKey);
2010
2250
  }
2011
- release(key) {
2012
- const count = this.refCounts.get(key);
2013
- if (count === void 0) ; else if (count === 1) {
2014
- this.refCounts.delete(key);
2015
- this.gcMap.set(key, Date.now() + this.keepAlive);
2016
- } else {
2017
- this.refCounts.set(key, count - 1);
2251
+ get(cacheKey, createQuery) {
2252
+ let query = this.peek(cacheKey);
2253
+ if (!query) {
2254
+ query = createQuery();
2255
+ this.map.set(cacheKey, query);
2018
2256
  }
2257
+ return query;
2019
2258
  }
2020
- has(key) {
2021
- return this.refCounts.has(key);
2259
+ keys() {
2260
+ return this.map.keys();
2022
2261
  }
2023
- gc() {
2024
- const now = Date.now();
2025
- for (const [key, deathTime] of this.gcMap) {
2026
- if (deathTime < now) {
2027
- this.gcMap.delete(key);
2028
- this.cleanup(key);
2029
- }
2030
- }
2262
+ delete(cacheKey) {
2263
+ this.map.get(cacheKey)?.dispose();
2264
+ this.map.delete(cacheKey);
2031
2265
  }
2032
2266
  };
2033
2267
  var WhereClauseCanonicalizer = class {
@@ -2096,69 +2330,40 @@ var WhereClauseCanonicalizer = class {
2096
2330
  };
2097
2331
 
2098
2332
  // src/observable/internal/Store.ts
2099
- function createInitEntry(cacheKey) {
2100
- return {
2101
- cacheKey,
2102
- status: "init",
2103
- value: void 0,
2104
- lastUpdated: 0
2105
- };
2106
- }
2107
2333
  var Store = class {
2108
2334
  whereCanonicalizer = new WhereClauseCanonicalizer();
2109
2335
  orderByCanonicalizer = new OrderByCanonicalizer();
2110
- #truthLayer = new Layer(void 0, void 0);
2111
- #topLayer;
2112
2336
  /** @internal */
2113
- // we can use a regular Map here because the refCounting will
2114
- // handle cleanup.
2115
- #queries = /* @__PURE__ */ new Map();
2116
- #cacheKeyToSubject = /* @__PURE__ */ new WeakMap();
2117
- #cacheKeys;
2118
- #refCounts = new RefCounts(6e4, (k) => this.#cleanupCacheKey(k));
2119
- // we are currently only using this for debug logging and should just remove it in the future if that
2120
- // continues to be true
2121
- #finalizationRegistry;
2337
+ queries = new Queries();
2338
+ layers = new Layers({
2339
+ logger: this.logger,
2340
+ onRevalidate: this.#maybeRevalidateQueries.bind(this)
2341
+ });
2342
+ subjects = this.layers.subjects;
2122
2343
  // these are hopefully temporary
2123
2344
  constructor(client) {
2124
- this.client = client;
2125
- this.logger = client[chunkELAA4C2C_cjs.additionalContext].logger?.child({}, {
2345
+ this.logger = client[chunk5KDG5ZET_cjs.additionalContext].logger?.child({}, {
2126
2346
  msgPrefix: "Store"
2127
2347
  });
2128
- this.lists = new ListsHelper(this, this.whereCanonicalizer, this.orderByCanonicalizer);
2129
- this.objects = new ObjectsHelper(this);
2130
- this.links = new LinksHelper(this, this.whereCanonicalizer, this.orderByCanonicalizer);
2131
- this.#topLayer = this.#truthLayer;
2132
- this.#cacheKeys = new CacheKeys(this.whereCanonicalizer, this.orderByCanonicalizer, (k) => {
2133
- this.#refCounts.register(k);
2134
- });
2135
- setInterval(() => {
2136
- this.#refCounts.gc();
2137
- }, 1e3);
2138
- this.#finalizationRegistry = new FinalizationRegistry((cleanupCallback) => {
2139
- try {
2140
- cleanupCallback();
2141
- } catch (e) {
2142
- console.error("Caught an error while running a finalization callback", e);
2143
- }
2348
+ this.client = client;
2349
+ this.cacheKeys = new CacheKeys({
2350
+ onDestroy: this.#cleanupCacheKey
2144
2351
  });
2352
+ this.lists = new ListsHelper(this, this.cacheKeys, this.whereCanonicalizer, this.orderByCanonicalizer);
2353
+ this.objects = new ObjectsHelper(this, this.cacheKeys);
2354
+ this.links = new LinksHelper(this, this.cacheKeys, this.whereCanonicalizer, this.orderByCanonicalizer);
2145
2355
  }
2146
2356
  /**
2147
2357
  * Called after a key is no longer retained and the timeout has elapsed
2148
2358
  * @param key
2149
2359
  */
2150
2360
  #cleanupCacheKey = (key) => {
2151
- const subject = this.peekSubject(key);
2152
- this.#cacheKeys.remove(key);
2361
+ const subject = this.subjects.peek(key);
2153
2362
  if (process.env.NODE_ENV !== "production") {
2154
2363
  !subject ? process.env.NODE_ENV !== "production" ? invariant2__default.default(false) : invariant2__default.default(false) : void 0;
2155
2364
  }
2156
- if (subject) {
2157
- subject.complete();
2158
- this.#cacheKeyToSubject.delete(key);
2159
- }
2160
- this.#queries.get(key)?.dispose();
2161
- this.#queries.delete(key);
2365
+ this.subjects.delete(key);
2366
+ this.queries.delete(key);
2162
2367
  };
2163
2368
  applyAction = async (action, args, opts) => {
2164
2369
  return await new ActionApplication(this).applyAction(action, args, opts);
@@ -2170,120 +2375,18 @@ var Store = class {
2170
2375
  });
2171
2376
  return result;
2172
2377
  };
2173
- removeLayer(layerId) {
2174
- !(layerId != null) ? process.env.NODE_ENV !== "production" ? invariant2__default.default(false, "undefined is the reserved layerId for the truth layer") : invariant2__default.default(false) : void 0;
2175
- let currentLayer = this.#topLayer;
2176
- const cacheKeys = /* @__PURE__ */ new Map();
2177
- while (currentLayer != null && currentLayer.parentLayer != null) {
2178
- if (currentLayer.layerId === layerId) {
2179
- for (const [k, v] of currentLayer.entries()) {
2180
- if (cacheKeys.has(k)) continue;
2181
- cacheKeys.set(k, v);
2182
- }
2183
- }
2184
- currentLayer = currentLayer.parentLayer;
2185
- }
2186
- this.#topLayer = this.#topLayer.removeLayer(layerId);
2187
- for (const [k, oldEntry] of cacheKeys) {
2188
- const currentEntry = this.#topLayer.get(k);
2189
- if (oldEntry !== currentEntry) {
2190
- this.peekSubject(k)?.next({
2191
- // eslint-disable-next-line @typescript-eslint/no-misused-spread
2192
- ...currentEntry ?? createInitEntry(k),
2193
- isOptimistic: currentEntry?.value !== this.#truthLayer.get(k)?.value
2194
- });
2195
- }
2196
- }
2197
- }
2198
- getCacheKey(type, ...args) {
2199
- return this.#refCounts.register(this.#cacheKeys.get(type, ...args));
2200
- }
2201
- peekSubject = (cacheKey) => {
2202
- return this.#cacheKeyToSubject.get(cacheKey);
2203
- };
2204
- getSubject = (cacheKey) => {
2205
- let subject = this.#cacheKeyToSubject.get(cacheKey);
2206
- if (!subject) {
2207
- const initialValue = this.#topLayer.get(cacheKey) ?? createInitEntry(cacheKey);
2208
- subject = new rxjs.BehaviorSubject({
2209
- // eslint-disable-next-line @typescript-eslint/no-misused-spread
2210
- ...initialValue,
2211
- isOptimistic: initialValue.value !== this.#truthLayer.get(cacheKey)?.value
2212
- });
2213
- this.#cacheKeyToSubject.set(cacheKey, subject);
2214
- }
2215
- return subject;
2216
- };
2217
- canonicalizeWhereClause(where) {
2218
- return this.whereCanonicalizer.canonicalize(where);
2219
- }
2220
- peekQuery(cacheKey) {
2221
- return this.#queries.get(cacheKey);
2222
- }
2223
- getQuery(cacheKey, createQuery) {
2224
- let query = this.peekQuery(cacheKey);
2225
- if (!query) {
2226
- query = createQuery();
2227
- this.#queries.set(cacheKey, query);
2228
- }
2229
- return query;
2230
- }
2231
2378
  getValue(cacheKey) {
2232
- return this.#topLayer.get(cacheKey);
2379
+ return this.layers.top.get(cacheKey);
2233
2380
  }
2234
- batch = ({
2381
+ batch({
2235
2382
  optimisticId,
2236
2383
  changes = createChangedObjects()
2237
- }, batchFn) => {
2238
- !(optimisticId === void 0 || !!optimisticId) ? process.env.NODE_ENV !== "production" ? invariant2__default.default(false, "optimistic must be undefined or not falsy") : invariant2__default.default(false) : void 0;
2239
- let needsLayer = optimisticId !== void 0;
2240
- const batchContext = {
2241
- changes,
2242
- createLayerIfNeeded: () => {
2243
- if (needsLayer) {
2244
- this.#topLayer = this.#topLayer.addLayer(optimisticId);
2245
- needsLayer = false;
2246
- }
2247
- },
2248
- optimisticWrite: !!optimisticId,
2249
- write: (cacheKey, value, status) => {
2250
- const oldTopValue = this.#topLayer.get(cacheKey);
2251
- if (optimisticId) batchContext.createLayerIfNeeded();
2252
- const writeLayer = optimisticId ? this.#topLayer : this.#truthLayer;
2253
- const newValue = new Entry(cacheKey, value, Date.now(), status);
2254
- writeLayer.set(cacheKey, newValue);
2255
- const newTopValue = this.#topLayer.get(cacheKey);
2256
- if (oldTopValue !== newTopValue) {
2257
- this.getSubject(cacheKey)?.next({
2258
- // eslint-disable-next-line @typescript-eslint/no-misused-spread
2259
- ...newValue,
2260
- isOptimistic: newTopValue?.value !== this.#truthLayer.get(cacheKey)?.value
2261
- });
2262
- }
2263
- return newValue;
2264
- },
2265
- delete: (cacheKey, status) => {
2266
- return batchContext.write(cacheKey, tombstone, status);
2267
- },
2268
- read: (cacheKey) => {
2269
- return optimisticId ? this.#topLayer.get(cacheKey) : this.#truthLayer.get(cacheKey);
2270
- }
2271
- };
2272
- const retVal = batchFn(batchContext);
2273
- this.maybeRevalidateQueries(changes, optimisticId).catch((e) => {
2274
- if (this.logger) {
2275
- this.logger.error("Unhandled error in batch", e);
2276
- } else {
2277
- console.error("Unhandled error in batch", e);
2278
- throw e;
2279
- }
2280
- });
2281
- return {
2282
- batchResult: batchContext,
2283
- retVal,
2284
- changes: batchContext.changes
2285
- };
2286
- };
2384
+ }, batchFn) {
2385
+ return this.layers.batch({
2386
+ optimisticId,
2387
+ changes
2388
+ }, batchFn);
2389
+ }
2287
2390
  invalidateObject(apiName, pk) {
2288
2391
  if (typeof apiName !== "string") {
2289
2392
  apiName = apiName.apiName;
@@ -2296,34 +2399,31 @@ var Store = class {
2296
2399
  true
2297
2400
  );
2298
2401
  }
2299
- async maybeRevalidateQueries(changes, optimisticId) {
2402
+ async #maybeRevalidateQueries(changes, optimisticId) {
2403
+ const logger = process.env.NODE_ENV !== "production" ? this.logger?.child({
2404
+ methodName: "maybeRevalidateQueries"
2405
+ }) : void 0;
2300
2406
  if (changes.isEmpty()) {
2301
2407
  if (process.env.NODE_ENV !== "production") {
2302
- this.logger?.child({
2303
- methodName: "maybeRevalidateQueries"
2304
- }).debug("No changes, aborting");
2408
+ logger?.debug("No changes, aborting");
2305
2409
  }
2306
2410
  return;
2307
2411
  }
2308
2412
  if (process.env.NODE_ENV !== "production") {
2309
- this.logger?.child({
2310
- methodName: "maybeRevalidateQueries"
2311
- }).debug(DEBUG_ONLY__changesToString(changes), {
2413
+ logger?.debug(DEBUG_ONLY__changesToString(changes), {
2312
2414
  optimisticId
2313
2415
  });
2314
2416
  }
2315
2417
  try {
2316
2418
  const promises = [];
2317
- for (const cacheKey of this.#queries.keys()) {
2318
- const promise = this.peekQuery(cacheKey)?.maybeUpdateAndRevalidate?.(changes, optimisticId);
2419
+ for (const cacheKey of this.queries.keys()) {
2420
+ const promise = this.queries.peek(cacheKey)?.maybeUpdateAndRevalidate?.(changes, optimisticId);
2319
2421
  if (promise) promises.push(promise);
2320
2422
  }
2321
2423
  await Promise.all(promises);
2322
2424
  } finally {
2323
2425
  if (process.env.NODE_ENV !== "production") {
2324
- this.logger?.child({
2325
- methodName: "maybeRevalidateQueries"
2326
- }).debug("in finally", DEBUG_ONLY__changesToString(changes));
2426
+ logger?.debug("in finally", DEBUG_ONLY__changesToString(changes));
2327
2427
  }
2328
2428
  }
2329
2429
  }
@@ -2348,30 +2448,24 @@ var Store = class {
2348
2448
  }).info(changes ? DEBUG_ONLY__changesToString(changes) : void 0);
2349
2449
  }
2350
2450
  const promises = [];
2351
- for (const cacheKey of this.#truthLayer.keys()) {
2451
+ for (const cacheKey of this.layers.truth.keys()) {
2352
2452
  if (changes && changes.modified.has(cacheKey)) {
2353
2453
  continue;
2354
2454
  }
2355
- const query = this.peekQuery(cacheKey);
2455
+ const query = this.queries.peek(cacheKey);
2356
2456
  if (!query) continue;
2357
2457
  promises.push(query.invalidateObjectType(apiName, changes));
2358
2458
  }
2359
2459
  return Promise.allSettled(promises).then(() => void 0);
2360
2460
  }
2361
- retain(cacheKey) {
2362
- this.#refCounts.retain(cacheKey);
2363
- }
2364
- release(cacheKey) {
2365
- this.#refCounts.release(cacheKey);
2366
- }
2367
2461
  };
2368
2462
 
2369
2463
  // src/observable/ObservableClient.ts
2370
2464
  function createObservableClient(client) {
2371
- const tweakedClient = chunkKCMGIMVC_cjs.createClientFromContext({
2372
- ...client[chunkELAA4C2C_cjs.additionalContext],
2373
- fetch: shared_net_fetch.createFetchHeaderMutator(client[chunkELAA4C2C_cjs.additionalContext].fetch, (headers) => {
2374
- headers.set("Fetch-User-Agent", [headers.get("Fetch-User-Agent"), chunkKCMGIMVC_cjs.OBSERVABLE_USER_AGENT].filter((x) => x && x?.length > 0).join(" "));
2465
+ const tweakedClient = chunkBY2PQEYA_cjs.createClientFromContext({
2466
+ ...client[chunk5KDG5ZET_cjs.additionalContext],
2467
+ fetch: shared_net_fetch.createFetchHeaderMutator(client[chunk5KDG5ZET_cjs.additionalContext].fetch, (headers) => {
2468
+ headers.set("Fetch-User-Agent", [headers.get("Fetch-User-Agent"), chunkBY2PQEYA_cjs.OBSERVABLE_USER_AGENT].filter((x) => x && x?.length > 0).join(" "));
2375
2469
  return headers;
2376
2470
  })
2377
2471
  });
@@ -2414,11 +2508,11 @@ function getOsdkConfig(ontologyRid) {
2414
2508
 
2415
2509
  Object.defineProperty(exports, "createClientWithTransaction", {
2416
2510
  enumerable: true,
2417
- get: function () { return chunkKCMGIMVC_cjs.createClientWithTransaction; }
2511
+ get: function () { return chunkBY2PQEYA_cjs.createClientWithTransaction; }
2418
2512
  });
2419
2513
  Object.defineProperty(exports, "augment", {
2420
2514
  enumerable: true,
2421
- get: function () { return chunkELAA4C2C_cjs.augment; }
2515
+ get: function () { return chunk5KDG5ZET_cjs.augment; }
2422
2516
  });
2423
2517
  exports.createObservableClient = createObservableClient;
2424
2518
  exports.getMetaTagContent = getMetaTagContent;