Skip to content

Conversation

nikgraf
Copy link
Collaborator

@nikgraf nikgraf commented Nov 27, 2024

This PR sync an automerge document (using a hardcoded ID) via encrypted messages.

Initially I tried to build a automerge-repo-network adapter, but since the protocol is hardcoded and requires two roundtrips I gave up on the idea. Especially it would be very tricky to reverse engineer how to fake the first round-trip on sending/receiving an update. Instead I updated for manual manipulation of the document, but still leveraging the automerge-repo infrastructure so we can added cross-tab communication and the storage adapter from it.

Also switched from useState to @xstate/store for document storage since I needed to access the state (space and it's keys) outside of react. Makes sense anyway since soon we want to move to a solution that's also persisted in IndexedDB.

In addition I upgraded to the latest alphas since this maps the main repositories and has several simplifications e.g. awaitingNetwork state has been removed.

More info can be found here: https://automerge.org/docs/repositories/

There are several follow up tasks that I added here: #62

@nikgraf nikgraf force-pushed the sync-data-via-automerge-repo-network-adapter branch from dd012a7 to 12eb243 Compare December 3, 2024 13:26
@nikgraf nikgraf force-pushed the sync-data-via-automerge-repo-network-adapter branch from 12eb243 to 458640a Compare December 3, 2024 13:28
@nikgraf nikgraf marked this pull request as ready for review December 3, 2024 13:49
@nikgraf nikgraf requested a review from cmwhited December 3, 2024 13:49
@nikgraf nikgraf self-assigned this Dec 3, 2024
@nikgraf nikgraf changed the title Sync data via automerge repo network adapter Sync automerge via e2e encrypted ypdates Dec 3, 2024
@nikgraf nikgraf changed the title Sync automerge via e2e encrypted ypdates Sync automerge CRDT datastructure via e2e encrypted updates Dec 3, 2024
Copy link
Collaborator

@cmwhited cmwhited left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a big xstate fan. It looks like most of this impl is in the events app. An open question (not for this PR, specifically) but do you see a pkg that exposes xstate functionality being something useful?

@nikgraf
Copy link
Collaborator Author

nikgraf commented Dec 4, 2024

@cmwhited not sure, secsync https://github.com/nikgraf/secsync/blob/main/packages/secsync/src/createSyncMachine.ts is built on top of a xstate machine and was exported so you could use the machine directly.

The hooks then used it internally https://github.com/nikgraf/secsync/blob/main/packages/secsync-react-automerge/src/useAutomergeSync.ts

There is some overlap between what Effect can do and xState e.g. automatic retries can be mapped as multiple states or be managed by Effect. Here I'm not sure yet what makes more sense. Depends a bit on our requirements.

@nikgraf nikgraf merged commit dbfa6ae into main Dec 4, 2024
1 check passed
@nikgraf nikgraf deleted the sync-data-via-automerge-repo-network-adapter branch December 4, 2024 07:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants