@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.
@@ -1488,8 +1488,10 @@ const getMemoryPluginCollectionSize = (plugin, schema) => {
1488
1488
  }
1489
1489
  throw new Error("Cannot get size of collection for MemoryPlugin, not an instance of MemoryPlugin");
1490
1490
  };
1491
- const WAS_OPTIMISTIC_DB_HYDRATED_KEY = "was-hydrated";
1492
- const cache = new Set();
1491
+ const HYDRATION_STATUS_PENDING = "hydration-pending";
1492
+ const HYDRATION_STATUS_ERROR = "hydration-error";
1493
+ const HYDRATION_STATUS_SUCCESS = "hydration-success";
1494
+ let hydrationStatus = "hydration-not-started";
1493
1495
  class OptimisticReplicationDbPlugin {
1494
1496
  plugins;
1495
1497
  constructor(plugins) {
@@ -1513,10 +1515,10 @@ class OptimisticReplicationDbPlugin {
1513
1515
  const readPlugin = this.plugins.read;
1514
1516
  const sourcePlugin = this.plugins.source;
1515
1517
  const collectionSize = getMemoryPluginCollectionSize(this.plugins.read, event.operation.schema);
1516
- if (collectionSize === 0 && cache.has(WAS_OPTIMISTIC_DB_HYDRATED_KEY) === false) {
1518
+ if (collectionSize === 0 && hydrationStatus === "hydration-not-started") {
1517
1519
  // Notify the cache that the db was hydrated right away
1518
- cache.add(WAS_OPTIMISTIC_DB_HYDRATED_KEY);
1519
- console.log('[ROUTIER] - Optimistic Query', cache);
1520
+ hydrationStatus = "hydration-pending";
1521
+ console.log('[ROUTIER] - Optimistic Query', hydrationStatus);
1520
1522
  // nothing is hydrated, let's try and hydrate before querying
1521
1523
  // Memory plugin might not be hydrated, lets hydrate it for the targeted schema only,
1522
1524
  // Other queries will do the same and hydrate if needed
@@ -1530,19 +1532,22 @@ class OptimisticReplicationDbPlugin {
1530
1532
  }, (sourceResult) => {
1531
1533
  if (sourceResult.ok === _results__WEBPACK_IMPORTED_MODULE_2__.Result.ERROR) {
1532
1534
  // Notify that hydration failed
1533
- cache.delete(WAS_OPTIMISTIC_DB_HYDRATED_KEY);
1535
+ hydrationStatus = "hydration-error";
1536
+ console.log("[ROUTIER] - Hydration Error - Source Query", sourceResult);
1534
1537
  done(sourceResult);
1535
1538
  return;
1536
1539
  }
1537
1540
  if (sourceResult == null || (Array.isArray(sourceResult.data) && sourceResult.data.length === 0)) {
1538
1541
  // Notify that hydration had no data
1539
- cache.delete(WAS_OPTIMISTIC_DB_HYDRATED_KEY);
1542
+ hydrationStatus = "hydration-error";
1543
+ console.log("[ROUTIER] - Hydration Error - No Data", sourceResult);
1540
1544
  done(sourceResult);
1541
1545
  return;
1542
1546
  }
1543
1547
  if (Array.isArray(sourceResult.data) === false) {
1544
1548
  // Notify that hydration failed
1545
- cache.delete(WAS_OPTIMISTIC_DB_HYDRATED_KEY);
1549
+ hydrationStatus = "hydration-error";
1550
+ console.log("[ROUTIER] - Hydration Error - Bad Result", sourceResult);
1546
1551
  done(_results__WEBPACK_IMPORTED_MODULE_2__.PluginEventResult.error(event.id, "Query result is not an array"));
1547
1552
  return;
1548
1553
  }
@@ -1558,10 +1563,12 @@ class OptimisticReplicationDbPlugin {
1558
1563
  }, (readPersistResult) => {
1559
1564
  if (readPersistResult.ok === _results__WEBPACK_IMPORTED_MODULE_2__.Result.ERROR) {
1560
1565
  // Notify that hydration failed
1561
- cache.delete(WAS_OPTIMISTIC_DB_HYDRATED_KEY);
1566
+ hydrationStatus = "hydration-error";
1567
+ console.log("[ROUTIER] - Hydration Error - Could Not Save", readPersistResult);
1562
1568
  done(readPersistResult);
1563
1569
  return;
1564
1570
  }
1571
+ hydrationStatus = "hydration-success";
1565
1572
  // requery the read plugin
1566
1573
  readPlugin.query(event, done);
1567
1574
  });