Just to prime - The following question is about understanding why automerge, indexedDB adapter and svelte-stores behave in a specific way - I'm not searching for code debug and just put the code in a hopefully minimal way so the question can be followed.
Context
I built a svelte-flow app with Svelte 5 (runes) where, as to official documentation, I store nodes and edges as following:
let __nodes = $state.raw([]);
let __edges = $state.raw([]);
All fine - works.
To store changes I use the Automerge for Svelte together with IndexedDB adapter:
import { Repo } from '@automerge/automerge-repo';
repo = new Repo({
network: [new BroadcastChannelNetworkAdapter()],
storage: new IndexedDBStorageAdapter()
});
async function registerOrLoadStore() {
return await repo.create({ docs: {} })
}
As I require multiple documents in repo, I have a class to provide handle access:
class AutomergeHandle {
constructor() {
this._handle = null;
}
async __registerOrLoadHandle() {
this._handle = await registerOrLoadStore();
}
async getHandleDeref() {
let _handle = get(await this.getHandle()); // import { get } from 'svelte/store';
return _handle;
}
async getHandle() {
if (!this._handle) await this.__registerOrLoadHandle();
return this._handle;
}
}
Again, works fine.
Using the handle I sync nodes with Automerge. E.g., when deleting a node I run the following code:
const automergeHandle = new AutomergeHandle();
async function deleteNode(nodeIds) {
(await automergeHandle.getHandle()).change((doc) => {
nodeIds.forEach((nodeId) => { delete doc.docs[nodeId]; });
});
}
Issue
So I can see the code is working and await deleteNode([...]) does correctly remove the item(s) from automerge - All fine. I also can check on the Automerge and see everything is correctly updated - No waiting/race condition whatever missing.
BUT: When I reload my browser (cache cleared full refresh restart) I can see the formerly deleted item (svelte-flow node) reappear with all it's data.
Why this puzzles me:
Actually, I load on Mount the data back from IndexedDB.
Thus, while automerge purged the data in the session it wasn't properly represented in IndexedDB I guess.
And here comes what really screws my mind: If I put a svelte-store deref in front of the automerge action, it suddenly works:
async function deleteNode(nodeIds) {
await automergeHandle.getHandleDeref(); // -> With this line it works!
...
}
So just dereferencing the svelte store of the handle - not even doing anything with it, just doing it - Solves it.
Question
So long text so far, I'm sorry, but really:
Why does automerge IndexedDB adapter only syncs when I do de-reference the svelte-store before?