-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document local-only tables and deferred sync pattern
- Loading branch information
Showing
4 changed files
with
59 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Callout } from "nextra-theme-docs"; | ||
|
||
# Patterns | ||
|
||
## Deferred Sync with Local-Only Tables | ||
|
||
Tables with a name prefixed with `_` are local only, which means they are never | ||
synced. It's useful for device-specific or temporal data. | ||
|
||
Imagine editing a JSON representing a rich-text formatted document. Syncing the | ||
whole document on every change would be inefficient. The ideal solution could | ||
be to use some advanced CRDT logic, for example, the | ||
[Peritext](https://www.inkandswitch.com/peritext), but a reliable implementation | ||
doesn't exist yet. | ||
|
||
Fortunately, we can leverage Evolu's local-only tables instead. Saving huge | ||
JSON on every keystroke isn't an issue because Evolu uses Web Workers, | ||
so saving doesn't block the main thread. In React Native, we use | ||
`InteractionManager.runAfterInteractions` (soon). | ||
|
||
<Callout> | ||
Is postMessage slow? No, not really. (It depends.) | ||
[surma.dev/things/is-postmessage-slow](https://surma.dev/things/is-postmessage-slow) | ||
</Callout> | ||
|
||
When we decide it's time to sync, we move data from the local-only table to the | ||
regular table. There is no API for that; just set `isDeleted` to `true` and insert | ||
data into a new table. Evolu batches mutations in microtask and runs it within a | ||
transaction, so there is no chance for data loss. | ||
|
||
```ts | ||
// Both `update` and `create` run within a transaction. | ||
evolu.update("_todo", { id: someId, isDeleted: true }); | ||
// This mutation starts syncing immediately. | ||
evolu.create("todo", { title }); | ||
``` | ||
|
||
The last question is, when should we do that? We can expose an explicit sync | ||
button, but that's not a friendly UX. The better approach is to use a reliable | ||
heuristic to detect the user unit of work. We can leverage page visibility, | ||
a route change, and other techniques. Unfortunately, we can't rely on unload | ||
event because it's unreliable. **Evolu will release a helper for that soon.** |
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
a258841
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
evolu – ./
evolu-evolu.vercel.app
www.evolu.dev
evolu-git-main-evolu.vercel.app
evolu.vercel.app
evolu.dev