@routier/core 0.0.3 → 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,9 +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);
5396
+ hydrationStatus = "hydration-pending";
5397
+ console.log('[ROUTIER] - Optimistic Query', hydrationStatus);
5395
5398
  // nothing is hydrated, let's try and hydrate before querying
5396
5399
  // Memory plugin might not be hydrated, lets hydrate it for the targeted schema only,
5397
5400
  // Other queries will do the same and hydrate if needed
@@ -5405,19 +5408,22 @@ class OptimisticReplicationDbPlugin {
5405
5408
  }, (sourceResult) => {
5406
5409
  if (sourceResult.ok === _results__WEBPACK_IMPORTED_MODULE_2__.Result.ERROR) {
5407
5410
  // Notify that hydration failed
5408
- cache.delete(WAS_OPTIMISTIC_DB_HYDRATED_KEY);
5411
+ hydrationStatus = "hydration-error";
5412
+ console.log("[ROUTIER] - Hydration Error - Source Query", sourceResult);
5409
5413
  done(sourceResult);
5410
5414
  return;
5411
5415
  }
5412
5416
  if (sourceResult == null || (Array.isArray(sourceResult.data) && sourceResult.data.length === 0)) {
5413
5417
  // Notify that hydration had no data
5414
- cache.delete(WAS_OPTIMISTIC_DB_HYDRATED_KEY);
5418
+ hydrationStatus = "hydration-error";
5419
+ console.log("[ROUTIER] - Hydration Error - No Data", sourceResult);
5415
5420
  done(sourceResult);
5416
5421
  return;
5417
5422
  }
5418
5423
  if (Array.isArray(sourceResult.data) === false) {
5419
5424
  // Notify that hydration failed
5420
- cache.delete(WAS_OPTIMISTIC_DB_HYDRATED_KEY);
5425
+ hydrationStatus = "hydration-error";
5426
+ console.log("[ROUTIER] - Hydration Error - Bad Result", sourceResult);
5421
5427
  done(_results__WEBPACK_IMPORTED_MODULE_2__.PluginEventResult.error(event.id, "Query result is not an array"));
5422
5428
  return;
5423
5429
  }
@@ -5433,10 +5439,12 @@ class OptimisticReplicationDbPlugin {
5433
5439
  }, (readPersistResult) => {
5434
5440
  if (readPersistResult.ok === _results__WEBPACK_IMPORTED_MODULE_2__.Result.ERROR) {
5435
5441
  // Notify that hydration failed
5436
- cache.delete(WAS_OPTIMISTIC_DB_HYDRATED_KEY);
5442
+ hydrationStatus = "hydration-error";
5443
+ console.log("[ROUTIER] - Hydration Error - Could Not Save", readPersistResult);
5437
5444
  done(readPersistResult);
5438
5445
  return;
5439
5446
  }
5447
+ hydrationStatus = "hydration-success";
5440
5448
  // requery the read plugin
5441
5449
  readPlugin.query(event, done);
5442
5450
  });