An open API service indexing awesome lists of open source software.

https://github.com/chee/solid-automerge

fine-grained reactivity that's consistent across space and time.
https://github.com/chee/solid-automerge

automerge automerge-repo hooks primitives reactivity signals solid solid-js solidjs

Last synced: 2 months ago
JSON representation

fine-grained reactivity that's consistent across space and time.

Awesome Lists containing this project

README

          

> [!important]
> omg

> [!caution]
> This is now situated warm and cozy as part of the Automerge Repo monorepo:
>
> [@automerge/automerge-repo-solid-primitives](https://github.com/automerge/automerge-repo/tree/main/packages/automerge-repo-solid-primitives)

> [!tip]
> You can `pnpm add solid-automerge@npm:@automerge/automerge-repo-solid-primitives` and
> carry on like nothing happened.

> [!important]
> 🎉 omg 🎉

# Solid Automerge

Solid primitives for Automerge .

```sh
pnpm add solid-js @automerge/automerge-repo solid-automerge
```

## useDocument ✨

Get a fine-grained live view of an automerge document from its URL.

When the handle receives changes, it converts the incoming automerge patch ops
to precise solid store updates, giving you fine-grained reactivity that's
consistent across space and time.

Returns `[doc, handle]`.

```ts
useDocument(
() => AutomergeURL,
options?: {repo: Repo}
): [Doc, DocHandle]
```

```tsx
// example
const [url, setURL] = createSignal(props.url)
const [doc, handle] = useDocument(url, {repo})

const inc = () => handle()?.change(d => d.count++)
return {doc()?.count}
```

The `{repo}` option can be left out if you are using [RepoContext](#repocontext).

## createDocumentProjection

Get a fine-grained live view from a signal automerge `DocHandle`.

Underlying primitive for [`useDocument`](#usedocument-).

Works with [`useHandle`](#usehandle).

```ts
createDocumentProjection(() => AutomergeUrl): Doc
```

```tsx
// example
const handle = repo.find(url)
const doc = makeDocumentProjection<{items: {title: string}[]}>(handle)

// subscribes fine-grained to doc.items[1].title
return

{doc.items[1].title}


```

## makeDocumentProjection

Just like `createDocumentProjection`, but without a reactive input.

Underlying primitive for [`createDocumentProjection`](#createDocumentProjection).

```ts
makeDocumentProjection(handle: Handle): Doc
```

```tsx
// example
const handle = repo.find(url)
const doc = makeDocumentProjection<{items: {title: string}[]}>(handle)

// subscribes fine-grained to doc.items[1].title
return

{doc.items[1].title}


```

## useDocHandle

Get a [DocHandle](https://automerge.org/docs/repositories/dochandles/) from the
repo as a
[resource](https://docs.solidjs.com/reference/basic-reactivity/create-resource).

Perfect for handing to `createDocumentProjection`.

```ts
useDocHandle(
() => AnyDocumentId,
options?: {repo: Repo}
): Resource>
```

```tsx
const handle = useDocHandle(id, {repo})
// or
const handle = useDocHandle(id)
```

The `repo` option can be left out if you are using [RepoContext](#repocontext).

## context

If you prefer the context pattern for some reason, you can pass the repo higher
up in your app with `RepoContext`

### `RepoContext`

A convenience context for Automerge-Repo Solid apps. Optional: if you prefer you
can pass a repo as an option to `useDocHandle` and `useDocument`.

```tsx

```

### `useRepo`

Get the repo from the [context](#repocontext).

```ts
useRepo(): Repo
```

#### e.g.

```ts
const repo = useRepo()
```