@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.
@@ -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,9 +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);
1520
+ hydrationStatus = "hydration-pending";
1521
+ console.log('[ROUTIER] - Optimistic Query', hydrationStatus);
1519
1522
  // nothing is hydrated, let's try and hydrate before querying
1520
1523
  // Memory plugin might not be hydrated, lets hydrate it for the targeted schema only,
1521
1524
  // Other queries will do the same and hydrate if needed
@@ -1529,19 +1532,22 @@ class OptimisticReplicationDbPlugin {
1529
1532
  }, (sourceResult) => {
1530
1533
  if (sourceResult.ok === _results__WEBPACK_IMPORTED_MODULE_2__.Result.ERROR) {
1531
1534
  // Notify that hydration failed
1532
- cache.delete(WAS_OPTIMISTIC_DB_HYDRATED_KEY);
1535
+ hydrationStatus = "hydration-error";
1536
+ console.log("[ROUTIER] - Hydration Error - Source Query", sourceResult);
1533
1537
  done(sourceResult);
1534
1538
  return;
1535
1539
  }
1536
1540
  if (sourceResult == null || (Array.isArray(sourceResult.data) && sourceResult.data.length === 0)) {
1537
1541
  // Notify that hydration had no data
1538
- cache.delete(WAS_OPTIMISTIC_DB_HYDRATED_KEY);
1542
+ hydrationStatus = "hydration-error";
1543
+ console.log("[ROUTIER] - Hydration Error - No Data", sourceResult);
1539
1544
  done(sourceResult);
1540
1545
  return;
1541
1546
  }
1542
1547
  if (Array.isArray(sourceResult.data) === false) {
1543
1548
  // Notify that hydration failed
1544
- cache.delete(WAS_OPTIMISTIC_DB_HYDRATED_KEY);
1549
+ hydrationStatus = "hydration-error";
1550
+ console.log("[ROUTIER] - Hydration Error - Bad Result", sourceResult);
1545
1551
  done(_results__WEBPACK_IMPORTED_MODULE_2__.PluginEventResult.error(event.id, "Query result is not an array"));
1546
1552
  return;
1547
1553
  }
@@ -1557,10 +1563,12 @@ class OptimisticReplicationDbPlugin {
1557
1563
  }, (readPersistResult) => {
1558
1564
  if (readPersistResult.ok === _results__WEBPACK_IMPORTED_MODULE_2__.Result.ERROR) {
1559
1565
  // Notify that hydration failed
1560
- cache.delete(WAS_OPTIMISTIC_DB_HYDRATED_KEY);
1566
+ hydrationStatus = "hydration-error";
1567
+ console.log("[ROUTIER] - Hydration Error - Could Not Save", readPersistResult);
1561
1568
  done(readPersistResult);
1562
1569
  return;
1563
1570
  }
1571
+ hydrationStatus = "hydration-success";
1564
1572
  // requery the read plugin
1565
1573
  readPlugin.query(event, done);
1566
1574
  });