automerge / automerge-repo

MIT License
419 stars 43 forks source link

[Svelte] Counter works but Text doesn't #335

Closed freerig closed 3 months ago

freerig commented 3 months ago

I use svelte with vite and typescript on Arch btw, and I have a websocket sync server running on localhost:3030

<script lang="ts">
    import { browser } from "$app/environment";
    import { Text } from "@automerge/automerge";
    import { Repo, isValidAutomergeUrl } from "@automerge/automerge-repo";
    import { BrowserWebSocketClientAdapter } from "@automerge/automerge-repo-network-websocket";
    import { IndexedDBStorageAdapter } from "@automerge/automerge-repo-storage-indexeddb";
    import {
        document as getDocumentStore,
        setContextRepo,
    } from "@automerge/automerge-repo-svelte-store";

    type DocType = { text: Text };

    let tempText = "";
    let handle: ReturnType<typeof Repo.prototype.create<DocType>>;
    let documentStore: ReturnType<typeof getDocumentStore<DocType>>;

    if (browser) {
        const repo = new Repo({
            network: [new BrowserWebSocketClientAdapter("ws://localhost:3030")],
            storage: new IndexedDBStorageAdapter(),
        });

        setContextRepo(repo);

        const rootDocUrl = `${document.location.hash.substring(1)}`;
        if (isValidAutomergeUrl(rootDocUrl)) {
            handle = repo.find(rootDocUrl);
        } else {
            handle = repo.create<DocType>({ text: new Text() });    // <<<<<< I talk about this line
        }

        handle.doc().then((v) => console.log(v));

        document.location.hash = handle.url;
        documentStore = getDocumentStore<DocType>(handle.url);
    }
</script>

<div>
    <textarea bind:value={tempText}></textarea>
</div>

This code works until the line I'm talking about (see comment in my code) and produce this error:

Uncaught (in promise) Error: value was not a string
    at assertString (chunk-4QK4UTW2.js?v=60f6f176:1420:11)
    at Object.set (chunk-4QK4UTW2.js?v=60f6f176:823:11)
    at Function.assign (<anonymous>)
    at chunk-4QK4UTW2.js?v=60f6f176:3831:57
    at _change (chunk-4QK4UTW2.js?v=60f6f176:3904:5)
    at from (chunk-4QK4UTW2.js?v=60f6f176:3831:10)
    at from2 (chunk-4QK4UTW2.js?v=60f6f176:4288:10)
    at new DocHandle (chunk-7CG2QION.js?v=60f6f176:8369:13)
    at #getHandle (chunk-7CG2QION.js?v=60f6f176:9967:20)
    at Repo.create (chunk-7CG2QION.js?v=60f6f176:9989:35)

and this one: [HMR][Svelte] Unrecoverable HMR error in <TextEdit>: next update will trigger a full reload

But when I replace new Text() by new Counter(), it works good. I tried removing the document folder in my sync server, or removing the DB storage adapter.

Does someone have an idea of what's going on (I hope this is not a stupid problem) ?

acurrieclark commented 3 months ago

Automerge repo is based on the next Automerge api, which doesn't use Text at all. Instead, you should just set a string value (with type string) and then manipulate it using the splice function.

freerig commented 3 months ago

OK, thanks, I will consider using string.