automerge / automerge-repo

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow Svelte's `document` store to explicitely receive the `repo`

romaricpascal opened this issue · comments

The repo accessed by the document Svelte store is grabbed via getContextRepo. This means that the store can only be accessed during a component's initialisation as it uses Svelte's context API. If you try outside of a component's initialisation, Svelte throws an error when trying to access the context.

It'd be great to make the dependency to Svelte's context API optional. That would allow the store to be used in SvelteKit's load function, used to load data before rendering components.

This could be as simple as adding an optional repo parameter to the document function and defaulting it to getContextRepo() if it is not set:

export function document<T>(documentId: AutomergeUrl, repo: Repo) {
  repo ??= getContextRepo()
   // Rest remains unchanged
}

Or the store split into two stores, a document one that requires an explicit repo and a documentFromContextRepo that wraps the first one to automatically set the context repo.

Let me know if that sounds useful, I'd be happy to contribute this 😊

I don't know much about Svelte but it sounds reasonable. Alex wrote the Svelte support so I'll defer to him for an informed opinion here :)

This sounds sensible to me. It's a quick fix, so I'll put in a quick PR to patch it. Thanks for the suggestion @romaricpascal

This shipped! Thanks @romaricpascal, hope it helps.