From 40c7215fc1e9c2e7499eebf232c48e02db85c3cf Mon Sep 17 00:00:00 2001 From: Dawid Kisielewski Date: Tue, 9 Jul 2024 09:31:35 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20Add=20documentions=20about=20com?= =?UTF-8?q?pering=20old=20and=20new=20version=20of=20snapshot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As per this discussion: - https://github.com/share/sharedb/pull/667#discussion_r1654876315 --- docs/middleware/op-submission.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/docs/middleware/op-submission.md b/docs/middleware/op-submission.md index 737fd914e..fc412b6dd 100644 --- a/docs/middleware/op-submission.md +++ b/docs/middleware/op-submission.md @@ -157,3 +157,24 @@ backend.use('apply', (request, next) => { {: .warn :} The `request.$fixup()` method may throw an error, which should be handled appropriately, usually by passing directly to the `next()` callback. + +## Comparing old snapshot version with new version + +Frequently, it becomes necessary to verify the changes made. This can be accomplished by leveraging two hooks, `apply` and `commit`, and creating a snapshot clone within the `apply` hook. + +```js +backend.use('apply', (request, next) => { + // Here use your favorite deep clone implementation + // sharedb team recommends rfdc + // https://github.com/davidmarkclements/rfdc#readme + request.snapshotBeforeApply = clone(request.snapshot); + next(error); +}); + +backend.use('commit', (request, next) => { + // Snapshot without ops and $fixupOps applied is now available as request.snapshotBeforeApply + // Snapshot with ops and $fixupOps applied is still available as request.snapshot + console.log(request.snapshotBeforeApply) + next(error); +}); +``` \ No newline at end of file