-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Allow to preserve previous snapshot version that will be available …
…in commit middleware Occasionally, it is necessary to reference the previous version of a snapshot to execute auxiliary operations within the commit middleware, for example: Let's imagine we have this doc that describes user: ```typescript { id: '123', emailConfirmed: false; } ``` Now we would want to send notification to the admin whenever `emailConfirmed` changed from `false` to `true` At the moment there is no simple way to check in the middlewares if something is changing, apart form trying to apply the ops manually in the `apply` middleware. Something like ```typescript backend.use('apply', function(request, next) { const changedSnapshot = applyOps(clone(request.snapshot), request.op); if( request.snapshot.data.emailConfirmed === false && request.snapshot.data.emailConfirmed === true ) { notifyAdmin(); } }) ``` This approach has few drawbacks: 1. We need to apply ops twice once in this middleware and once in the sharedb itself. 2. It is quite difficult for app to make considerations and mimick the flow of `$fixOps` Solution --- Let's make it possible to opt-in for storing the pre-apply snapshot version in request by setting a `preservePreapplySnapshot` flag to `true` in any middleware that happens before apply. This will allow us to: 1. Have the old and new version of snapshot available in the `commit` middleware 2. We do not have much memory overhead as this is opt-in, so if user never needs it the snapshot would never be cloned. 3. We only need to apply ops once 4. The apply mechanism is using shareDb power house.
- Loading branch information
Showing
3 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters