Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

svelte: provide a way to await the document becoming ready #342

Open
artm opened this issue Apr 21, 2024 · 1 comment
Open

svelte: provide a way to await the document becoming ready #342

artm opened this issue Apr 21, 2024 · 1 comment

Comments

@artm
Copy link

artm commented Apr 21, 2024

The automerge svelte store hides the doc handle so there is no way to do something like

<script>
    // automerge
    import { Repo } from "@automerge/automerge-repo";
    import { IndexedDBStorageAdapter } from "@automerge/automerge-repo-storage-indexeddb";
    import { document, setContextRepo } from "@automerge/automerge-repo-svelte-store";
    // own stuff
    import ActualComponent from "$lib/components/ActualComponent.svelte";

    setContextRepo(
        new Repo({
            storage: new IndexedDBStorageAdapter(),
            network: [],
        }),
    );
    const doc = document( URL_GOT_ELSEWHERE );
</script>

<!-- wait for the document to become ready, imaginary method by analogy with DocHandle -->
{#await doc.whenReady()}
    <div>Loading...</div>
{:then _}
    <!-- ActualComponent may assume doc (the store) is ready and $doc is not undefined -->
    <ActualComponent {doc} />
{/await}

I'm not sure what makes sense API-wise, maybe just expose the doc handle on the object returned from document, so the first line would be something like:

{#await doc.handle().whenReady()}
@artm
Copy link
Author

artm commented Apr 21, 2024

A workaround for now:

<script>
    // automerge
    import { Repo } from "@automerge/automerge-repo";
    import { IndexedDBStorageAdapter } from "@automerge/automerge-repo-storage-indexeddb";
    import { document, setContextRepo } from "@automerge/automerge-repo-svelte-store";
    // own stuff
    import ActualComponent from "$lib/components/ActualComponent.svelte";

    setContextRepo(
        new Repo({
            storage: new IndexedDBStorageAdapter(),
            network: [],
        }),
    );
    const docHandle = repo.find( URL_GOT_ELSEWHERE );
</script>

{#await docHandle.whenReady()}
    <div>Loading...</div>
{:then _} <!-- the async value of whenReady doesn't matter -->
    <!-- I guess the documents are cached in the repo so now the docHandle inside the store will be ready right away -->
    {@const doc = document( URL_GOT_ELSEWHERE )}
    <!-- ActualComponent may assume doc (the store) is ready and $doc is not undefined -->
    <ActualComponent {doc} />
{/await}

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

No branches or pull requests

1 participant