@routier/core 0.0.4 → 0.0.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.
package/dist/index.js CHANGED
@@ -5364,8 +5364,10 @@ 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
+ const HYDRATION_STATUS_PENDING = "hydration-pending";
5368
+ const HYDRATION_STATUS_ERROR = "hydration-error";
5369
+ const HYDRATION_STATUS_SUCCESS = "hydration-success";
5370
+ let hydrationStatus = "hydration-not-started";
5369
5371
  class OptimisticReplicationDbPlugin {
5370
5372
  plugins;
5371
5373
  constructor(plugins) {
@@ -5389,10 +5391,10 @@ class OptimisticReplicationDbPlugin {
5389
5391
  const readPlugin = this.plugins.read;
5390
5392
  const sourcePlugin = this.plugins.source;
5391
5393
  const collectionSize = getMemoryPluginCollectionSize(this.plugins.read, event.operation.schema);
5392
- if (collectionSize === 0 && cache.has(WAS_OPTIMISTIC_DB_HYDRATED_KEY) === false) {
5394
+ if (collectionSize === 0 && hydrationStatus === "hydration-not-started") {
5393
5395
  // Notify the cache that the db was hydrated right away
5394
- cache.add(WAS_OPTIMISTIC_DB_HYDRATED_KEY);
5395
- console.log('[ROUTIER] - Optimistic Query', cache);
5396
+ hydrationStatus = "hydration-pending";
5397
+ console.log('[ROUTIER] - Optimistic Query', hydrationStatus);
5396
5398
  // nothing is hydrated, let's try and hydrate before querying
5397
5399
  // Memory plugin might not be hydrated, lets hydrate it for the targeted schema only,
5398
5400
  // Other queries will do the same and hydrate if needed
@@ -5406,19 +5408,22 @@ class OptimisticReplicationDbPlugin {
5406
5408
  }, (sourceResult) => {
5407
5409
  if (sourceResult.ok === _results__WEBPACK_IMPORTED_MODULE_2__.Result.ERROR) {
5408
5410
  // Notify that hydration failed
5409
- cache.delete(WAS_OPTIMISTIC_DB_HYDRATED_KEY);
5411
+ hydrationStatus = "hydration-error";
5412
+ console.log("[ROUTIER] - Hydration Error - Source Query", sourceResult);
5410
5413
  done(sourceResult);
5411
5414
  return;
5412
5415
  }
5413
5416
  if (sourceResult == null || (Array.isArray(sourceResult.data) && sourceResult.data.length === 0)) {
5414
5417
  // Notify that hydration had no data
5415
- cache.delete(WAS_OPTIMISTIC_DB_HYDRATED_KEY);
5418
+ hydrationStatus = "hydration-error";
5419
+ console.log("[ROUTIER] - Hydration Error - No Data", sourceResult);
5416
5420
  done(sourceResult);
5417
5421
  return;
5418
5422
  }
5419
5423
  if (Array.isArray(sourceResult.data) === false) {
5420
5424
  // Notify that hydration failed
5421
- cache.delete(WAS_OPTIMISTIC_DB_HYDRATED_KEY);
5425
+ hydrationStatus = "hydration-error";
5426
+ console.log("[ROUTIER] - Hydration Error - Bad Result", sourceResult);
5422
5427
  done(_results__WEBPACK_IMPORTED_MODULE_2__.PluginEventResult.error(event.id, "Query result is not an array"));
5423
5428
  return;
5424
5429
  }
@@ -5434,10 +5439,12 @@ class OptimisticReplicationDbPlugin {
5434
5439
  }, (readPersistResult) => {
5435
5440
  if (readPersistResult.ok === _results__WEBPACK_IMPORTED_MODULE_2__.Result.ERROR) {
5436
5441
  // Notify that hydration failed
5437
- cache.delete(WAS_OPTIMISTIC_DB_HYDRATED_KEY);
5442
+ hydrationStatus = "hydration-error";
5443
+ console.log("[ROUTIER] - Hydration Error - Could Not Save", readPersistResult);
5438
5444
  done(readPersistResult);
5439
5445
  return;
5440
5446
  }
5447
+ hydrationStatus = "hydration-success";
5441
5448
  // requery the read plugin
5442
5449
  readPlugin.query(event, done);
5443
5450
  });