Add deno js release files
This commit is contained in:
parent
58a7a06b75
commit
7d9e564c32
21 changed files with 3300 additions and 460 deletions
17
.github/workflows/advisory-cron.yaml
vendored
17
.github/workflows/advisory-cron.yaml
vendored
|
@ -1,17 +0,0 @@
|
|||
name: Advisories
|
||||
on:
|
||||
schedule:
|
||||
- cron: '0 18 * * *'
|
||||
jobs:
|
||||
cargo-deny:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
checks:
|
||||
- advisories
|
||||
- bans licenses sources
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: EmbarkStudios/cargo-deny-action@v1
|
||||
with:
|
||||
command: check ${{ matrix.checks }}
|
177
.github/workflows/ci.yaml
vendored
177
.github/workflows/ci.yaml
vendored
|
@ -1,177 +0,0 @@
|
|||
name: CI
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
pull_request:
|
||||
branches:
|
||||
- main
|
||||
jobs:
|
||||
fmt:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: 1.66.0
|
||||
default: true
|
||||
components: rustfmt
|
||||
- uses: Swatinem/rust-cache@v1
|
||||
- run: ./scripts/ci/fmt
|
||||
shell: bash
|
||||
|
||||
lint:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: 1.66.0
|
||||
default: true
|
||||
components: clippy
|
||||
- uses: Swatinem/rust-cache@v1
|
||||
- run: ./scripts/ci/lint
|
||||
shell: bash
|
||||
|
||||
docs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: 1.66.0
|
||||
default: true
|
||||
- uses: Swatinem/rust-cache@v1
|
||||
- name: Build rust docs
|
||||
run: ./scripts/ci/rust-docs
|
||||
shell: bash
|
||||
- name: Install doxygen
|
||||
run: sudo apt-get install -y doxygen
|
||||
shell: bash
|
||||
|
||||
cargo-deny:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
checks:
|
||||
- advisories
|
||||
- bans licenses sources
|
||||
continue-on-error: ${{ matrix.checks == 'advisories' }}
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: EmbarkStudios/cargo-deny-action@v1
|
||||
with:
|
||||
arguments: '--manifest-path ./rust/Cargo.toml'
|
||||
command: check ${{ matrix.checks }}
|
||||
|
||||
wasm_tests:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Install wasm-bindgen-cli
|
||||
run: cargo install wasm-bindgen-cli wasm-opt
|
||||
- name: Install wasm32 target
|
||||
run: rustup target add wasm32-unknown-unknown
|
||||
- name: run tests
|
||||
run: ./scripts/ci/wasm_tests
|
||||
deno_tests:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: denoland/setup-deno@v1
|
||||
with:
|
||||
deno-version: v1.x
|
||||
- name: Install wasm-bindgen-cli
|
||||
run: cargo install wasm-bindgen-cli wasm-opt
|
||||
- name: Install wasm32 target
|
||||
run: rustup target add wasm32-unknown-unknown
|
||||
- name: run tests
|
||||
run: ./scripts/ci/deno_tests
|
||||
|
||||
js_fmt:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: install
|
||||
run: yarn global add prettier
|
||||
- name: format
|
||||
run: prettier -c javascript/.prettierrc javascript
|
||||
|
||||
js_tests:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Install wasm-bindgen-cli
|
||||
run: cargo install wasm-bindgen-cli wasm-opt
|
||||
- name: Install wasm32 target
|
||||
run: rustup target add wasm32-unknown-unknown
|
||||
- name: run tests
|
||||
run: ./scripts/ci/js_tests
|
||||
|
||||
cmake_build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: 1.66.0
|
||||
default: true
|
||||
- uses: Swatinem/rust-cache@v1
|
||||
- name: Install CMocka
|
||||
run: sudo apt-get install -y libcmocka-dev
|
||||
- name: Install/update CMake
|
||||
uses: jwlawson/actions-setup-cmake@v1.12
|
||||
with:
|
||||
cmake-version: latest
|
||||
- name: Build and test C bindings
|
||||
run: ./scripts/ci/cmake-build Release Static
|
||||
shell: bash
|
||||
|
||||
linux:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
toolchain:
|
||||
- 1.66.0
|
||||
- nightly
|
||||
continue-on-error: ${{ matrix.toolchain == 'nightly' }}
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: ${{ matrix.toolchain }}
|
||||
default: true
|
||||
- uses: Swatinem/rust-cache@v1
|
||||
- run: ./scripts/ci/build-test
|
||||
shell: bash
|
||||
|
||||
macos:
|
||||
runs-on: macos-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: 1.66.0
|
||||
default: true
|
||||
- uses: Swatinem/rust-cache@v1
|
||||
- run: ./scripts/ci/build-test
|
||||
shell: bash
|
||||
|
||||
windows:
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: 1.66.0
|
||||
default: true
|
||||
- uses: Swatinem/rust-cache@v1
|
||||
- run: ./scripts/ci/build-test
|
||||
shell: bash
|
52
.github/workflows/docs.yaml
vendored
52
.github/workflows/docs.yaml
vendored
|
@ -1,52 +0,0 @@
|
|||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
|
||||
name: Documentation
|
||||
|
||||
jobs:
|
||||
deploy-docs:
|
||||
concurrency: deploy-docs
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
- name: Toolchain
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: stable
|
||||
override: true
|
||||
|
||||
- name: Cache
|
||||
uses: Swatinem/rust-cache@v1
|
||||
|
||||
- name: Clean docs dir
|
||||
run: rm -rf docs
|
||||
shell: bash
|
||||
|
||||
- name: Clean Rust docs dir
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: clean
|
||||
args: --manifest-path ./rust/Cargo.toml --doc
|
||||
|
||||
- name: Build Rust docs
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: doc
|
||||
args: --manifest-path ./rust/Cargo.toml --workspace --all-features --no-deps
|
||||
|
||||
- name: Move Rust docs
|
||||
run: mkdir -p docs && mv rust/target/doc/* docs/.
|
||||
shell: bash
|
||||
|
||||
- name: Configure root page
|
||||
run: echo '<meta http-equiv="refresh" content="0; url=automerge">' > docs/index.html
|
||||
|
||||
- name: Deploy docs
|
||||
uses: peaceiris/actions-gh-pages@v3
|
||||
with:
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
publish_dir: ./docs
|
214
.github/workflows/release.yaml
vendored
214
.github/workflows/release.yaml
vendored
|
@ -1,214 +0,0 @@
|
|||
name: Release
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
|
||||
jobs:
|
||||
check_if_wasm_version_upgraded:
|
||||
name: Check if WASM version has been upgraded
|
||||
runs-on: ubuntu-latest
|
||||
outputs:
|
||||
wasm_version: ${{ steps.version-updated.outputs.current-package-version }}
|
||||
wasm_has_updated: ${{ steps.version-updated.outputs.has-updated }}
|
||||
steps:
|
||||
- uses: JiPaix/package-json-updated-action@v1.0.5
|
||||
id: version-updated
|
||||
with:
|
||||
path: rust/automerge-wasm/package.json
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
publish-wasm:
|
||||
name: Publish WASM package
|
||||
runs-on: ubuntu-latest
|
||||
needs:
|
||||
- check_if_wasm_version_upgraded
|
||||
# We create release only if the version in the package.json has been upgraded
|
||||
if: needs.check_if_wasm_version_upgraded.outputs.wasm_has_updated == 'true'
|
||||
steps:
|
||||
- uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: '16.x'
|
||||
registry-url: 'https://registry.npmjs.org'
|
||||
- uses: denoland/setup-deno@v1
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
fetch-depth: 0
|
||||
ref: ${{ github.ref }}
|
||||
- name: Get rid of local github workflows
|
||||
run: rm -r .github/workflows
|
||||
- name: Remove tmp_branch if it exists
|
||||
run: git push origin :tmp_branch || true
|
||||
- run: git checkout -b tmp_branch
|
||||
- name: Install wasm-bindgen-cli
|
||||
run: cargo install wasm-bindgen-cli wasm-opt
|
||||
- name: Install wasm32 target
|
||||
run: rustup target add wasm32-unknown-unknown
|
||||
- name: run wasm js tests
|
||||
id: wasm_js_tests
|
||||
run: ./scripts/ci/wasm_tests
|
||||
- name: run wasm deno tests
|
||||
id: wasm_deno_tests
|
||||
run: ./scripts/ci/deno_tests
|
||||
- name: build release
|
||||
id: build_release
|
||||
run: |
|
||||
npm --prefix $GITHUB_WORKSPACE/rust/automerge-wasm run release
|
||||
- name: Collate deno release files
|
||||
if: steps.wasm_js_tests.outcome == 'success' && steps.wasm_deno_tests.outcome == 'success'
|
||||
run: |
|
||||
mkdir $GITHUB_WORKSPACE/deno_wasm_dist
|
||||
cp $GITHUB_WORKSPACE/rust/automerge-wasm/deno/* $GITHUB_WORKSPACE/deno_wasm_dist
|
||||
cp $GITHUB_WORKSPACE/rust/automerge-wasm/index.d.ts $GITHUB_WORKSPACE/deno_wasm_dist
|
||||
cp $GITHUB_WORKSPACE/rust/automerge-wasm/README.md $GITHUB_WORKSPACE/deno_wasm_dist
|
||||
cp $GITHUB_WORKSPACE/rust/automerge-wasm/LICENSE $GITHUB_WORKSPACE/deno_wasm_dist
|
||||
sed -i '1i /// <reference types="./index.d.ts" />' $GITHUB_WORKSPACE/deno_wasm_dist/automerge_wasm.js
|
||||
- name: Create npm release
|
||||
if: steps.wasm_js_tests.outcome == 'success' && steps.wasm_deno_tests.outcome == 'success'
|
||||
run: |
|
||||
if [ "$(npm --prefix $GITHUB_WORKSPACE/rust/automerge-wasm show . version)" = "$VERSION" ]; then
|
||||
echo "This version is already published"
|
||||
exit 0
|
||||
fi
|
||||
EXTRA_ARGS="--access public"
|
||||
if [[ $VERSION == *"alpha."* ]] || [[ $VERSION == *"beta."* ]] || [[ $VERSION == *"rc."* ]]; then
|
||||
echo "Is pre-release version"
|
||||
EXTRA_ARGS="$EXTRA_ARGS --tag next"
|
||||
fi
|
||||
if [ "$NODE_AUTH_TOKEN" = "" ]; then
|
||||
echo "Can't publish on NPM, You need a NPM_TOKEN secret."
|
||||
false
|
||||
fi
|
||||
npm publish $GITHUB_WORKSPACE/rust/automerge-wasm $EXTRA_ARGS
|
||||
env:
|
||||
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
|
||||
VERSION: ${{ needs.check_if_wasm_version_upgraded.outputs.wasm_version }}
|
||||
- name: Commit wasm deno release files
|
||||
run: |
|
||||
git config --global user.name "actions"
|
||||
git config --global user.email actions@github.com
|
||||
git add $GITHUB_WORKSPACE/deno_wasm_dist
|
||||
git commit -am "Add deno release files"
|
||||
git push origin tmp_branch
|
||||
- name: Tag wasm release
|
||||
if: steps.wasm_js_tests.outcome == 'success' && steps.wasm_deno_tests.outcome == 'success'
|
||||
uses: softprops/action-gh-release@v1
|
||||
with:
|
||||
name: Automerge Wasm v${{ needs.check_if_wasm_version_upgraded.outputs.wasm_version }}
|
||||
tag_name: js/automerge-wasm-${{ needs.check_if_wasm_version_upgraded.outputs.wasm_version }}
|
||||
target_commitish: tmp_branch
|
||||
generate_release_notes: false
|
||||
draft: false
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
- name: Remove tmp_branch
|
||||
run: git push origin :tmp_branch
|
||||
check_if_js_version_upgraded:
|
||||
name: Check if JS version has been upgraded
|
||||
runs-on: ubuntu-latest
|
||||
outputs:
|
||||
js_version: ${{ steps.version-updated.outputs.current-package-version }}
|
||||
js_has_updated: ${{ steps.version-updated.outputs.has-updated }}
|
||||
steps:
|
||||
- uses: JiPaix/package-json-updated-action@v1.0.5
|
||||
id: version-updated
|
||||
with:
|
||||
path: javascript/package.json
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
publish-js:
|
||||
name: Publish JS package
|
||||
runs-on: ubuntu-latest
|
||||
needs:
|
||||
- check_if_js_version_upgraded
|
||||
- check_if_wasm_version_upgraded
|
||||
- publish-wasm
|
||||
# We create release only if the version in the package.json has been upgraded and after the WASM release
|
||||
if: |
|
||||
(always() && ! cancelled()) &&
|
||||
(needs.publish-wasm.result == 'success' || needs.publish-wasm.result == 'skipped') &&
|
||||
needs.check_if_js_version_upgraded.outputs.js_has_updated == 'true'
|
||||
steps:
|
||||
- uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: '16.x'
|
||||
registry-url: 'https://registry.npmjs.org'
|
||||
- uses: denoland/setup-deno@v1
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
fetch-depth: 0
|
||||
ref: ${{ github.ref }}
|
||||
- name: Get rid of local github workflows
|
||||
run: rm -r .github/workflows
|
||||
- name: Remove js_tmp_branch if it exists
|
||||
run: git push origin :js_tmp_branch || true
|
||||
- run: git checkout -b js_tmp_branch
|
||||
- name: check js formatting
|
||||
run: |
|
||||
yarn global add prettier
|
||||
prettier -c javascript/.prettierrc javascript
|
||||
- name: run js tests
|
||||
id: js_tests
|
||||
run: |
|
||||
cargo install wasm-bindgen-cli wasm-opt
|
||||
rustup target add wasm32-unknown-unknown
|
||||
./scripts/ci/js_tests
|
||||
- name: build js release
|
||||
id: build_release
|
||||
run: |
|
||||
npm --prefix $GITHUB_WORKSPACE/javascript run build
|
||||
- name: build js deno release
|
||||
id: build_deno_release
|
||||
run: |
|
||||
VERSION=$WASM_VERSION npm --prefix $GITHUB_WORKSPACE/javascript run deno:build
|
||||
env:
|
||||
WASM_VERSION: ${{ needs.check_if_wasm_version_upgraded.outputs.wasm_version }}
|
||||
- name: run deno tests
|
||||
id: deno_tests
|
||||
run: |
|
||||
npm --prefix $GITHUB_WORKSPACE/javascript run deno:test
|
||||
- name: Collate deno release files
|
||||
if: steps.js_tests.outcome == 'success' && steps.deno_tests.outcome == 'success'
|
||||
run: |
|
||||
mkdir $GITHUB_WORKSPACE/deno_js_dist
|
||||
cp $GITHUB_WORKSPACE/javascript/deno_dist/* $GITHUB_WORKSPACE/deno_js_dist
|
||||
- name: Create npm release
|
||||
if: steps.js_tests.outcome == 'success' && steps.deno_tests.outcome == 'success'
|
||||
run: |
|
||||
if [ "$(npm --prefix $GITHUB_WORKSPACE/javascript show . version)" = "$VERSION" ]; then
|
||||
echo "This version is already published"
|
||||
exit 0
|
||||
fi
|
||||
EXTRA_ARGS="--access public"
|
||||
if [[ $VERSION == *"alpha."* ]] || [[ $VERSION == *"beta."* ]] || [[ $VERSION == *"rc."* ]]; then
|
||||
echo "Is pre-release version"
|
||||
EXTRA_ARGS="$EXTRA_ARGS --tag next"
|
||||
fi
|
||||
if [ "$NODE_AUTH_TOKEN" = "" ]; then
|
||||
echo "Can't publish on NPM, You need a NPM_TOKEN secret."
|
||||
false
|
||||
fi
|
||||
npm publish $GITHUB_WORKSPACE/javascript $EXTRA_ARGS
|
||||
env:
|
||||
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
|
||||
VERSION: ${{ needs.check_if_js_version_upgraded.outputs.js_version }}
|
||||
- name: Commit js deno release files
|
||||
run: |
|
||||
git config --global user.name "actions"
|
||||
git config --global user.email actions@github.com
|
||||
git add $GITHUB_WORKSPACE/deno_js_dist
|
||||
git commit -am "Add deno js release files"
|
||||
git push origin js_tmp_branch
|
||||
- name: Tag JS release
|
||||
if: steps.js_tests.outcome == 'success' && steps.deno_tests.outcome == 'success'
|
||||
uses: softprops/action-gh-release@v1
|
||||
with:
|
||||
name: Automerge v${{ needs.check_if_js_version_upgraded.outputs.js_version }}
|
||||
tag_name: js/automerge-${{ needs.check_if_js_version_upgraded.outputs.js_version }}
|
||||
target_commitish: js_tmp_branch
|
||||
generate_release_notes: false
|
||||
draft: false
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
- name: Remove js_tmp_branch
|
||||
run: git push origin :js_tmp_branch
|
10
deno_js_dist/LICENSE
Normal file
10
deno_js_dist/LICENSE
Normal file
|
@ -0,0 +1,10 @@
|
|||
MIT License
|
||||
|
||||
Copyright 2022, Ink & Switch LLC
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
109
deno_js_dist/README.md
Normal file
109
deno_js_dist/README.md
Normal file
|
@ -0,0 +1,109 @@
|
|||
## Automerge
|
||||
|
||||
Automerge is a library of data structures for building collaborative
|
||||
applications, this package is the javascript implementation.
|
||||
|
||||
Detailed documentation is available at [automerge.org](http://automerge.org/)
|
||||
but see the following for a short getting started guid.
|
||||
|
||||
## Quickstart
|
||||
|
||||
First, install the library.
|
||||
|
||||
```
|
||||
yarn add @automerge/automerge
|
||||
```
|
||||
|
||||
If you're writing a `node` application, you can skip straight to [Make some
|
||||
data](#make-some-data). If you're in a browser you need a bundler
|
||||
|
||||
### Bundler setup
|
||||
|
||||
`@automerge/automerge` is a wrapper around a core library which is written in
|
||||
rust, compiled to WebAssembly and distributed as a separate package called
|
||||
`@automerge/automerge-wasm`. Browsers don't currently support WebAssembly
|
||||
modules taking part in ESM module imports, so you must use a bundler to import
|
||||
`@automerge/automerge` in the browser. There are a lot of bundlers out there, we
|
||||
have examples for common bundlers in the `examples` folder. Here is a short
|
||||
example using Webpack 5.
|
||||
|
||||
Assuming a standard setup of a new webpack project, you'll need to enable the
|
||||
`asyncWebAssembly` experiment. In a typical webpack project that means adding
|
||||
something like this to `webpack.config.js`
|
||||
|
||||
```javascript
|
||||
module.exports = {
|
||||
...
|
||||
experiments: { asyncWebAssembly: true },
|
||||
performance: { // we dont want the wasm blob to generate warnings
|
||||
hints: false,
|
||||
maxEntrypointSize: 512000,
|
||||
maxAssetSize: 512000
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
### Make some data
|
||||
|
||||
Automerge allows to separate threads of execution to make changes to some data
|
||||
and always be able to merge their changes later.
|
||||
|
||||
```javascript
|
||||
import * as automerge from "@automerge/automerge"
|
||||
import * as assert from "assert"
|
||||
|
||||
let doc1 = automerge.from({
|
||||
tasks: [
|
||||
{ description: "feed fish", done: false },
|
||||
{ description: "water plants", done: false },
|
||||
],
|
||||
})
|
||||
|
||||
// Create a new thread of execution
|
||||
let doc2 = automerge.clone(doc1)
|
||||
|
||||
// Now we concurrently make changes to doc1 and doc2
|
||||
|
||||
// Complete a task in doc2
|
||||
doc2 = automerge.change(doc2, d => {
|
||||
d.tasks[0].done = true
|
||||
})
|
||||
|
||||
// Add a task in doc1
|
||||
doc1 = automerge.change(doc1, d => {
|
||||
d.tasks.push({
|
||||
description: "water fish",
|
||||
done: false,
|
||||
})
|
||||
})
|
||||
|
||||
// Merge changes from both docs
|
||||
doc1 = automerge.merge(doc1, doc2)
|
||||
doc2 = automerge.merge(doc2, doc1)
|
||||
|
||||
// Both docs are merged and identical
|
||||
assert.deepEqual(doc1, {
|
||||
tasks: [
|
||||
{ description: "feed fish", done: true },
|
||||
{ description: "water plants", done: false },
|
||||
{ description: "water fish", done: false },
|
||||
],
|
||||
})
|
||||
|
||||
assert.deepEqual(doc2, {
|
||||
tasks: [
|
||||
{ description: "feed fish", done: true },
|
||||
{ description: "water plants", done: false },
|
||||
{ description: "water fish", done: false },
|
||||
],
|
||||
})
|
||||
```
|
||||
|
||||
## Development
|
||||
|
||||
See [HACKING.md](./HACKING.md)
|
||||
|
||||
## Meta
|
||||
|
||||
Copyright 2017–present, the Automerge contributors. Released under the terms of the
|
||||
MIT license (see `LICENSE`).
|
100
deno_js_dist/conflicts.ts
Normal file
100
deno_js_dist/conflicts.ts
Normal file
|
@ -0,0 +1,100 @@
|
|||
import { Counter, type AutomergeValue } from "./types.ts"
|
||||
import { Text } from "./text.ts"
|
||||
import { type AutomergeValue as UnstableAutomergeValue } from "./unstable_types.ts"
|
||||
import { type Target, Text1Target, Text2Target } from "./proxies.ts"
|
||||
import { mapProxy, listProxy, ValueType } from "./proxies.ts"
|
||||
import type { Prop, ObjID } from "https://deno.land/x/automerge_wasm@0.1.23/index.d.ts";
|
||||
import { Automerge } from "https://deno.land/x/automerge_wasm@0.1.23/automerge_wasm.js";
|
||||
|
||||
export type ConflictsF<T extends Target> = { [key: string]: ValueType<T> }
|
||||
export type Conflicts = ConflictsF<Text1Target>
|
||||
export type UnstableConflicts = ConflictsF<Text2Target>
|
||||
|
||||
export function stableConflictAt(
|
||||
context: Automerge,
|
||||
objectId: ObjID,
|
||||
prop: Prop
|
||||
): Conflicts | undefined {
|
||||
return conflictAt<Text1Target>(
|
||||
context,
|
||||
objectId,
|
||||
prop,
|
||||
true,
|
||||
(context: Automerge, conflictId: ObjID): AutomergeValue => {
|
||||
return new Text(context.text(conflictId))
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
export function unstableConflictAt(
|
||||
context: Automerge,
|
||||
objectId: ObjID,
|
||||
prop: Prop
|
||||
): UnstableConflicts | undefined {
|
||||
return conflictAt<Text2Target>(
|
||||
context,
|
||||
objectId,
|
||||
prop,
|
||||
true,
|
||||
(context: Automerge, conflictId: ObjID): UnstableAutomergeValue => {
|
||||
return context.text(conflictId)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
function conflictAt<T extends Target>(
|
||||
context: Automerge,
|
||||
objectId: ObjID,
|
||||
prop: Prop,
|
||||
textV2: boolean,
|
||||
handleText: (a: Automerge, conflictId: ObjID) => ValueType<T>
|
||||
): ConflictsF<T> | undefined {
|
||||
const values = context.getAll(objectId, prop)
|
||||
if (values.length <= 1) {
|
||||
return
|
||||
}
|
||||
const result: ConflictsF<T> = {}
|
||||
for (const fullVal of values) {
|
||||
switch (fullVal[0]) {
|
||||
case "map":
|
||||
result[fullVal[1]] = mapProxy<T>(
|
||||
context,
|
||||
fullVal[1],
|
||||
textV2,
|
||||
[prop],
|
||||
true
|
||||
)
|
||||
break
|
||||
case "list":
|
||||
result[fullVal[1]] = listProxy<T>(
|
||||
context,
|
||||
fullVal[1],
|
||||
textV2,
|
||||
[prop],
|
||||
true
|
||||
)
|
||||
break
|
||||
case "text":
|
||||
result[fullVal[1]] = handleText(context, fullVal[1] as ObjID)
|
||||
break
|
||||
case "str":
|
||||
case "uint":
|
||||
case "int":
|
||||
case "f64":
|
||||
case "boolean":
|
||||
case "bytes":
|
||||
case "null":
|
||||
result[fullVal[2]] = fullVal[1] as ValueType<T>
|
||||
break
|
||||
case "counter":
|
||||
result[fullVal[2]] = new Counter(fullVal[1]) as ValueType<T>
|
||||
break
|
||||
case "timestamp":
|
||||
result[fullVal[2]] = new Date(fullVal[1]) as ValueType<T>
|
||||
break
|
||||
default:
|
||||
throw RangeError(`datatype ${fullVal[0]} unimplemented`)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
12
deno_js_dist/constants.ts
Normal file
12
deno_js_dist/constants.ts
Normal file
|
@ -0,0 +1,12 @@
|
|||
// Properties of the document root object
|
||||
|
||||
export const STATE = Symbol.for("_am_meta") // symbol used to hide application metadata on automerge objects
|
||||
export const TRACE = Symbol.for("_am_trace") // used for debugging
|
||||
export const OBJECT_ID = Symbol.for("_am_objectId") // symbol used to hide the object id on automerge objects
|
||||
export const IS_PROXY = Symbol.for("_am_isProxy") // symbol used to test if the document is a proxy object
|
||||
|
||||
export const UINT = Symbol.for("_am_uint")
|
||||
export const INT = Symbol.for("_am_int")
|
||||
export const F64 = Symbol.for("_am_f64")
|
||||
export const COUNTER = Symbol.for("_am_counter")
|
||||
export const TEXT = Symbol.for("_am_text")
|
107
deno_js_dist/counter.ts
Normal file
107
deno_js_dist/counter.ts
Normal file
|
@ -0,0 +1,107 @@
|
|||
import { Automerge, type ObjID, type Prop } from "https://deno.land/x/automerge_wasm@0.1.23/automerge_wasm.js";
|
||||
import { COUNTER } from "./constants.ts"
|
||||
/**
|
||||
* The most basic CRDT: an integer value that can be changed only by
|
||||
* incrementing and decrementing. Since addition of integers is commutative,
|
||||
* the value trivially converges.
|
||||
*/
|
||||
export class Counter {
|
||||
value: number
|
||||
|
||||
constructor(value?: number) {
|
||||
this.value = value || 0
|
||||
Reflect.defineProperty(this, COUNTER, { value: true })
|
||||
}
|
||||
|
||||
/**
|
||||
* A peculiar JavaScript language feature from its early days: if the object
|
||||
* `x` has a `valueOf()` method that returns a number, you can use numerical
|
||||
* operators on the object `x` directly, such as `x + 1` or `x < 4`.
|
||||
* This method is also called when coercing a value to a string by
|
||||
* concatenating it with another string, as in `x + ''`.
|
||||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf
|
||||
*/
|
||||
valueOf(): number {
|
||||
return this.value
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the counter value as a decimal string. If `x` is a counter object,
|
||||
* this method is called e.g. when you do `['value: ', x].join('')` or when
|
||||
* you use string interpolation: `value: ${x}`.
|
||||
*/
|
||||
toString(): string {
|
||||
return this.valueOf().toString()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the counter value, so that a JSON serialization of an Automerge
|
||||
* document represents the counter simply as an integer.
|
||||
*/
|
||||
toJSON(): number {
|
||||
return this.value
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An instance of this class is used when a counter is accessed within a change
|
||||
* callback.
|
||||
*/
|
||||
class WriteableCounter extends Counter {
|
||||
context: Automerge
|
||||
path: Prop[]
|
||||
objectId: ObjID
|
||||
key: Prop
|
||||
|
||||
constructor(
|
||||
value: number,
|
||||
context: Automerge,
|
||||
path: Prop[],
|
||||
objectId: ObjID,
|
||||
key: Prop
|
||||
) {
|
||||
super(value)
|
||||
this.context = context
|
||||
this.path = path
|
||||
this.objectId = objectId
|
||||
this.key = key
|
||||
}
|
||||
|
||||
/**
|
||||
* Increases the value of the counter by `delta`. If `delta` is not given,
|
||||
* increases the value of the counter by 1.
|
||||
*/
|
||||
increment(delta: number): number {
|
||||
delta = typeof delta === "number" ? delta : 1
|
||||
this.context.increment(this.objectId, this.key, delta)
|
||||
this.value += delta
|
||||
return this.value
|
||||
}
|
||||
|
||||
/**
|
||||
* Decreases the value of the counter by `delta`. If `delta` is not given,
|
||||
* decreases the value of the counter by 1.
|
||||
*/
|
||||
decrement(delta: number): number {
|
||||
return this.increment(typeof delta === "number" ? -delta : -1)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an instance of `WriteableCounter` for use in a change callback.
|
||||
* `context` is the proxy context that keeps track of the mutations.
|
||||
* `objectId` is the ID of the object containing the counter, and `key` is
|
||||
* the property name (key in map, or index in list) where the counter is
|
||||
* located.
|
||||
*/
|
||||
export function getWriteableCounter(
|
||||
value: number,
|
||||
context: Automerge,
|
||||
path: Prop[],
|
||||
objectId: ObjID,
|
||||
key: Prop
|
||||
): WriteableCounter {
|
||||
return new WriteableCounter(value, context, path, objectId, key)
|
||||
}
|
||||
|
||||
//module.exports = { Counter, getWriteableCounter }
|
242
deno_js_dist/index.ts
Normal file
242
deno_js_dist/index.ts
Normal file
|
@ -0,0 +1,242 @@
|
|||
/**
|
||||
* # Automerge
|
||||
*
|
||||
* This library provides the core automerge data structure and sync algorithms.
|
||||
* Other libraries can be built on top of this one which provide IO and
|
||||
* persistence.
|
||||
*
|
||||
* An automerge document can be though of an immutable POJO (plain old javascript
|
||||
* object) which `automerge` tracks the history of, allowing it to be merged with
|
||||
* any other automerge document.
|
||||
*
|
||||
* ## Creating and modifying a document
|
||||
*
|
||||
* You can create a document with {@link init} or {@link from} and then make
|
||||
* changes to it with {@link change}, you can merge two documents with {@link
|
||||
* merge}.
|
||||
*
|
||||
* ```ts
|
||||
* import * as automerge from "@automerge/automerge"
|
||||
*
|
||||
* type DocType = {ideas: Array<automerge.Text>}
|
||||
*
|
||||
* let doc1 = automerge.init<DocType>()
|
||||
* doc1 = automerge.change(doc1, d => {
|
||||
* d.ideas = [new automerge.Text("an immutable document")]
|
||||
* })
|
||||
*
|
||||
* let doc2 = automerge.init<DocType>()
|
||||
* doc2 = automerge.merge(doc2, automerge.clone(doc1))
|
||||
* doc2 = automerge.change<DocType>(doc2, d => {
|
||||
* d.ideas.push(new automerge.Text("which records it's history"))
|
||||
* })
|
||||
*
|
||||
* // Note the `automerge.clone` call, see the "cloning" section of this readme for
|
||||
* // more detail
|
||||
* doc1 = automerge.merge(doc1, automerge.clone(doc2))
|
||||
* doc1 = automerge.change(doc1, d => {
|
||||
* d.ideas[0].deleteAt(13, 8)
|
||||
* d.ideas[0].insertAt(13, "object")
|
||||
* })
|
||||
*
|
||||
* let doc3 = automerge.merge(doc1, doc2)
|
||||
* // doc3 is now {ideas: ["an immutable object", "which records it's history"]}
|
||||
* ```
|
||||
*
|
||||
* ## Applying changes from another document
|
||||
*
|
||||
* You can get a representation of the result of the last {@link change} you made
|
||||
* to a document with {@link getLastLocalChange} and you can apply that change to
|
||||
* another document using {@link applyChanges}.
|
||||
*
|
||||
* If you need to get just the changes which are in one document but not in another
|
||||
* you can use {@link getHeads} to get the heads of the document without the
|
||||
* changes and then {@link getMissingDeps}, passing the result of {@link getHeads}
|
||||
* on the document with the changes.
|
||||
*
|
||||
* ## Saving and loading documents
|
||||
*
|
||||
* You can {@link save} a document to generate a compresed binary representation of
|
||||
* the document which can be loaded with {@link load}. If you have a document which
|
||||
* you have recently made changes to you can generate recent changes with {@link
|
||||
* saveIncremental}, this will generate all the changes since you last called
|
||||
* `saveIncremental`, the changes generated can be applied to another document with
|
||||
* {@link loadIncremental}.
|
||||
*
|
||||
* ## Viewing different versions of a document
|
||||
*
|
||||
* Occasionally you may wish to explicitly step to a different point in a document
|
||||
* history. One common reason to do this is if you need to obtain a set of changes
|
||||
* which take the document from one state to another in order to send those changes
|
||||
* to another peer (or to save them somewhere). You can use {@link view} to do this.
|
||||
*
|
||||
* ```ts
|
||||
* import * as automerge from "@automerge/automerge"
|
||||
* import * as assert from "assert"
|
||||
*
|
||||
* let doc = automerge.from({
|
||||
* key1: "value1",
|
||||
* })
|
||||
*
|
||||
* // Make a clone of the document at this point, maybe this is actually on another
|
||||
* // peer.
|
||||
* let doc2 = automerge.clone < any > doc
|
||||
*
|
||||
* let heads = automerge.getHeads(doc)
|
||||
*
|
||||
* doc =
|
||||
* automerge.change <
|
||||
* any >
|
||||
* (doc,
|
||||
* d => {
|
||||
* d.key2 = "value2"
|
||||
* })
|
||||
*
|
||||
* doc =
|
||||
* automerge.change <
|
||||
* any >
|
||||
* (doc,
|
||||
* d => {
|
||||
* d.key3 = "value3"
|
||||
* })
|
||||
*
|
||||
* // At this point we've generated two separate changes, now we want to send
|
||||
* // just those changes to someone else
|
||||
*
|
||||
* // view is a cheap reference based copy of a document at a given set of heads
|
||||
* let before = automerge.view(doc, heads)
|
||||
*
|
||||
* // This view doesn't show the last two changes in the document state
|
||||
* assert.deepEqual(before, {
|
||||
* key1: "value1",
|
||||
* })
|
||||
*
|
||||
* // Get the changes to send to doc2
|
||||
* let changes = automerge.getChanges(before, doc)
|
||||
*
|
||||
* // Apply the changes at doc2
|
||||
* doc2 = automerge.applyChanges < any > (doc2, changes)[0]
|
||||
* assert.deepEqual(doc2, {
|
||||
* key1: "value1",
|
||||
* key2: "value2",
|
||||
* key3: "value3",
|
||||
* })
|
||||
* ```
|
||||
*
|
||||
* If you have a {@link view} of a document which you want to make changes to you
|
||||
* can {@link clone} the viewed document.
|
||||
*
|
||||
* ## Syncing
|
||||
*
|
||||
* The sync protocol is stateful. This means that we start by creating a {@link
|
||||
* SyncState} for each peer we are communicating with using {@link initSyncState}.
|
||||
* Then we generate a message to send to the peer by calling {@link
|
||||
* generateSyncMessage}. When we receive a message from the peer we call {@link
|
||||
* receiveSyncMessage}. Here's a simple example of a loop which just keeps two
|
||||
* peers in sync.
|
||||
*
|
||||
* ```ts
|
||||
* let sync1 = automerge.initSyncState()
|
||||
* let msg: Uint8Array | null
|
||||
* ;[sync1, msg] = automerge.generateSyncMessage(doc1, sync1)
|
||||
*
|
||||
* while (true) {
|
||||
* if (msg != null) {
|
||||
* network.send(msg)
|
||||
* }
|
||||
* let resp: Uint8Array =
|
||||
* (network.receive()[(doc1, sync1, _ignore)] =
|
||||
* automerge.receiveSyncMessage(doc1, sync1, resp)[(sync1, msg)] =
|
||||
* automerge.generateSyncMessage(doc1, sync1))
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* ## Conflicts
|
||||
*
|
||||
* The only time conflicts occur in automerge documents is in concurrent
|
||||
* assignments to the same key in an object. In this case automerge
|
||||
* deterministically chooses an arbitrary value to present to the application but
|
||||
* you can examine the conflicts using {@link getConflicts}.
|
||||
*
|
||||
* ```
|
||||
* import * as automerge from "@automerge/automerge"
|
||||
*
|
||||
* type Profile = {
|
||||
* pets: Array<{name: string, type: string}>
|
||||
* }
|
||||
*
|
||||
* let doc1 = automerge.init<Profile>("aaaa")
|
||||
* doc1 = automerge.change(doc1, d => {
|
||||
* d.pets = [{name: "Lassie", type: "dog"}]
|
||||
* })
|
||||
* let doc2 = automerge.init<Profile>("bbbb")
|
||||
* doc2 = automerge.merge(doc2, automerge.clone(doc1))
|
||||
*
|
||||
* doc2 = automerge.change(doc2, d => {
|
||||
* d.pets[0].name = "Beethoven"
|
||||
* })
|
||||
*
|
||||
* doc1 = automerge.change(doc1, d => {
|
||||
* d.pets[0].name = "Babe"
|
||||
* })
|
||||
*
|
||||
* const doc3 = automerge.merge(doc1, doc2)
|
||||
*
|
||||
* // Note that here we pass `doc3.pets`, not `doc3`
|
||||
* let conflicts = automerge.getConflicts(doc3.pets[0], "name")
|
||||
*
|
||||
* // The two conflicting values are the keys of the conflicts object
|
||||
* assert.deepEqual(Object.values(conflicts), ["Babe", Beethoven"])
|
||||
* ```
|
||||
*
|
||||
* ## Actor IDs
|
||||
*
|
||||
* By default automerge will generate a random actor ID for you, but most methods
|
||||
* for creating a document allow you to set the actor ID. You can get the actor ID
|
||||
* associated with the document by calling {@link getActorId}. Actor IDs must not
|
||||
* be used in concurrent threads of executiong - all changes by a given actor ID
|
||||
* are expected to be sequential.
|
||||
*
|
||||
* ## Listening to patches
|
||||
*
|
||||
* Sometimes you want to respond to changes made to an automerge document. In this
|
||||
* case you can use the {@link PatchCallback} type to receive notifications when
|
||||
* changes have been made.
|
||||
*
|
||||
* ## Cloning
|
||||
*
|
||||
* Currently you cannot make mutating changes (i.e. call {@link change}) to a
|
||||
* document which you have two pointers to. For example, in this code:
|
||||
*
|
||||
* ```javascript
|
||||
* let doc1 = automerge.init()
|
||||
* let doc2 = automerge.change(doc1, d => (d.key = "value"))
|
||||
* ```
|
||||
*
|
||||
* `doc1` and `doc2` are both pointers to the same state. Any attempt to call
|
||||
* mutating methods on `doc1` will now result in an error like
|
||||
*
|
||||
* Attempting to change an out of date document
|
||||
*
|
||||
* If you encounter this you need to clone the original document, the above sample
|
||||
* would work as:
|
||||
*
|
||||
* ```javascript
|
||||
* let doc1 = automerge.init()
|
||||
* let doc2 = automerge.change(automerge.clone(doc1), d => (d.key = "value"))
|
||||
* ```
|
||||
* @packageDocumentation
|
||||
*
|
||||
* ## The {@link unstable} module
|
||||
*
|
||||
* We are working on some changes to automerge which are not yet complete and
|
||||
* will result in backwards incompatible API changes. Once these changes are
|
||||
* ready for production use we will release a new major version of automerge.
|
||||
* However, until that point you can use the {@link unstable} module to try out
|
||||
* the new features, documents from the {@link unstable} module are
|
||||
* interoperable with documents from the main module. Please see the docs for
|
||||
* the {@link unstable} module for more details.
|
||||
*/
|
||||
export * from "./stable.ts"
|
||||
import * as unstable from "./unstable.ts"
|
||||
export { unstable }
|
43
deno_js_dist/internal_state.ts
Normal file
43
deno_js_dist/internal_state.ts
Normal file
|
@ -0,0 +1,43 @@
|
|||
import { type ObjID, type Heads, Automerge } from "https://deno.land/x/automerge_wasm@0.1.23/automerge_wasm.js";
|
||||
|
||||
import { STATE, OBJECT_ID, TRACE, IS_PROXY } from "./constants.ts"
|
||||
|
||||
import type { Doc, PatchCallback } from "./types.ts"
|
||||
|
||||
export interface InternalState<T> {
|
||||
handle: Automerge
|
||||
heads: Heads | undefined
|
||||
freeze: boolean
|
||||
patchCallback?: PatchCallback<T>
|
||||
textV2: boolean
|
||||
}
|
||||
|
||||
export function _state<T>(doc: Doc<T>, checkroot = true): InternalState<T> {
|
||||
if (typeof doc !== "object") {
|
||||
throw new RangeError("must be the document root")
|
||||
}
|
||||
const state = Reflect.get(doc, STATE) as InternalState<T>
|
||||
if (
|
||||
state === undefined ||
|
||||
state == null ||
|
||||
(checkroot && _obj(doc) !== "_root")
|
||||
) {
|
||||
throw new RangeError("must be the document root")
|
||||
}
|
||||
return state
|
||||
}
|
||||
|
||||
export function _trace<T>(doc: Doc<T>): string | undefined {
|
||||
return Reflect.get(doc, TRACE) as string
|
||||
}
|
||||
|
||||
export function _obj<T>(doc: Doc<T>): ObjID | null {
|
||||
if (!(typeof doc === "object") || doc === null) {
|
||||
return null
|
||||
}
|
||||
return Reflect.get(doc, OBJECT_ID) as ObjID
|
||||
}
|
||||
|
||||
export function _is_proxy<T>(doc: Doc<T>): boolean {
|
||||
return !!Reflect.get(doc, IS_PROXY)
|
||||
}
|
58
deno_js_dist/low_level.ts
Normal file
58
deno_js_dist/low_level.ts
Normal file
|
@ -0,0 +1,58 @@
|
|||
import {
|
||||
type API,
|
||||
Automerge,
|
||||
type Change,
|
||||
type DecodedChange,
|
||||
type Actor,
|
||||
SyncState,
|
||||
type SyncMessage,
|
||||
type JsSyncState,
|
||||
type DecodedSyncMessage,
|
||||
type ChangeToEncode,
|
||||
} from "https://deno.land/x/automerge_wasm@0.1.23/automerge_wasm.js";
|
||||
export type { ChangeToEncode } from "https://deno.land/x/automerge_wasm@0.1.23/index.d.ts";
|
||||
|
||||
export function UseApi(api: API) {
|
||||
for (const k in api) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-extra-semi,@typescript-eslint/no-explicit-any
|
||||
;(ApiHandler as any)[k] = (api as any)[k]
|
||||
}
|
||||
}
|
||||
|
||||
/* eslint-disable */
|
||||
export const ApiHandler: API = {
|
||||
create(textV2: boolean, actor?: Actor): Automerge {
|
||||
throw new RangeError("Automerge.use() not called")
|
||||
},
|
||||
load(data: Uint8Array, textV2: boolean, actor?: Actor): Automerge {
|
||||
throw new RangeError("Automerge.use() not called (load)")
|
||||
},
|
||||
encodeChange(change: ChangeToEncode): Change {
|
||||
throw new RangeError("Automerge.use() not called (encodeChange)")
|
||||
},
|
||||
decodeChange(change: Change): DecodedChange {
|
||||
throw new RangeError("Automerge.use() not called (decodeChange)")
|
||||
},
|
||||
initSyncState(): SyncState {
|
||||
throw new RangeError("Automerge.use() not called (initSyncState)")
|
||||
},
|
||||
encodeSyncMessage(message: DecodedSyncMessage): SyncMessage {
|
||||
throw new RangeError("Automerge.use() not called (encodeSyncMessage)")
|
||||
},
|
||||
decodeSyncMessage(msg: SyncMessage): DecodedSyncMessage {
|
||||
throw new RangeError("Automerge.use() not called (decodeSyncMessage)")
|
||||
},
|
||||
encodeSyncState(state: SyncState): Uint8Array {
|
||||
throw new RangeError("Automerge.use() not called (encodeSyncState)")
|
||||
},
|
||||
decodeSyncState(data: Uint8Array): SyncState {
|
||||
throw new RangeError("Automerge.use() not called (decodeSyncState)")
|
||||
},
|
||||
exportSyncState(state: SyncState): JsSyncState {
|
||||
throw new RangeError("Automerge.use() not called (exportSyncState)")
|
||||
},
|
||||
importSyncState(state: JsSyncState): SyncState {
|
||||
throw new RangeError("Automerge.use() not called (importSyncState)")
|
||||
},
|
||||
}
|
||||
/* eslint-enable */
|
54
deno_js_dist/numbers.ts
Normal file
54
deno_js_dist/numbers.ts
Normal file
|
@ -0,0 +1,54 @@
|
|||
// Convenience classes to allow users to strictly specify the number type they want
|
||||
|
||||
import { INT, UINT, F64 } from "./constants.ts"
|
||||
|
||||
export class Int {
|
||||
value: number
|
||||
|
||||
constructor(value: number) {
|
||||
if (
|
||||
!(
|
||||
Number.isInteger(value) &&
|
||||
value <= Number.MAX_SAFE_INTEGER &&
|
||||
value >= Number.MIN_SAFE_INTEGER
|
||||
)
|
||||
) {
|
||||
throw new RangeError(`Value ${value} cannot be a uint`)
|
||||
}
|
||||
this.value = value
|
||||
Reflect.defineProperty(this, INT, { value: true })
|
||||
Object.freeze(this)
|
||||
}
|
||||
}
|
||||
|
||||
export class Uint {
|
||||
value: number
|
||||
|
||||
constructor(value: number) {
|
||||
if (
|
||||
!(
|
||||
Number.isInteger(value) &&
|
||||
value <= Number.MAX_SAFE_INTEGER &&
|
||||
value >= 0
|
||||
)
|
||||
) {
|
||||
throw new RangeError(`Value ${value} cannot be a uint`)
|
||||
}
|
||||
this.value = value
|
||||
Reflect.defineProperty(this, UINT, { value: true })
|
||||
Object.freeze(this)
|
||||
}
|
||||
}
|
||||
|
||||
export class Float64 {
|
||||
value: number
|
||||
|
||||
constructor(value: number) {
|
||||
if (typeof value !== "number") {
|
||||
throw new RangeError(`Value ${value} cannot be a float64`)
|
||||
}
|
||||
this.value = value || 0.0
|
||||
Reflect.defineProperty(this, F64, { value: true })
|
||||
Object.freeze(this)
|
||||
}
|
||||
}
|
1006
deno_js_dist/proxies.ts
Normal file
1006
deno_js_dist/proxies.ts
Normal file
File diff suppressed because it is too large
Load diff
6
deno_js_dist/raw_string.ts
Normal file
6
deno_js_dist/raw_string.ts
Normal file
|
@ -0,0 +1,6 @@
|
|||
export class RawString {
|
||||
val: string
|
||||
constructor(val: string) {
|
||||
this.val = val
|
||||
}
|
||||
}
|
933
deno_js_dist/stable.ts
Normal file
933
deno_js_dist/stable.ts
Normal file
|
@ -0,0 +1,933 @@
|
|||
/** @hidden **/
|
||||
export { /** @hidden */ uuid } from "./uuid.ts"
|
||||
|
||||
import { rootProxy } from "./proxies.ts"
|
||||
import { STATE } from "./constants.ts"
|
||||
|
||||
import {
|
||||
type AutomergeValue,
|
||||
Counter,
|
||||
type Doc,
|
||||
type PatchCallback,
|
||||
} from "./types.ts"
|
||||
export {
|
||||
type AutomergeValue,
|
||||
Counter,
|
||||
type Doc,
|
||||
Int,
|
||||
Uint,
|
||||
Float64,
|
||||
type Patch,
|
||||
type PatchCallback,
|
||||
type ScalarValue,
|
||||
} from "./types.ts"
|
||||
|
||||
import { Text } from "./text.ts"
|
||||
export { Text } from "./text.ts"
|
||||
|
||||
import type {
|
||||
API,
|
||||
Actor as ActorId,
|
||||
Prop,
|
||||
ObjID,
|
||||
Change,
|
||||
DecodedChange,
|
||||
Heads,
|
||||
MaterializeValue,
|
||||
JsSyncState as SyncState,
|
||||
SyncMessage,
|
||||
DecodedSyncMessage,
|
||||
} from "https://deno.land/x/automerge_wasm@0.1.23/index.d.ts";
|
||||
export type {
|
||||
PutPatch,
|
||||
DelPatch,
|
||||
SpliceTextPatch,
|
||||
InsertPatch,
|
||||
IncPatch,
|
||||
SyncMessage,
|
||||
} from "https://deno.land/x/automerge_wasm@0.1.23/index.d.ts";
|
||||
import { ApiHandler, type ChangeToEncode, UseApi } from "./low_level.ts"
|
||||
|
||||
import { Automerge } from "https://deno.land/x/automerge_wasm@0.1.23/automerge_wasm.js";
|
||||
|
||||
import { RawString } from "./raw_string.ts"
|
||||
|
||||
import { _state, _is_proxy, _trace, _obj } from "./internal_state.ts"
|
||||
|
||||
import { stableConflictAt } from "./conflicts.ts"
|
||||
|
||||
/** Options passed to {@link change}, and {@link emptyChange}
|
||||
* @typeParam T - The type of value contained in the document
|
||||
*/
|
||||
export type ChangeOptions<T> = {
|
||||
/** A message which describes the changes */
|
||||
message?: string
|
||||
/** The unix timestamp of the change (purely advisory, not used in conflict resolution) */
|
||||
time?: number
|
||||
/** A callback which will be called to notify the caller of any changes to the document */
|
||||
patchCallback?: PatchCallback<T>
|
||||
}
|
||||
|
||||
/** Options passed to {@link loadIncremental}, {@link applyChanges}, and {@link receiveSyncMessage}
|
||||
* @typeParam T - The type of value contained in the document
|
||||
*/
|
||||
export type ApplyOptions<T> = { patchCallback?: PatchCallback<T> }
|
||||
|
||||
/**
|
||||
* A List is an extended Array that adds the two helper methods `deleteAt` and `insertAt`.
|
||||
*/
|
||||
export interface List<T> extends Array<T> {
|
||||
insertAt(index: number, ...args: T[]): List<T>
|
||||
deleteAt(index: number, numDelete?: number): List<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* To extend an arbitrary type, we have to turn any arrays that are part of the type's definition into Lists.
|
||||
* So we recurse through the properties of T, turning any Arrays we find into Lists.
|
||||
*/
|
||||
export type Extend<T> =
|
||||
// is it an array? make it a list (we recursively extend the type of the array's elements as well)
|
||||
T extends Array<infer T>
|
||||
? List<Extend<T>>
|
||||
: // is it an object? recursively extend all of its properties
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
T extends Object
|
||||
? { [P in keyof T]: Extend<T[P]> }
|
||||
: // otherwise leave the type alone
|
||||
T
|
||||
|
||||
/**
|
||||
* Function which is called by {@link change} when making changes to a `Doc<T>`
|
||||
* @typeParam T - The type of value contained in the document
|
||||
*
|
||||
* This function may mutate `doc`
|
||||
*/
|
||||
export type ChangeFn<T> = (doc: Extend<T>) => void
|
||||
|
||||
/** @hidden **/
|
||||
export interface State<T> {
|
||||
change: DecodedChange
|
||||
snapshot: T
|
||||
}
|
||||
|
||||
/** @hidden **/
|
||||
export function use(api: API) {
|
||||
UseApi(api)
|
||||
}
|
||||
|
||||
import * as wasm from "https://deno.land/x/automerge_wasm@0.1.23/automerge_wasm.js";
|
||||
use(wasm)
|
||||
|
||||
/**
|
||||
* Options to be passed to {@link init} or {@link load}
|
||||
* @typeParam T - The type of the value the document contains
|
||||
*/
|
||||
export type InitOptions<T> = {
|
||||
/** The actor ID to use for this document, a random one will be generated if `null` is passed */
|
||||
actor?: ActorId
|
||||
freeze?: boolean
|
||||
/** A callback which will be called with the initial patch once the document has finished loading */
|
||||
patchCallback?: PatchCallback<T>
|
||||
/** @hidden */
|
||||
enableTextV2?: boolean
|
||||
}
|
||||
|
||||
/** @hidden */
|
||||
export function getBackend<T>(doc: Doc<T>): Automerge {
|
||||
return _state(doc).handle
|
||||
}
|
||||
|
||||
function importOpts<T>(_actor?: ActorId | InitOptions<T>): InitOptions<T> {
|
||||
if (typeof _actor === "object") {
|
||||
return _actor
|
||||
} else {
|
||||
return { actor: _actor }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new automerge document
|
||||
*
|
||||
* @typeParam T - The type of value contained in the document. This will be the
|
||||
* type that is passed to the change closure in {@link change}
|
||||
* @param _opts - Either an actorId or an {@link InitOptions} (which may
|
||||
* contain an actorId). If this is null the document will be initialised with a
|
||||
* random actor ID
|
||||
*/
|
||||
export function init<T>(_opts?: ActorId | InitOptions<T>): Doc<T> {
|
||||
const opts = importOpts(_opts)
|
||||
const freeze = !!opts.freeze
|
||||
const patchCallback = opts.patchCallback
|
||||
const handle = ApiHandler.create(opts.enableTextV2 || false, opts.actor)
|
||||
handle.enablePatches(true)
|
||||
handle.enableFreeze(!!opts.freeze)
|
||||
handle.registerDatatype("counter", (n: number) => new Counter(n))
|
||||
const textV2 = opts.enableTextV2 || false
|
||||
if (textV2) {
|
||||
handle.registerDatatype("str", (n: string) => new RawString(n))
|
||||
} else {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
handle.registerDatatype("text", (n: any) => new Text(n))
|
||||
}
|
||||
const doc = handle.materialize("/", undefined, {
|
||||
handle,
|
||||
heads: undefined,
|
||||
freeze,
|
||||
patchCallback,
|
||||
textV2,
|
||||
}) as Doc<T>
|
||||
return doc
|
||||
}
|
||||
|
||||
/**
|
||||
* Make an immutable view of an automerge document as at `heads`
|
||||
*
|
||||
* @remarks
|
||||
* The document returned from this function cannot be passed to {@link change}.
|
||||
* This is because it shares the same underlying memory as `doc`, but it is
|
||||
* consequently a very cheap copy.
|
||||
*
|
||||
* Note that this function will throw an error if any of the hashes in `heads`
|
||||
* are not in the document.
|
||||
*
|
||||
* @typeParam T - The type of the value contained in the document
|
||||
* @param doc - The document to create a view of
|
||||
* @param heads - The hashes of the heads to create a view at
|
||||
*/
|
||||
export function view<T>(doc: Doc<T>, heads: Heads): Doc<T> {
|
||||
const state = _state(doc)
|
||||
const handle = state.handle
|
||||
return state.handle.materialize("/", heads, {
|
||||
...state,
|
||||
handle,
|
||||
heads,
|
||||
}) as Doc<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a full writable copy of an automerge document
|
||||
*
|
||||
* @remarks
|
||||
* Unlike {@link view} this function makes a full copy of the memory backing
|
||||
* the document and can thus be passed to {@link change}. It also generates a
|
||||
* new actor ID so that changes made in the new document do not create duplicate
|
||||
* sequence numbers with respect to the old document. If you need control over
|
||||
* the actor ID which is generated you can pass the actor ID as the second
|
||||
* argument
|
||||
*
|
||||
* @typeParam T - The type of the value contained in the document
|
||||
* @param doc - The document to clone
|
||||
* @param _opts - Either an actor ID to use for the new doc or an {@link InitOptions}
|
||||
*/
|
||||
export function clone<T>(
|
||||
doc: Doc<T>,
|
||||
_opts?: ActorId | InitOptions<T>
|
||||
): Doc<T> {
|
||||
const state = _state(doc)
|
||||
const heads = state.heads
|
||||
const opts = importOpts(_opts)
|
||||
const handle = state.handle.fork(opts.actor, heads)
|
||||
|
||||
// `change` uses the presence of state.heads to determine if we are in a view
|
||||
// set it to undefined to indicate that this is a full fat document
|
||||
const { heads: _oldHeads, ...stateSansHeads } = state
|
||||
return handle.applyPatches(doc, { ...stateSansHeads, handle })
|
||||
}
|
||||
|
||||
/** Explicity free the memory backing a document. Note that this is note
|
||||
* necessary in environments which support
|
||||
* [`FinalizationRegistry`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry)
|
||||
*/
|
||||
export function free<T>(doc: Doc<T>) {
|
||||
return _state(doc).handle.free()
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an automerge document from a POJO
|
||||
*
|
||||
* @param initialState - The initial state which will be copied into the document
|
||||
* @typeParam T - The type of the value passed to `from` _and_ the type the resulting document will contain
|
||||
* @typeParam actor - The actor ID of the resulting document, if this is null a random actor ID will be used
|
||||
*
|
||||
* @example
|
||||
* ```
|
||||
* const doc = automerge.from({
|
||||
* tasks: [
|
||||
* {description: "feed dogs", done: false}
|
||||
* ]
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
export function from<T extends Record<string, unknown>>(
|
||||
initialState: T | Doc<T>,
|
||||
_opts?: ActorId | InitOptions<T>
|
||||
): Doc<T> {
|
||||
return change(init(_opts), d => Object.assign(d, initialState))
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the contents of an automerge document
|
||||
* @typeParam T - The type of the value contained in the document
|
||||
* @param doc - The document to update
|
||||
* @param options - Either a message, an {@link ChangeOptions}, or a {@link ChangeFn}
|
||||
* @param callback - A `ChangeFn` to be used if `options` was a `string`
|
||||
*
|
||||
* Note that if the second argument is a function it will be used as the `ChangeFn` regardless of what the third argument is.
|
||||
*
|
||||
* @example A simple change
|
||||
* ```
|
||||
* let doc1 = automerge.init()
|
||||
* doc1 = automerge.change(doc1, d => {
|
||||
* d.key = "value"
|
||||
* })
|
||||
* assert.equal(doc1.key, "value")
|
||||
* ```
|
||||
*
|
||||
* @example A change with a message
|
||||
*
|
||||
* ```
|
||||
* doc1 = automerge.change(doc1, "add another value", d => {
|
||||
* d.key2 = "value2"
|
||||
* })
|
||||
* ```
|
||||
*
|
||||
* @example A change with a message and a timestamp
|
||||
*
|
||||
* ```
|
||||
* doc1 = automerge.change(doc1, {message: "add another value", timestamp: 1640995200}, d => {
|
||||
* d.key2 = "value2"
|
||||
* })
|
||||
* ```
|
||||
*
|
||||
* @example responding to a patch callback
|
||||
* ```
|
||||
* let patchedPath
|
||||
* let patchCallback = patch => {
|
||||
* patchedPath = patch.path
|
||||
* }
|
||||
* doc1 = automerge.change(doc1, {message, "add another value", timestamp: 1640995200, patchCallback}, d => {
|
||||
* d.key2 = "value2"
|
||||
* })
|
||||
* assert.equal(patchedPath, ["key2"])
|
||||
* ```
|
||||
*/
|
||||
export function change<T>(
|
||||
doc: Doc<T>,
|
||||
options: string | ChangeOptions<T> | ChangeFn<T>,
|
||||
callback?: ChangeFn<T>
|
||||
): Doc<T> {
|
||||
if (typeof options === "function") {
|
||||
return _change(doc, {}, options)
|
||||
} else if (typeof callback === "function") {
|
||||
if (typeof options === "string") {
|
||||
options = { message: options }
|
||||
}
|
||||
return _change(doc, options, callback)
|
||||
} else {
|
||||
throw RangeError("Invalid args for change")
|
||||
}
|
||||
}
|
||||
|
||||
function progressDocument<T>(
|
||||
doc: Doc<T>,
|
||||
heads: Heads | null,
|
||||
callback?: PatchCallback<T>
|
||||
): Doc<T> {
|
||||
if (heads == null) {
|
||||
return doc
|
||||
}
|
||||
const state = _state(doc)
|
||||
const nextState = { ...state, heads: undefined }
|
||||
const nextDoc = state.handle.applyPatches(doc, nextState, callback)
|
||||
state.heads = heads
|
||||
return nextDoc
|
||||
}
|
||||
|
||||
function _change<T>(
|
||||
doc: Doc<T>,
|
||||
options: ChangeOptions<T>,
|
||||
callback: ChangeFn<T>
|
||||
): Doc<T> {
|
||||
if (typeof callback !== "function") {
|
||||
throw new RangeError("invalid change function")
|
||||
}
|
||||
|
||||
const state = _state(doc)
|
||||
|
||||
if (doc === undefined || state === undefined) {
|
||||
throw new RangeError("must be the document root")
|
||||
}
|
||||
if (state.heads) {
|
||||
throw new RangeError(
|
||||
"Attempting to change an outdated document. Use Automerge.clone() if you wish to make a writable copy."
|
||||
)
|
||||
}
|
||||
if (_is_proxy(doc)) {
|
||||
throw new RangeError("Calls to Automerge.change cannot be nested")
|
||||
}
|
||||
const heads = state.handle.getHeads()
|
||||
try {
|
||||
state.heads = heads
|
||||
const root: T = rootProxy(state.handle, state.textV2)
|
||||
callback(root as Extend<T>)
|
||||
if (state.handle.pendingOps() === 0) {
|
||||
state.heads = undefined
|
||||
return doc
|
||||
} else {
|
||||
state.handle.commit(options.message, options.time)
|
||||
return progressDocument(
|
||||
doc,
|
||||
heads,
|
||||
options.patchCallback || state.patchCallback
|
||||
)
|
||||
}
|
||||
} catch (e) {
|
||||
state.heads = undefined
|
||||
state.handle.rollback()
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a change to a document which does not modify the document
|
||||
*
|
||||
* @param doc - The doc to add the empty change to
|
||||
* @param options - Either a message or a {@link ChangeOptions} for the new change
|
||||
*
|
||||
* Why would you want to do this? One reason might be that you have merged
|
||||
* changes from some other peers and you want to generate a change which
|
||||
* depends on those merged changes so that you can sign the new change with all
|
||||
* of the merged changes as part of the new change.
|
||||
*/
|
||||
export function emptyChange<T>(
|
||||
doc: Doc<T>,
|
||||
options: string | ChangeOptions<T> | void
|
||||
) {
|
||||
if (options === undefined) {
|
||||
options = {}
|
||||
}
|
||||
if (typeof options === "string") {
|
||||
options = { message: options }
|
||||
}
|
||||
|
||||
const state = _state(doc)
|
||||
|
||||
if (state.heads) {
|
||||
throw new RangeError(
|
||||
"Attempting to change an outdated document. Use Automerge.clone() if you wish to make a writable copy."
|
||||
)
|
||||
}
|
||||
if (_is_proxy(doc)) {
|
||||
throw new RangeError("Calls to Automerge.change cannot be nested")
|
||||
}
|
||||
|
||||
const heads = state.handle.getHeads()
|
||||
state.handle.emptyChange(options.message, options.time)
|
||||
return progressDocument(doc, heads)
|
||||
}
|
||||
|
||||
/**
|
||||
* Load an automerge document from a compressed document produce by {@link save}
|
||||
*
|
||||
* @typeParam T - The type of the value which is contained in the document.
|
||||
* Note that no validation is done to make sure this type is in
|
||||
* fact the type of the contained value so be a bit careful
|
||||
* @param data - The compressed document
|
||||
* @param _opts - Either an actor ID or some {@link InitOptions}, if the actor
|
||||
* ID is null a random actor ID will be created
|
||||
*
|
||||
* Note that `load` will throw an error if passed incomplete content (for
|
||||
* example if you are receiving content over the network and don't know if you
|
||||
* have the complete document yet). If you need to handle incomplete content use
|
||||
* {@link init} followed by {@link loadIncremental}.
|
||||
*/
|
||||
export function load<T>(
|
||||
data: Uint8Array,
|
||||
_opts?: ActorId | InitOptions<T>
|
||||
): Doc<T> {
|
||||
const opts = importOpts(_opts)
|
||||
const actor = opts.actor
|
||||
const patchCallback = opts.patchCallback
|
||||
const handle = ApiHandler.load(data, opts.enableTextV2 || false, actor)
|
||||
handle.enablePatches(true)
|
||||
handle.enableFreeze(!!opts.freeze)
|
||||
handle.registerDatatype("counter", (n: number) => new Counter(n))
|
||||
const textV2 = opts.enableTextV2 || false
|
||||
if (textV2) {
|
||||
handle.registerDatatype("str", (n: string) => new RawString(n))
|
||||
} else {
|
||||
handle.registerDatatype("text", (n: string) => new Text(n))
|
||||
}
|
||||
const doc = handle.materialize("/", undefined, {
|
||||
handle,
|
||||
heads: undefined,
|
||||
patchCallback,
|
||||
textV2,
|
||||
}) as Doc<T>
|
||||
return doc
|
||||
}
|
||||
|
||||
/**
|
||||
* Load changes produced by {@link saveIncremental}, or partial changes
|
||||
*
|
||||
* @typeParam T - The type of the value which is contained in the document.
|
||||
* Note that no validation is done to make sure this type is in
|
||||
* fact the type of the contained value so be a bit careful
|
||||
* @param data - The compressedchanges
|
||||
* @param opts - an {@link ApplyOptions}
|
||||
*
|
||||
* This function is useful when staying up to date with a connected peer.
|
||||
* Perhaps the other end sent you a full compresed document which you loaded
|
||||
* with {@link load} and they're sending you the result of
|
||||
* {@link getLastLocalChange} every time they make a change.
|
||||
*
|
||||
* Note that this function will succesfully load the results of {@link save} as
|
||||
* well as {@link getLastLocalChange} or any other incremental change.
|
||||
*/
|
||||
export function loadIncremental<T>(
|
||||
doc: Doc<T>,
|
||||
data: Uint8Array,
|
||||
opts?: ApplyOptions<T>
|
||||
): Doc<T> {
|
||||
if (!opts) {
|
||||
opts = {}
|
||||
}
|
||||
const state = _state(doc)
|
||||
if (state.heads) {
|
||||
throw new RangeError(
|
||||
"Attempting to change an out of date document - set at: " + _trace(doc)
|
||||
)
|
||||
}
|
||||
if (_is_proxy(doc)) {
|
||||
throw new RangeError("Calls to Automerge.change cannot be nested")
|
||||
}
|
||||
const heads = state.handle.getHeads()
|
||||
state.handle.loadIncremental(data)
|
||||
return progressDocument(doc, heads, opts.patchCallback || state.patchCallback)
|
||||
}
|
||||
|
||||
/**
|
||||
* Export the contents of a document to a compressed format
|
||||
*
|
||||
* @param doc - The doc to save
|
||||
*
|
||||
* The returned bytes can be passed to {@link load} or {@link loadIncremental}
|
||||
*/
|
||||
export function save<T>(doc: Doc<T>): Uint8Array {
|
||||
return _state(doc).handle.save()
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge `local` into `remote`
|
||||
* @typeParam T - The type of values contained in each document
|
||||
* @param local - The document to merge changes into
|
||||
* @param remote - The document to merge changes from
|
||||
*
|
||||
* @returns - The merged document
|
||||
*
|
||||
* Often when you are merging documents you will also need to clone them. Both
|
||||
* arguments to `merge` are frozen after the call so you can no longer call
|
||||
* mutating methods (such as {@link change}) on them. The symtom of this will be
|
||||
* an error which says "Attempting to change an out of date document". To
|
||||
* overcome this call {@link clone} on the argument before passing it to {@link
|
||||
* merge}.
|
||||
*/
|
||||
export function merge<T>(local: Doc<T>, remote: Doc<T>): Doc<T> {
|
||||
const localState = _state(local)
|
||||
|
||||
if (localState.heads) {
|
||||
throw new RangeError(
|
||||
"Attempting to change an out of date document - set at: " + _trace(local)
|
||||
)
|
||||
}
|
||||
const heads = localState.handle.getHeads()
|
||||
const remoteState = _state(remote)
|
||||
const changes = localState.handle.getChangesAdded(remoteState.handle)
|
||||
localState.handle.applyChanges(changes)
|
||||
return progressDocument(local, heads, localState.patchCallback)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the actor ID associated with the document
|
||||
*/
|
||||
export function getActorId<T>(doc: Doc<T>): ActorId {
|
||||
const state = _state(doc)
|
||||
return state.handle.getActorId()
|
||||
}
|
||||
|
||||
/**
|
||||
* The type of conflicts for particular key or index
|
||||
*
|
||||
* Maps and sequences in automerge can contain conflicting values for a
|
||||
* particular key or index. In this case {@link getConflicts} can be used to
|
||||
* obtain a `Conflicts` representing the multiple values present for the property
|
||||
*
|
||||
* A `Conflicts` is a map from a unique (per property or index) key to one of
|
||||
* the possible conflicting values for the given property.
|
||||
*/
|
||||
type Conflicts = { [key: string]: AutomergeValue }
|
||||
|
||||
/**
|
||||
* Get the conflicts associated with a property
|
||||
*
|
||||
* The values of properties in a map in automerge can be conflicted if there
|
||||
* are concurrent "put" operations to the same key. Automerge chooses one value
|
||||
* arbitrarily (but deterministically, any two nodes who have the same set of
|
||||
* changes will choose the same value) from the set of conflicting values to
|
||||
* present as the value of the key.
|
||||
*
|
||||
* Sometimes you may want to examine these conflicts, in this case you can use
|
||||
* {@link getConflicts} to get the conflicts for the key.
|
||||
*
|
||||
* @example
|
||||
* ```
|
||||
* import * as automerge from "@automerge/automerge"
|
||||
*
|
||||
* type Profile = {
|
||||
* pets: Array<{name: string, type: string}>
|
||||
* }
|
||||
*
|
||||
* let doc1 = automerge.init<Profile>("aaaa")
|
||||
* doc1 = automerge.change(doc1, d => {
|
||||
* d.pets = [{name: "Lassie", type: "dog"}]
|
||||
* })
|
||||
* let doc2 = automerge.init<Profile>("bbbb")
|
||||
* doc2 = automerge.merge(doc2, automerge.clone(doc1))
|
||||
*
|
||||
* doc2 = automerge.change(doc2, d => {
|
||||
* d.pets[0].name = "Beethoven"
|
||||
* })
|
||||
*
|
||||
* doc1 = automerge.change(doc1, d => {
|
||||
* d.pets[0].name = "Babe"
|
||||
* })
|
||||
*
|
||||
* const doc3 = automerge.merge(doc1, doc2)
|
||||
*
|
||||
* // Note that here we pass `doc3.pets`, not `doc3`
|
||||
* let conflicts = automerge.getConflicts(doc3.pets[0], "name")
|
||||
*
|
||||
* // The two conflicting values are the keys of the conflicts object
|
||||
* assert.deepEqual(Object.values(conflicts), ["Babe", Beethoven"])
|
||||
* ```
|
||||
*/
|
||||
export function getConflicts<T>(
|
||||
doc: Doc<T>,
|
||||
prop: Prop
|
||||
): Conflicts | undefined {
|
||||
const state = _state(doc, false)
|
||||
if (state.textV2) {
|
||||
throw new Error("use unstable.getConflicts for an unstable document")
|
||||
}
|
||||
const objectId = _obj(doc)
|
||||
if (objectId != null) {
|
||||
return stableConflictAt(state.handle, objectId, prop)
|
||||
} else {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the binary representation of the last change which was made to this doc
|
||||
*
|
||||
* This is most useful when staying in sync with other peers, every time you
|
||||
* make a change locally via {@link change} you immediately call {@link
|
||||
* getLastLocalChange} and send the result over the network to other peers.
|
||||
*/
|
||||
export function getLastLocalChange<T>(doc: Doc<T>): Change | undefined {
|
||||
const state = _state(doc)
|
||||
return state.handle.getLastLocalChange() || undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the object ID of an arbitrary javascript value
|
||||
*
|
||||
* This is useful to determine if something is actually an automerge document,
|
||||
* if `doc` is not an automerge document this will return null.
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export function getObjectId(doc: any, prop?: Prop): ObjID | null {
|
||||
if (prop) {
|
||||
const state = _state(doc, false)
|
||||
const objectId = _obj(doc)
|
||||
if (!state || !objectId) {
|
||||
return null
|
||||
}
|
||||
return state.handle.get(objectId, prop) as ObjID
|
||||
} else {
|
||||
return _obj(doc)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the changes which are in `newState` but not in `oldState`. The returned
|
||||
* changes can be loaded in `oldState` via {@link applyChanges}.
|
||||
*
|
||||
* Note that this will crash if there are changes in `oldState` which are not in `newState`.
|
||||
*/
|
||||
export function getChanges<T>(oldState: Doc<T>, newState: Doc<T>): Change[] {
|
||||
const n = _state(newState)
|
||||
return n.handle.getChanges(getHeads(oldState))
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the changes in a document
|
||||
*
|
||||
* This is different to {@link save} because the output is an array of changes
|
||||
* which can be individually applied via {@link applyChanges}`
|
||||
*
|
||||
*/
|
||||
export function getAllChanges<T>(doc: Doc<T>): Change[] {
|
||||
const state = _state(doc)
|
||||
return state.handle.getChanges([])
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply changes received from another document
|
||||
*
|
||||
* `doc` will be updated to reflect the `changes`. If there are changes which
|
||||
* we do not have dependencies for yet those will be stored in the document and
|
||||
* applied when the depended on changes arrive.
|
||||
*
|
||||
* You can use the {@link ApplyOptions} to pass a patchcallback which will be
|
||||
* informed of any changes which occur as a result of applying the changes
|
||||
*
|
||||
*/
|
||||
export function applyChanges<T>(
|
||||
doc: Doc<T>,
|
||||
changes: Change[],
|
||||
opts?: ApplyOptions<T>
|
||||
): [Doc<T>] {
|
||||
const state = _state(doc)
|
||||
if (!opts) {
|
||||
opts = {}
|
||||
}
|
||||
if (state.heads) {
|
||||
throw new RangeError(
|
||||
"Attempting to change an outdated document. Use Automerge.clone() if you wish to make a writable copy."
|
||||
)
|
||||
}
|
||||
if (_is_proxy(doc)) {
|
||||
throw new RangeError("Calls to Automerge.change cannot be nested")
|
||||
}
|
||||
const heads = state.handle.getHeads()
|
||||
state.handle.applyChanges(changes)
|
||||
state.heads = heads
|
||||
return [
|
||||
progressDocument(doc, heads, opts.patchCallback || state.patchCallback),
|
||||
]
|
||||
}
|
||||
|
||||
/** @hidden */
|
||||
export function getHistory<T>(doc: Doc<T>): State<T>[] {
|
||||
const textV2 = _state(doc).textV2
|
||||
const history = getAllChanges(doc)
|
||||
return history.map((change, index) => ({
|
||||
get change() {
|
||||
return decodeChange(change)
|
||||
},
|
||||
get snapshot() {
|
||||
const [state] = applyChanges(
|
||||
init({ enableTextV2: textV2 }),
|
||||
history.slice(0, index + 1)
|
||||
)
|
||||
return <T>state
|
||||
},
|
||||
}))
|
||||
}
|
||||
|
||||
/** @hidden */
|
||||
// FIXME : no tests
|
||||
// FIXME can we just use deep equals now?
|
||||
export function equals(val1: unknown, val2: unknown): boolean {
|
||||
if (!isObject(val1) || !isObject(val2)) return val1 === val2
|
||||
const keys1 = Object.keys(val1).sort(),
|
||||
keys2 = Object.keys(val2).sort()
|
||||
if (keys1.length !== keys2.length) return false
|
||||
for (let i = 0; i < keys1.length; i++) {
|
||||
if (keys1[i] !== keys2[i]) return false
|
||||
if (!equals(val1[keys1[i]], val2[keys2[i]])) return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* encode a {@link SyncState} into binary to send over the network
|
||||
*
|
||||
* @group sync
|
||||
* */
|
||||
export function encodeSyncState(state: SyncState): Uint8Array {
|
||||
const sync = ApiHandler.importSyncState(state)
|
||||
const result = ApiHandler.encodeSyncState(sync)
|
||||
sync.free()
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode some binary data into a {@link SyncState}
|
||||
*
|
||||
* @group sync
|
||||
*/
|
||||
export function decodeSyncState(state: Uint8Array): SyncState {
|
||||
const sync = ApiHandler.decodeSyncState(state)
|
||||
const result = ApiHandler.exportSyncState(sync)
|
||||
sync.free()
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a sync message to send to the peer represented by `inState`
|
||||
* @param doc - The doc to generate messages about
|
||||
* @param inState - The {@link SyncState} representing the peer we are talking to
|
||||
*
|
||||
* @group sync
|
||||
*
|
||||
* @returns An array of `[newSyncState, syncMessage | null]` where
|
||||
* `newSyncState` should replace `inState` and `syncMessage` should be sent to
|
||||
* the peer if it is not null. If `syncMessage` is null then we are up to date.
|
||||
*/
|
||||
export function generateSyncMessage<T>(
|
||||
doc: Doc<T>,
|
||||
inState: SyncState
|
||||
): [SyncState, SyncMessage | null] {
|
||||
const state = _state(doc)
|
||||
const syncState = ApiHandler.importSyncState(inState)
|
||||
const message = state.handle.generateSyncMessage(syncState)
|
||||
const outState = ApiHandler.exportSyncState(syncState)
|
||||
return [outState, message]
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a document and our sync state on receiving a sync message
|
||||
*
|
||||
* @group sync
|
||||
*
|
||||
* @param doc - The doc the sync message is about
|
||||
* @param inState - The {@link SyncState} for the peer we are communicating with
|
||||
* @param message - The message which was received
|
||||
* @param opts - Any {@link ApplyOption}s, used for passing a
|
||||
* {@link PatchCallback} which will be informed of any changes
|
||||
* in `doc` which occur because of the received sync message.
|
||||
*
|
||||
* @returns An array of `[newDoc, newSyncState, syncMessage | null]` where
|
||||
* `newDoc` is the updated state of `doc`, `newSyncState` should replace
|
||||
* `inState` and `syncMessage` should be sent to the peer if it is not null. If
|
||||
* `syncMessage` is null then we are up to date.
|
||||
*/
|
||||
export function receiveSyncMessage<T>(
|
||||
doc: Doc<T>,
|
||||
inState: SyncState,
|
||||
message: SyncMessage,
|
||||
opts?: ApplyOptions<T>
|
||||
): [Doc<T>, SyncState, null] {
|
||||
const syncState = ApiHandler.importSyncState(inState)
|
||||
if (!opts) {
|
||||
opts = {}
|
||||
}
|
||||
const state = _state(doc)
|
||||
if (state.heads) {
|
||||
throw new RangeError(
|
||||
"Attempting to change an outdated document. Use Automerge.clone() if you wish to make a writable copy."
|
||||
)
|
||||
}
|
||||
if (_is_proxy(doc)) {
|
||||
throw new RangeError("Calls to Automerge.change cannot be nested")
|
||||
}
|
||||
const heads = state.handle.getHeads()
|
||||
state.handle.receiveSyncMessage(syncState, message)
|
||||
const outSyncState = ApiHandler.exportSyncState(syncState)
|
||||
return [
|
||||
progressDocument(doc, heads, opts.patchCallback || state.patchCallback),
|
||||
outSyncState,
|
||||
null,
|
||||
]
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new, blank {@link SyncState}
|
||||
*
|
||||
* When communicating with a peer for the first time use this to generate a new
|
||||
* {@link SyncState} for them
|
||||
*
|
||||
* @group sync
|
||||
*/
|
||||
export function initSyncState(): SyncState {
|
||||
return ApiHandler.exportSyncState(ApiHandler.initSyncState())
|
||||
}
|
||||
|
||||
/** @hidden */
|
||||
export function encodeChange(change: ChangeToEncode): Change {
|
||||
return ApiHandler.encodeChange(change)
|
||||
}
|
||||
|
||||
/** @hidden */
|
||||
export function decodeChange(data: Change): DecodedChange {
|
||||
return ApiHandler.decodeChange(data)
|
||||
}
|
||||
|
||||
/** @hidden */
|
||||
export function encodeSyncMessage(message: DecodedSyncMessage): SyncMessage {
|
||||
return ApiHandler.encodeSyncMessage(message)
|
||||
}
|
||||
|
||||
/** @hidden */
|
||||
export function decodeSyncMessage(message: SyncMessage): DecodedSyncMessage {
|
||||
return ApiHandler.decodeSyncMessage(message)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get any changes in `doc` which are not dependencies of `heads`
|
||||
*/
|
||||
export function getMissingDeps<T>(doc: Doc<T>, heads: Heads): Heads {
|
||||
const state = _state(doc)
|
||||
return state.handle.getMissingDeps(heads)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the hashes of the heads of this document
|
||||
*/
|
||||
export function getHeads<T>(doc: Doc<T>): Heads {
|
||||
const state = _state(doc)
|
||||
return state.heads || state.handle.getHeads()
|
||||
}
|
||||
|
||||
/** @hidden */
|
||||
export function dump<T>(doc: Doc<T>) {
|
||||
const state = _state(doc)
|
||||
state.handle.dump()
|
||||
}
|
||||
|
||||
/** @hidden */
|
||||
export function toJS<T>(doc: Doc<T>): T {
|
||||
const state = _state(doc)
|
||||
const enabled = state.handle.enableFreeze(false)
|
||||
const result = state.handle.materialize()
|
||||
state.handle.enableFreeze(enabled)
|
||||
return result as T
|
||||
}
|
||||
|
||||
export function isAutomerge(doc: unknown): boolean {
|
||||
if (typeof doc == "object" && doc !== null) {
|
||||
return getObjectId(doc) === "_root" && !!Reflect.get(doc, STATE)
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
function isObject(obj: unknown): obj is Record<string, unknown> {
|
||||
return typeof obj === "object" && obj !== null
|
||||
}
|
||||
|
||||
export type {
|
||||
API,
|
||||
SyncState,
|
||||
ActorId,
|
||||
Conflicts,
|
||||
Prop,
|
||||
Change,
|
||||
ObjID,
|
||||
DecodedChange,
|
||||
DecodedSyncMessage,
|
||||
Heads,
|
||||
MaterializeValue,
|
||||
}
|
224
deno_js_dist/text.ts
Normal file
224
deno_js_dist/text.ts
Normal file
|
@ -0,0 +1,224 @@
|
|||
import type { Value } from "https://deno.land/x/automerge_wasm@0.1.23/index.d.ts";
|
||||
import { TEXT, STATE } from "./constants.ts"
|
||||
import type { InternalState } from "./internal_state.ts"
|
||||
|
||||
export class Text {
|
||||
//eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
elems: Array<any>
|
||||
str: string | undefined
|
||||
//eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
spans: Array<any> | undefined;
|
||||
//eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
[STATE]?: InternalState<any>
|
||||
|
||||
constructor(text?: string | string[] | Value[]) {
|
||||
if (typeof text === "string") {
|
||||
this.elems = [...text]
|
||||
} else if (Array.isArray(text)) {
|
||||
this.elems = text
|
||||
} else if (text === undefined) {
|
||||
this.elems = []
|
||||
} else {
|
||||
throw new TypeError(`Unsupported initial value for Text: ${text}`)
|
||||
}
|
||||
Reflect.defineProperty(this, TEXT, { value: true })
|
||||
}
|
||||
|
||||
get length(): number {
|
||||
return this.elems.length
|
||||
}
|
||||
|
||||
//eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
get(index: number): any {
|
||||
return this.elems[index]
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterates over the text elements character by character, including any
|
||||
* inline objects.
|
||||
*/
|
||||
[Symbol.iterator]() {
|
||||
const elems = this.elems
|
||||
let index = -1
|
||||
return {
|
||||
next() {
|
||||
index += 1
|
||||
if (index < elems.length) {
|
||||
return { done: false, value: elems[index] }
|
||||
} else {
|
||||
return { done: true }
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the content of the Text object as a simple string, ignoring any
|
||||
* non-character elements.
|
||||
*/
|
||||
toString(): string {
|
||||
if (!this.str) {
|
||||
// Concatting to a string is faster than creating an array and then
|
||||
// .join()ing for small (<100KB) arrays.
|
||||
// https://jsperf.com/join-vs-loop-w-type-test
|
||||
this.str = ""
|
||||
for (const elem of this.elems) {
|
||||
if (typeof elem === "string") this.str += elem
|
||||
else this.str += "\uFFFC"
|
||||
}
|
||||
}
|
||||
return this.str
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the content of the Text object as a sequence of strings,
|
||||
* interleaved with non-character elements.
|
||||
*
|
||||
* For example, the value `['a', 'b', {x: 3}, 'c', 'd']` has spans:
|
||||
* `=> ['ab', {x: 3}, 'cd']`
|
||||
*/
|
||||
toSpans(): Array<Value | object> {
|
||||
if (!this.spans) {
|
||||
this.spans = []
|
||||
let chars = ""
|
||||
for (const elem of this.elems) {
|
||||
if (typeof elem === "string") {
|
||||
chars += elem
|
||||
} else {
|
||||
if (chars.length > 0) {
|
||||
this.spans.push(chars)
|
||||
chars = ""
|
||||
}
|
||||
this.spans.push(elem)
|
||||
}
|
||||
}
|
||||
if (chars.length > 0) {
|
||||
this.spans.push(chars)
|
||||
}
|
||||
}
|
||||
return this.spans
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the content of the Text object as a simple string, so that the
|
||||
* JSON serialization of an Automerge document represents text nicely.
|
||||
*/
|
||||
toJSON(): string {
|
||||
return this.toString()
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the list item at position `index` to a new value `value`.
|
||||
*/
|
||||
set(index: number, value: Value) {
|
||||
if (this[STATE]) {
|
||||
throw new RangeError(
|
||||
"object cannot be modified outside of a change block"
|
||||
)
|
||||
}
|
||||
this.elems[index] = value
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts new list items `values` starting at position `index`.
|
||||
*/
|
||||
insertAt(index: number, ...values: Array<Value | object>) {
|
||||
if (this[STATE]) {
|
||||
throw new RangeError(
|
||||
"object cannot be modified outside of a change block"
|
||||
)
|
||||
}
|
||||
this.elems.splice(index, 0, ...values)
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes `numDelete` list items starting at position `index`.
|
||||
* if `numDelete` is not given, one item is deleted.
|
||||
*/
|
||||
deleteAt(index: number, numDelete = 1) {
|
||||
if (this[STATE]) {
|
||||
throw new RangeError(
|
||||
"object cannot be modified outside of a change block"
|
||||
)
|
||||
}
|
||||
this.elems.splice(index, numDelete)
|
||||
}
|
||||
|
||||
map<T>(callback: (e: Value | object) => T) {
|
||||
this.elems.map(callback)
|
||||
}
|
||||
|
||||
lastIndexOf(searchElement: Value, fromIndex?: number) {
|
||||
this.elems.lastIndexOf(searchElement, fromIndex)
|
||||
}
|
||||
|
||||
concat(other: Text): Text {
|
||||
return new Text(this.elems.concat(other.elems))
|
||||
}
|
||||
|
||||
every(test: (v: Value) => boolean): boolean {
|
||||
return this.elems.every(test)
|
||||
}
|
||||
|
||||
filter(test: (v: Value) => boolean): Text {
|
||||
return new Text(this.elems.filter(test))
|
||||
}
|
||||
|
||||
find(test: (v: Value) => boolean): Value | undefined {
|
||||
return this.elems.find(test)
|
||||
}
|
||||
|
||||
findIndex(test: (v: Value) => boolean): number | undefined {
|
||||
return this.elems.findIndex(test)
|
||||
}
|
||||
|
||||
forEach(f: (v: Value) => undefined) {
|
||||
this.elems.forEach(f)
|
||||
}
|
||||
|
||||
includes(elem: Value): boolean {
|
||||
return this.elems.includes(elem)
|
||||
}
|
||||
|
||||
indexOf(elem: Value) {
|
||||
return this.elems.indexOf(elem)
|
||||
}
|
||||
|
||||
join(sep?: string): string {
|
||||
return this.elems.join(sep)
|
||||
}
|
||||
|
||||
reduce(
|
||||
f: (
|
||||
previousValue: Value,
|
||||
currentValue: Value,
|
||||
currentIndex: number,
|
||||
array: Value[]
|
||||
) => Value
|
||||
) {
|
||||
this.elems.reduce(f)
|
||||
}
|
||||
|
||||
reduceRight(
|
||||
f: (
|
||||
previousValue: Value,
|
||||
currentValue: Value,
|
||||
currentIndex: number,
|
||||
array: Value[]
|
||||
) => Value
|
||||
) {
|
||||
this.elems.reduceRight(f)
|
||||
}
|
||||
|
||||
slice(start?: number, end?: number) {
|
||||
new Text(this.elems.slice(start, end))
|
||||
}
|
||||
|
||||
some(test: (arg: Value) => boolean): boolean {
|
||||
return this.elems.some(test)
|
||||
}
|
||||
|
||||
toLocaleString() {
|
||||
this.toString()
|
||||
}
|
||||
}
|
46
deno_js_dist/types.ts
Normal file
46
deno_js_dist/types.ts
Normal file
|
@ -0,0 +1,46 @@
|
|||
export { Text } from "./text.ts"
|
||||
import { Text } from "./text.ts"
|
||||
export { Counter } from "./counter.ts"
|
||||
export { Int, Uint, Float64 } from "./numbers.ts"
|
||||
|
||||
import { Counter } from "./counter.ts"
|
||||
import type { Patch } from "https://deno.land/x/automerge_wasm@0.1.23/index.d.ts";
|
||||
export type { Patch } from "https://deno.land/x/automerge_wasm@0.1.23/index.d.ts";
|
||||
|
||||
export type AutomergeValue =
|
||||
| ScalarValue
|
||||
| { [key: string]: AutomergeValue }
|
||||
| Array<AutomergeValue>
|
||||
| Text
|
||||
export type MapValue = { [key: string]: AutomergeValue }
|
||||
export type ListValue = Array<AutomergeValue>
|
||||
export type ScalarValue =
|
||||
| string
|
||||
| number
|
||||
| null
|
||||
| boolean
|
||||
| Date
|
||||
| Counter
|
||||
| Uint8Array
|
||||
|
||||
/**
|
||||
* An automerge document.
|
||||
* @typeParam T - The type of the value contained in this document
|
||||
*
|
||||
* Note that this provides read only access to the fields of the value. To
|
||||
* modify the value use {@link change}
|
||||
*/
|
||||
export type Doc<T> = { readonly [P in keyof T]: T[P] }
|
||||
|
||||
/**
|
||||
* Callback which is called by various methods in this library to notify the
|
||||
* user of what changes have been made.
|
||||
* @param patch - A description of the changes made
|
||||
* @param before - The document before the change was made
|
||||
* @param after - The document after the change was made
|
||||
*/
|
||||
export type PatchCallback<T> = (
|
||||
patches: Array<Patch>,
|
||||
before: Doc<T>,
|
||||
after: Doc<T>
|
||||
) => void
|
294
deno_js_dist/unstable.ts
Normal file
294
deno_js_dist/unstable.ts
Normal file
|
@ -0,0 +1,294 @@
|
|||
/**
|
||||
* # The unstable API
|
||||
*
|
||||
* This module contains new features we are working on which are either not yet
|
||||
* ready for a stable release and/or which will result in backwards incompatible
|
||||
* API changes. The API of this module may change in arbitrary ways between
|
||||
* point releases - we will always document what these changes are in the
|
||||
* [CHANGELOG](#changelog) below, but only depend on this module if you are prepared to deal
|
||||
* with frequent changes.
|
||||
*
|
||||
* ## Differences from stable
|
||||
*
|
||||
* In the stable API text objects are represented using the {@link Text} class.
|
||||
* This means you must decide up front whether your string data might need
|
||||
* concurrent merges in the future and if you change your mind you have to
|
||||
* figure out how to migrate your data. In the unstable API the `Text` class is
|
||||
* gone and all `string`s are represented using the text CRDT, allowing for
|
||||
* concurrent changes. Modifying a string is done using the {@link splice}
|
||||
* function. You can still access the old behaviour of strings which do not
|
||||
* support merging behaviour via the {@link RawString} class.
|
||||
*
|
||||
* This leads to the following differences from `stable`:
|
||||
*
|
||||
* * There is no `unstable.Text` class, all strings are text objects
|
||||
* * Reading strings in an `unstable` document is the same as reading any other
|
||||
* javascript string
|
||||
* * To modify strings in an `unstable` document use {@link splice}
|
||||
* * The {@link AutomergeValue} type does not include the {@link Text}
|
||||
* class but the {@link RawString} class is included in the {@link ScalarValue}
|
||||
* type
|
||||
*
|
||||
* ## CHANGELOG
|
||||
* * Introduce this module to expose the new API which has no `Text` class
|
||||
*
|
||||
*
|
||||
* @module
|
||||
*/
|
||||
|
||||
export {
|
||||
Counter,
|
||||
type Doc,
|
||||
Int,
|
||||
Uint,
|
||||
Float64,
|
||||
type Patch,
|
||||
type PatchCallback,
|
||||
type AutomergeValue,
|
||||
type ScalarValue,
|
||||
} from "./unstable_types.ts"
|
||||
|
||||
import type { PatchCallback } from "./stable.ts"
|
||||
|
||||
import { type UnstableConflicts as Conflicts } from "./conflicts.ts"
|
||||
import { unstableConflictAt } from "./conflicts.ts"
|
||||
|
||||
export type {
|
||||
PutPatch,
|
||||
DelPatch,
|
||||
SpliceTextPatch,
|
||||
InsertPatch,
|
||||
IncPatch,
|
||||
SyncMessage,
|
||||
} from "https://deno.land/x/automerge_wasm@0.1.23/index.d.ts";
|
||||
|
||||
export type { ChangeOptions, ApplyOptions, ChangeFn } from "./stable.ts"
|
||||
export {
|
||||
view,
|
||||
free,
|
||||
getHeads,
|
||||
change,
|
||||
emptyChange,
|
||||
loadIncremental,
|
||||
save,
|
||||
merge,
|
||||
getActorId,
|
||||
getLastLocalChange,
|
||||
getChanges,
|
||||
getAllChanges,
|
||||
applyChanges,
|
||||
getHistory,
|
||||
equals,
|
||||
encodeSyncState,
|
||||
decodeSyncState,
|
||||
generateSyncMessage,
|
||||
receiveSyncMessage,
|
||||
initSyncState,
|
||||
encodeChange,
|
||||
decodeChange,
|
||||
encodeSyncMessage,
|
||||
decodeSyncMessage,
|
||||
getMissingDeps,
|
||||
dump,
|
||||
toJS,
|
||||
isAutomerge,
|
||||
getObjectId,
|
||||
} from "./stable.ts"
|
||||
|
||||
export type InitOptions<T> = {
|
||||
/** The actor ID to use for this document, a random one will be generated if `null` is passed */
|
||||
actor?: ActorId
|
||||
freeze?: boolean
|
||||
/** A callback which will be called with the initial patch once the document has finished loading */
|
||||
patchCallback?: PatchCallback<T>
|
||||
}
|
||||
|
||||
import { ActorId, Doc } from "./stable.ts"
|
||||
import * as stable from "./stable.ts"
|
||||
export { RawString } from "./raw_string.ts"
|
||||
|
||||
/** @hidden */
|
||||
export const getBackend = stable.getBackend
|
||||
|
||||
import { _is_proxy, _state, _obj } from "./internal_state.ts"
|
||||
|
||||
/**
|
||||
* Create a new automerge document
|
||||
*
|
||||
* @typeParam T - The type of value contained in the document. This will be the
|
||||
* type that is passed to the change closure in {@link change}
|
||||
* @param _opts - Either an actorId or an {@link InitOptions} (which may
|
||||
* contain an actorId). If this is null the document will be initialised with a
|
||||
* random actor ID
|
||||
*/
|
||||
export function init<T>(_opts?: ActorId | InitOptions<T>): Doc<T> {
|
||||
const opts = importOpts(_opts)
|
||||
opts.enableTextV2 = true
|
||||
return stable.init(opts)
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a full writable copy of an automerge document
|
||||
*
|
||||
* @remarks
|
||||
* Unlike {@link view} this function makes a full copy of the memory backing
|
||||
* the document and can thus be passed to {@link change}. It also generates a
|
||||
* new actor ID so that changes made in the new document do not create duplicate
|
||||
* sequence numbers with respect to the old document. If you need control over
|
||||
* the actor ID which is generated you can pass the actor ID as the second
|
||||
* argument
|
||||
*
|
||||
* @typeParam T - The type of the value contained in the document
|
||||
* @param doc - The document to clone
|
||||
* @param _opts - Either an actor ID to use for the new doc or an {@link InitOptions}
|
||||
*/
|
||||
export function clone<T>(
|
||||
doc: Doc<T>,
|
||||
_opts?: ActorId | InitOptions<T>
|
||||
): Doc<T> {
|
||||
const opts = importOpts(_opts)
|
||||
opts.enableTextV2 = true
|
||||
return stable.clone(doc, opts)
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an automerge document from a POJO
|
||||
*
|
||||
* @param initialState - The initial state which will be copied into the document
|
||||
* @typeParam T - The type of the value passed to `from` _and_ the type the resulting document will contain
|
||||
* @typeParam actor - The actor ID of the resulting document, if this is null a random actor ID will be used
|
||||
*
|
||||
* @example
|
||||
* ```
|
||||
* const doc = automerge.from({
|
||||
* tasks: [
|
||||
* {description: "feed dogs", done: false}
|
||||
* ]
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
export function from<T extends Record<string, unknown>>(
|
||||
initialState: T | Doc<T>,
|
||||
_opts?: ActorId | InitOptions<T>
|
||||
): Doc<T> {
|
||||
const opts = importOpts(_opts)
|
||||
opts.enableTextV2 = true
|
||||
return stable.from(initialState, opts)
|
||||
}
|
||||
|
||||
/**
|
||||
* Load an automerge document from a compressed document produce by {@link save}
|
||||
*
|
||||
* @typeParam T - The type of the value which is contained in the document.
|
||||
* Note that no validation is done to make sure this type is in
|
||||
* fact the type of the contained value so be a bit careful
|
||||
* @param data - The compressed document
|
||||
* @param _opts - Either an actor ID or some {@link InitOptions}, if the actor
|
||||
* ID is null a random actor ID will be created
|
||||
*
|
||||
* Note that `load` will throw an error if passed incomplete content (for
|
||||
* example if you are receiving content over the network and don't know if you
|
||||
* have the complete document yet). If you need to handle incomplete content use
|
||||
* {@link init} followed by {@link loadIncremental}.
|
||||
*/
|
||||
export function load<T>(
|
||||
data: Uint8Array,
|
||||
_opts?: ActorId | InitOptions<T>
|
||||
): Doc<T> {
|
||||
const opts = importOpts(_opts)
|
||||
opts.enableTextV2 = true
|
||||
return stable.load(data, opts)
|
||||
}
|
||||
|
||||
function importOpts<T>(
|
||||
_actor?: ActorId | InitOptions<T>
|
||||
): stable.InitOptions<T> {
|
||||
if (typeof _actor === "object") {
|
||||
return _actor
|
||||
} else {
|
||||
return { actor: _actor }
|
||||
}
|
||||
}
|
||||
|
||||
export function splice<T>(
|
||||
doc: Doc<T>,
|
||||
prop: stable.Prop,
|
||||
index: number,
|
||||
del: number,
|
||||
newText?: string
|
||||
) {
|
||||
if (!_is_proxy(doc)) {
|
||||
throw new RangeError("object cannot be modified outside of a change block")
|
||||
}
|
||||
const state = _state(doc, false)
|
||||
const objectId = _obj(doc)
|
||||
if (!objectId) {
|
||||
throw new RangeError("invalid object for splice")
|
||||
}
|
||||
const value = `${objectId}/${prop}`
|
||||
try {
|
||||
return state.handle.splice(value, index, del, newText)
|
||||
} catch (e) {
|
||||
throw new RangeError(`Cannot splice: ${e}`)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the conflicts associated with a property
|
||||
*
|
||||
* The values of properties in a map in automerge can be conflicted if there
|
||||
* are concurrent "put" operations to the same key. Automerge chooses one value
|
||||
* arbitrarily (but deterministically, any two nodes who have the same set of
|
||||
* changes will choose the same value) from the set of conflicting values to
|
||||
* present as the value of the key.
|
||||
*
|
||||
* Sometimes you may want to examine these conflicts, in this case you can use
|
||||
* {@link getConflicts} to get the conflicts for the key.
|
||||
*
|
||||
* @example
|
||||
* ```
|
||||
* import * as automerge from "@automerge/automerge"
|
||||
*
|
||||
* type Profile = {
|
||||
* pets: Array<{name: string, type: string}>
|
||||
* }
|
||||
*
|
||||
* let doc1 = automerge.init<Profile>("aaaa")
|
||||
* doc1 = automerge.change(doc1, d => {
|
||||
* d.pets = [{name: "Lassie", type: "dog"}]
|
||||
* })
|
||||
* let doc2 = automerge.init<Profile>("bbbb")
|
||||
* doc2 = automerge.merge(doc2, automerge.clone(doc1))
|
||||
*
|
||||
* doc2 = automerge.change(doc2, d => {
|
||||
* d.pets[0].name = "Beethoven"
|
||||
* })
|
||||
*
|
||||
* doc1 = automerge.change(doc1, d => {
|
||||
* d.pets[0].name = "Babe"
|
||||
* })
|
||||
*
|
||||
* const doc3 = automerge.merge(doc1, doc2)
|
||||
*
|
||||
* // Note that here we pass `doc3.pets`, not `doc3`
|
||||
* let conflicts = automerge.getConflicts(doc3.pets[0], "name")
|
||||
*
|
||||
* // The two conflicting values are the keys of the conflicts object
|
||||
* assert.deepEqual(Object.values(conflicts), ["Babe", Beethoven"])
|
||||
* ```
|
||||
*/
|
||||
export function getConflicts<T>(
|
||||
doc: Doc<T>,
|
||||
prop: stable.Prop
|
||||
): Conflicts | undefined {
|
||||
const state = _state(doc, false)
|
||||
if (!state.textV2) {
|
||||
throw new Error("use getConflicts for a stable document")
|
||||
}
|
||||
const objectId = _obj(doc)
|
||||
if (objectId != null) {
|
||||
return unstableConflictAt(state.handle, objectId, prop)
|
||||
} else {
|
||||
return undefined
|
||||
}
|
||||
}
|
30
deno_js_dist/unstable_types.ts
Normal file
30
deno_js_dist/unstable_types.ts
Normal file
|
@ -0,0 +1,30 @@
|
|||
import { Counter } from "./types.ts"
|
||||
|
||||
export {
|
||||
Counter,
|
||||
type Doc,
|
||||
Int,
|
||||
Uint,
|
||||
Float64,
|
||||
type Patch,
|
||||
type PatchCallback,
|
||||
} from "./types.ts"
|
||||
|
||||
import { RawString } from "./raw_string.ts"
|
||||
export { RawString } from "./raw_string.ts"
|
||||
|
||||
export type AutomergeValue =
|
||||
| ScalarValue
|
||||
| { [key: string]: AutomergeValue }
|
||||
| Array<AutomergeValue>
|
||||
export type MapValue = { [key: string]: AutomergeValue }
|
||||
export type ListValue = Array<AutomergeValue>
|
||||
export type ScalarValue =
|
||||
| string
|
||||
| number
|
||||
| null
|
||||
| boolean
|
||||
| Date
|
||||
| Counter
|
||||
| Uint8Array
|
||||
| RawString
|
26
deno_js_dist/uuid.ts
Normal file
26
deno_js_dist/uuid.ts
Normal file
|
@ -0,0 +1,26 @@
|
|||
import * as v4 from "https://deno.land/x/uuid@v0.1.2/mod.ts"
|
||||
|
||||
// this file is a deno only port of the uuid module
|
||||
|
||||
function defaultFactory() {
|
||||
return v4.uuid().replace(/-/g, "")
|
||||
}
|
||||
|
||||
let factory = defaultFactory
|
||||
|
||||
interface UUIDFactory extends Function {
|
||||
setFactory(f: typeof factory): void
|
||||
reset(): void
|
||||
}
|
||||
|
||||
export const uuid: UUIDFactory = () => {
|
||||
return factory()
|
||||
}
|
||||
|
||||
uuid.setFactory = newFactory => {
|
||||
factory = newFactory
|
||||
}
|
||||
|
||||
uuid.reset = () => {
|
||||
factory = defaultFactory
|
||||
}
|
Loading…
Add table
Reference in a new issue