Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Why Is This Useful
The
SyncManager
is an incredibly powerful addition to SignalDB but it is missing the ability to gracefully pause and resume sync.I maintain a large application made up of many independent modules. Each module uses many collections: some shared, some unique. This application is long-lived. Users leave it running for hours or days at a time. Users have permission to access some or all the modules. All modules are not equally important to the user depending on their role. Users mostly use a few modules and rarely use the rest. Some modules are only used on an annual or quarterly basis.
This change is useful because it allows us to build applications where collections can become active or inactive throughout the lifetime of the session as the user navigates around the application. It would allow us to save both network and server resources by not syncing collections while they're not in use.
Summary of Changes
This is my implementation of sync pause and resume. This PR would introduce the following changes:
registerRemoteChange
takes thecollectionOptions
just likepush
andpull
registerRemoteChange
changes from executing once in the constructor to executing for each collection every time sync resumesregisterRemoteChange
can return a clean-up function to allow for reconnection after a pause (think WebSockets)syncAll
tostartSyncAll
.startSyncAll
fulfills the same purpose except it can be called multiple timesstartSync
which callsregisterRemoteChange
andsync
. This effectively replacessync
.sync
is now private. Collections start in offline mode and only start syncing afterstartSync
is calledpauseSync
to clean up remote-change subscriptions and put the collection into offline mode.pauseSyncAll
to pause sync for all collectionsFor an example of how this change would affect an application please see this diff: obedm503/trellix-offline@90b0300
Practical Example
The initial state of the application:
The application goes offline, web sockets are closed:
The user makes changes while offline but sync is paused:
The application goes back online, web sockets are reopened, and collections sync:
This is very much a draft PR. I know you initially said you're not sure about changing how remote-change subscriptions happen, but I hope I made a good case for the change. I updated the tests so they all pass. I have not updated the documentation but will do so once this is a final PR. Also, I tried to maintain your code style, but please let me know if there are any style changes you wish to make. For the future, I recommend adopting something like Prettier to enforce your style in an automated way.
Related to #947