@routier/core 0.0.1 → 0.0.3

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/index.js CHANGED
@@ -5364,6 +5364,8 @@ const getMemoryPluginCollectionSize = (plugin, schema) => {
5364
5364
  }
5365
5365
  throw new Error("Cannot get size of collection for MemoryPlugin, not an instance of MemoryPlugin");
5366
5366
  };
5367
+ const WAS_OPTIMISTIC_DB_HYDRATED_KEY = "was-hydrated";
5368
+ const cache = new Set();
5367
5369
  class OptimisticReplicationDbPlugin {
5368
5370
  plugins;
5369
5371
  constructor(plugins) {
@@ -5387,7 +5389,9 @@ class OptimisticReplicationDbPlugin {
5387
5389
  const readPlugin = this.plugins.read;
5388
5390
  const sourcePlugin = this.plugins.source;
5389
5391
  const collectionSize = getMemoryPluginCollectionSize(this.plugins.read, event.operation.schema);
5390
- if (collectionSize === 0) {
5392
+ if (collectionSize === 0 && cache.has(WAS_OPTIMISTIC_DB_HYDRATED_KEY) === false) {
5393
+ // Notify the cache that the db was hydrated right away
5394
+ cache.add(WAS_OPTIMISTIC_DB_HYDRATED_KEY);
5391
5395
  // nothing is hydrated, let's try and hydrate before querying
5392
5396
  // Memory plugin might not be hydrated, lets hydrate it for the targeted schema only,
5393
5397
  // Other queries will do the same and hydrate if needed
@@ -5400,14 +5404,20 @@ class OptimisticReplicationDbPlugin {
5400
5404
  operation: _query__WEBPACK_IMPORTED_MODULE_1__.Query.EMPTY(event.operation.schema)
5401
5405
  }, (sourceResult) => {
5402
5406
  if (sourceResult.ok === _results__WEBPACK_IMPORTED_MODULE_2__.Result.ERROR) {
5407
+ // Notify that hydration failed
5408
+ cache.delete(WAS_OPTIMISTIC_DB_HYDRATED_KEY);
5403
5409
  done(sourceResult);
5404
5410
  return;
5405
5411
  }
5406
5412
  if (sourceResult == null || (Array.isArray(sourceResult.data) && sourceResult.data.length === 0)) {
5413
+ // Notify that hydration had no data
5414
+ cache.delete(WAS_OPTIMISTIC_DB_HYDRATED_KEY);
5407
5415
  done(sourceResult);
5408
5416
  return;
5409
5417
  }
5410
5418
  if (Array.isArray(sourceResult.data) === false) {
5419
+ // Notify that hydration failed
5420
+ cache.delete(WAS_OPTIMISTIC_DB_HYDRATED_KEY);
5411
5421
  done(_results__WEBPACK_IMPORTED_MODULE_2__.PluginEventResult.error(event.id, "Query result is not an array"));
5412
5422
  return;
5413
5423
  }
@@ -5422,6 +5432,8 @@ class OptimisticReplicationDbPlugin {
5422
5432
  source: "collection",
5423
5433
  }, (readPersistResult) => {
5424
5434
  if (readPersistResult.ok === _results__WEBPACK_IMPORTED_MODULE_2__.Result.ERROR) {
5435
+ // Notify that hydration failed
5436
+ cache.delete(WAS_OPTIMISTIC_DB_HYDRATED_KEY);
5425
5437
  done(readPersistResult);
5426
5438
  return;
5427
5439
  }