Repository: maxnowack/signaldb
Pull request #1305 (2025-01-13 21:05:08)

feat: implement startAll and pauseAll methods in SyncManager

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/maxnowack/signaldb/pull/1305?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Max+Nowack) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 100.00%. Comparing base [(`3eb9465`)](https://app.codecov.io/gh/maxnowack/signaldb/commit/3eb9465b9ba28dc31f05678d9d6da1057ce90386?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Max+Nowack) to head [(`221bca4`)](https://app.codecov.io/gh/maxnowack/signaldb/commit/221bca48b2cef9bbdf1adbb919e6b1dafd505199?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Max+Nowack).

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff            @@
##              main     #1305   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           56        56           
  Lines         1712      1718    +6     
  Branches       385       385           
=========================================
+ Hits          1712      1718    +6     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/maxnowack/signaldb/pull/1305?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Max+Nowack).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Max+Nowack).


Patch:
diff --git a/docs/reference/sync/index.md b/docs/reference/sync/index.md
index 042b0a4..d364e33 100644
--- a/docs/reference/sync/index.md
+++ b/docs/reference/sync/index.md
@@ -133,6 +133,14 @@ Pauses the sync process for a collection. This means that the collection will no
 
 - `name` (`string`): The name of the collection.
 
+### `startAll()`
+
+Setup all collections to be synced with remote changes and enable automatic pushing changes to the remote source.
+
+### `pauseAll()`
+
+Pauses the sync process for all collections. This means that the collections will not be synced with remote changes and changes will not automatically be pushed to the remote source.
+
 ### `pushChanges(name)`
 
 Initiates the push process for a collection, syncing only if there are changes.
diff --git a/packages/base/sync/CHANGELOG.md b/packages/base/sync/CHANGELOG.md
index 3bec7c6..b0c3053 100644
--- a/packages/base/sync/CHANGELOG.md
+++ b/packages/base/sync/CHANGELOG.md
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 ### Added
 
 * Allow sync to be paused and resumed with `syncManager.startSync(name)` and `syncManager.pauseSync(name)` (thanks @obedm503)
+* Added `syncManager.startAll()` and `syncManasger.pauseAll()` to start and pause all collections
 
 ## [1.0.1] - 2025-01-10
 
diff --git a/packages/base/sync/src/SyncManager.ts b/packages/base/sync/src/SyncManager.ts
index 3462b49..5998586 100644
--- a/packages/base/sync/src/SyncManager.ts
+++ b/packages/base/sync/src/SyncManager.ts
@@ -315,6 +315,15 @@ export default class SyncManager<
     this.deboucedPush(name)
   }
 
+  /**
+   * Setup all collections to be synced with remote changes
+   * and enable automatic pushing changes to the remote source.
+   */
+  public async startAll() {
+    await Promise.all([...this.collections.keys()].map(id =>
+      this.startSync(id)))
+  }
+
   /**
    * Setup a collection to be synced with remote changes
    * and enable automatic pushing changes to the remote source.
@@ -377,6 +386,16 @@ export default class SyncManager<
     })
   }
 
+  /**
+   * Pauses the sync process for all collections.
+   * This means that the collections will not be synced with remote changes
+   * and changes will not automatically be pushed to the remote source.
+   */
+  public async pauseAll() {
+    await Promise.all([...this.collections.keys()].map(id =>
+      this.pauseSync(id)))
+  }
+
   /**
    * Pauses the sync process for a collection.
    * This means that the collection will not be synced with remote changes


Test patch:
diff --git a/packages/base/sync/__tests__/SyncManager.spec.ts b/packages/base/sync/__tests__/SyncManager.spec.ts
index d8c0167..9e7752b 100644
--- a/packages/base/sync/__tests__/SyncManager.spec.ts
+++ b/packages/base/sync/__tests__/SyncManager.spec.ts
@@ -1331,3 +1331,33 @@ it('should return a tuple from getCollection function', () => {
   expect(collectionInstance).toBeInstanceOf(Collection)
   expect(collectionOptions).toEqual({ name: 'test' })
 })
+
+it('should pause and resume sync all collections', async () => {
+  const mockPull = vi.fn<() => Promise<LoadResponse<TestItem>>>().mockResolvedValue({
+    items: [{ id: '1', name: 'Test Item' }],
+  })
+  const mockPush = vi.fn<(options: any, pushParameters: any) => Promise<void>>()
+    .mockResolvedValue()
+
+  const registerRemoteChange = vi.fn()
+  const syncManager = new SyncManager({
+    pull: mockPull,
+    push: mockPush,
+    registerRemoteChange,
+    autostart: false,
+  })
+
+  const col1 = new Collection<TestItem, string, any>()
+  syncManager.addCollection(col1, { name: 'test1' })
+
+  const col2 = new Collection<TestItem, string, any>()
+  syncManager.addCollection(col2, { name: 'test2' })
+
+  expect(registerRemoteChange).toBeCalledTimes(0)
+  await syncManager.startAll()
+  expect(registerRemoteChange).toHaveBeenNthCalledWith(1, { name: 'test1' }, expect.any(Function))
+  expect(registerRemoteChange).toHaveBeenNthCalledWith(2, { name: 'test2' }, expect.any(Function))
+
+  await syncManager.pauseAll()
+  expect(registerRemoteChange).toBeCalledTimes(2)
+})