cozy-pouch-link 60.28.0 → 60.28.2

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.
@@ -1683,7 +1683,8 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
1683
1683
  }()
1684
1684
  /**
1685
1685
  * Adds a new doctype to the list of managed doctypes, sets its replication options,
1686
- * adds it to the pouches, and starts replication.
1686
+ * adds it to the pouches, and starts replication. Does nothing when the doctype is
1687
+ * already managed, so the list stays free of duplicates.
1687
1688
  *
1688
1689
  * @param {string} doctype - The name of the doctype to add.
1689
1690
  * @param {Object} replicationOptions - The replication options for the doctype.
@@ -1699,23 +1700,37 @@ var PouchLink = /*#__PURE__*/function (_CozyLink) {
1699
1700
  while (1) {
1700
1701
  switch (_context23.prev = _context23.next) {
1701
1702
  case 0:
1703
+ if (!this.doctypes.includes(doctype)) {
1704
+ _context23.next = 3;
1705
+ break;
1706
+ }
1707
+
1708
+ _logger.default.debug("PouchLink: doctype ".concat(doctype, " already managed, skipping"));
1709
+
1710
+ return _context23.abrupt("return");
1711
+
1712
+ case 3:
1702
1713
  this.doctypes.push(doctype);
1703
1714
 
1704
1715
  if (!this.doctypesReplicationOptions) {
1705
1716
  this.doctypesReplicationOptions = {};
1706
1717
  }
1707
1718
 
1708
- this.doctypesReplicationOptions[doctype] = replicationOptions;
1709
- this.pouches.doctypes.push(doctype);
1710
- _context23.next = 6;
1719
+ this.doctypesReplicationOptions[doctype] = replicationOptions; // sync onto pouches.doctypes, a separate array only after removeDoctype
1720
+
1721
+ if (!this.pouches.doctypes.includes(doctype)) {
1722
+ this.pouches.doctypes.push(doctype);
1723
+ }
1724
+
1725
+ _context23.next = 9;
1711
1726
  return this.pouches.addDoctype(doctype, replicationOptions);
1712
1727
 
1713
- case 6:
1728
+ case 9:
1714
1729
  if ((options === null || options === void 0 ? void 0 : options.shouldStartReplication) === true) {
1715
1730
  this.startReplicationWithDebounce();
1716
1731
  }
1717
1732
 
1718
- case 7:
1733
+ case 10:
1719
1734
  case "end":
1720
1735
  return _context23.stop();
1721
1736
  }
@@ -574,6 +574,72 @@ describe('CozyPouchLink', () => {
574
574
  })
575
575
  })
576
576
 
577
+ describe('addDoctype', () => {
578
+ const OTHER_DOCTYPE = 'io.cozy.files'
579
+
580
+ it('should register a doctype only once, even though the link and pouch manager share the doctypes array', async () => {
581
+ await setup()
582
+ jest.spyOn(link.pouches, 'addDoctype').mockResolvedValue(undefined)
583
+ // The link and its pouch manager hold the same array, so a single
584
+ // registration must not land the doctype in it twice.
585
+ expect(link.doctypes).toBe(link.pouches.doctypes)
586
+
587
+ await link.addDoctype(OTHER_DOCTYPE, { strategy: 'fromRemote' })
588
+
589
+ expect(link.doctypes.filter(d => d === OTHER_DOCTYPE)).toHaveLength(1)
590
+ expect(
591
+ link.pouches.doctypes.filter(d => d === OTHER_DOCTYPE)
592
+ ).toHaveLength(1)
593
+ expect(link.pouches.addDoctype).toHaveBeenCalledTimes(1)
594
+ })
595
+
596
+ it('should not re-register, restart replication, or overwrite options for a managed doctype', async () => {
597
+ await setup()
598
+ jest.spyOn(link.pouches, 'addDoctype').mockResolvedValue(undefined)
599
+ jest
600
+ .spyOn(link, 'startReplicationWithDebounce')
601
+ .mockImplementation(() => {})
602
+
603
+ await link.addDoctype(
604
+ OTHER_DOCTYPE,
605
+ { strategy: 'fromRemote' },
606
+ { shouldStartReplication: true }
607
+ )
608
+ await link.addDoctype(
609
+ OTHER_DOCTYPE,
610
+ { strategy: 'sync' },
611
+ { shouldStartReplication: true }
612
+ )
613
+
614
+ expect(link.doctypes.filter(d => d === OTHER_DOCTYPE)).toHaveLength(1)
615
+ expect(
616
+ link.pouches.doctypes.filter(d => d === OTHER_DOCTYPE)
617
+ ).toHaveLength(1)
618
+ expect(link.pouches.addDoctype).toHaveBeenCalledTimes(1)
619
+ expect(link.startReplicationWithDebounce).toHaveBeenCalledTimes(1)
620
+ // The no-op re-add keeps the original options instead of overwriting them.
621
+ expect(link.doctypesReplicationOptions[OTHER_DOCTYPE]).toEqual({
622
+ strategy: 'fromRemote'
623
+ })
624
+ })
625
+
626
+ it('should register again a doctype that was previously removed', async () => {
627
+ await setup()
628
+ const addSpy = jest.spyOn(link.pouches, 'addDoctype')
629
+
630
+ await link.addDoctype(OTHER_DOCTYPE, { strategy: 'fromRemote' })
631
+ await link.removeDoctype(OTHER_DOCTYPE)
632
+ await link.addDoctype(OTHER_DOCTYPE, { strategy: 'fromRemote' })
633
+
634
+ expect(link.doctypes.filter(d => d === OTHER_DOCTYPE)).toHaveLength(1)
635
+ expect(
636
+ link.pouches.doctypes.filter(d => d === OTHER_DOCTYPE)
637
+ ).toHaveLength(1)
638
+ // The pouch is recreated on the second registration.
639
+ expect(addSpy).toHaveBeenCalledTimes(2)
640
+ })
641
+ })
642
+
577
643
  describe('immediate sync', () => {
578
644
  it('should not throw if pouches not there', async () => {
579
645
  await setup()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-pouch-link",
3
- "version": "60.28.0",
3
+ "version": "60.28.2",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "types/index.d.ts",
@@ -13,7 +13,7 @@
13
13
  "url": "git+https://github.com/linagora/cozy-client.git"
14
14
  },
15
15
  "dependencies": {
16
- "cozy-client": "^60.28.0",
16
+ "cozy-client": "^60.28.2",
17
17
  "pouchdb-browser": "^7.2.2",
18
18
  "pouchdb-find": "^7.2.2"
19
19
  },
@@ -41,5 +41,5 @@
41
41
  "typecheck": "tsc -p tsconfig.json"
42
42
  },
43
43
  "sideEffects": false,
44
- "gitHead": "8eeb721eca10bdd9e48d537b82b5092a81dc2dd3"
44
+ "gitHead": "272fde3c9e8dc4a4472744513ace8b71a8b2c417"
45
45
  }
@@ -253,7 +253,8 @@ declare class PouchLink extends CozyLink {
253
253
  syncImmediately(): Promise<void>;
254
254
  /**
255
255
  * Adds a new doctype to the list of managed doctypes, sets its replication options,
256
- * adds it to the pouches, and starts replication.
256
+ * adds it to the pouches, and starts replication. Does nothing when the doctype is
257
+ * already managed, so the list stays free of duplicates.
257
258
  *
258
259
  * @param {string} doctype - The name of the doctype to add.
259
260
  * @param {Object} replicationOptions - The replication options for the doctype.