Skip to content

Instantly share code, notes, and snippets.

@michielbdejong
Last active May 8, 2025 10:47
Show Gist options
  • Save michielbdejong/7987ae65caf2ed0196bd36e9b255a179 to your computer and use it in GitHub Desktop.
Save michielbdejong/7987ae65caf2ed0196bd36e9b255a179 to your computer and use it in GitHub Desktop.
automerge patchInfo source
import { Repo } from '@automerge/automerge-repo';
import { BroadcastChannelNetworkAdapter } from '@automerge/automerge-repo-network-broadcastchannel';
import { NodeFSStorageAdapter } from '@automerge/automerge-repo-storage-nodefs';
const repo1 = new Repo({
network: [new BroadcastChannelNetworkAdapter()],
storage: new NodeFSStorageAdapter('./data1'),
});
const docHandle1 = repo1.create();
docHandle1.on('change', ({ patchInfo }) => {
console.log(`change on docHandle1, patchInfo:`, patchInfo);
});
console.log('docHandle1 created');
const repo2 = new Repo({
network: [new BroadcastChannelNetworkAdapter()],
storage: new NodeFSStorageAdapter('./data2'),
});
const docHandle2 = repo2.find(docHandle1.documentId);
docHandle2.on('change', ({ patchInfo }) => {
console.log(`change on docHandle2, patchInfo:`, patchInfo);
});
console.log('docHandle2 created');
docHandle1.change(d => {
d['foo'] = 'bar';
});
@michielbdejong
Copy link
Author

Save this file as 'test.mjs', then:

npm install @automerge/automerge-repo
npm install @automerge/automerge-repo-network-broadcastchannel
npm install @automerge/automerge-repo-storage-nodefs
node test.mjs

The output will be:

docHandle1 created
docHandle2 created
change on docHandle1, patchInfo: { before: {}, after: { foo: 'bar' }, source: 'change' }
change on docHandle2, patchInfo: { before: {}, after: { foo: 'bar' }, source: 'change' }

I would have expected the source in the change event on docHandle2 to be 'receiveSyncMessage' instead of 'change'?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment