diff --git a/.github/workflows/advisory-cron.yaml b/.github/workflows/advisory-cron.yaml
deleted file mode 100644
index 31bac5a3..00000000
--- a/.github/workflows/advisory-cron.yaml
+++ /dev/null
@@ -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 }}
diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml
deleted file mode 100644
index 8519ac5e..00000000
--- a/.github/workflows/ci.yaml
+++ /dev/null
@@ -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.67.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.67.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.67.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: nightly-2023-01-26
- 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: Install rust-src
- run: rustup component add rust-src
- - name: Build and test C bindings
- run: ./scripts/ci/cmake-build Release Static
- shell: bash
-
- linux:
- runs-on: ubuntu-latest
- strategy:
- matrix:
- toolchain:
- - 1.67.0
- 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.67.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.67.0
- default: true
- - uses: Swatinem/rust-cache@v1
- - run: ./scripts/ci/build-test
- shell: bash
diff --git a/.github/workflows/docs.yaml b/.github/workflows/docs.yaml
deleted file mode 100644
index b501d526..00000000
--- a/.github/workflows/docs.yaml
+++ /dev/null
@@ -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 '' > docs/index.html
-
- - name: Deploy docs
- uses: peaceiris/actions-gh-pages@v3
- with:
- github_token: ${{ secrets.GITHUB_TOKEN }}
- publish_dir: ./docs
diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml
deleted file mode 100644
index 762671ff..00000000
--- a/.github/workflows/release.yaml
+++ /dev/null
@@ -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 /// ' $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
diff --git a/README.md b/README.md
index ad174da4..d11e9d1c 100644
--- a/README.md
+++ b/README.md
@@ -42,10 +42,9 @@ In general we try and respect semver.
### JavaScript
-A stable release of the javascript package is currently available as
-`@automerge/automerge@2.0.0` where. pre-release verisions of the `2.0.1` are
-available as `2.0.1-alpha.n`. `2.0.1*` packages are also available for Deno at
-https://deno.land/x/automerge
+An alpha release of the javascript package is currently available as
+`@automerge/automerge@2.0.0-alpha.n` where `n` is an integer. We are gathering
+feedback on the API and looking to release a `2.0.0` in the next few weeks.
### Rust
@@ -53,9 +52,7 @@ The rust codebase is currently oriented around producing a performant backend
for the Javascript wrapper and as such the API for Rust code is low level and
not well documented. We will be returning to this over the next few months but
for now you will need to be comfortable reading the tests and asking questions
-to figure out how to use it. If you are looking to build rust applications which
-use automerge you may want to look into
-[autosurgeon](https://github.com/alexjg/autosurgeon)
+to figure out how to use it.
## Repository Organisation
@@ -112,16 +109,9 @@ brew install cmake node cmocka
# install yarn
npm install --global yarn
-# install javascript dependencies
-yarn --cwd ./javascript
-
# install rust dependencies
cargo install wasm-bindgen-cli wasm-opt cargo-deny
-# get nightly rust to produce optimized automerge-c builds
-rustup toolchain install nightly
-rustup component add rust-src --toolchain nightly
-
# add wasm target in addition to current architecture
rustup target add wasm32-unknown-unknown
diff --git a/deno_js_dist/LICENSE b/deno_js_dist/LICENSE
new file mode 100644
index 00000000..63b21502
--- /dev/null
+++ b/deno_js_dist/LICENSE
@@ -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.
+
diff --git a/deno_js_dist/README.md b/deno_js_dist/README.md
new file mode 100644
index 00000000..af8306ac
--- /dev/null
+++ b/deno_js_dist/README.md
@@ -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`).
diff --git a/deno_js_dist/constants.ts b/deno_js_dist/constants.ts
new file mode 100644
index 00000000..7b714772
--- /dev/null
+++ b/deno_js_dist/constants.ts
@@ -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")
diff --git a/deno_js_dist/counter.ts b/deno_js_dist/counter.ts
new file mode 100644
index 00000000..e0da5e67
--- /dev/null
+++ b/deno_js_dist/counter.ts
@@ -0,0 +1,107 @@
+import { Automerge, type ObjID, type Prop } from "https://deno.land/x/automerge_wasm@0.1.21/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
+) {
+ return new WriteableCounter(value, context, path, objectId, key)
+}
+
+//module.exports = { Counter, getWriteableCounter }
diff --git a/deno_js_dist/index.ts b/deno_js_dist/index.ts
new file mode 100644
index 00000000..07a09b99
--- /dev/null
+++ b/deno_js_dist/index.ts
@@ -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}
+ *
+ * let doc1 = automerge.init()
+ * doc1 = automerge.change(doc1, d => {
+ * d.ideas = [new automerge.Text("an immutable document")]
+ * })
+ *
+ * let doc2 = automerge.init()
+ * doc2 = automerge.merge(doc2, automerge.clone(doc1))
+ * doc2 = automerge.change(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("aaaa")
+ * doc1 = automerge.change(doc1, d => {
+ * d.pets = [{name: "Lassie", type: "dog"}]
+ * })
+ * let doc2 = automerge.init("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 }
diff --git a/deno_js_dist/internal_state.ts b/deno_js_dist/internal_state.ts
new file mode 100644
index 00000000..7229eaaf
--- /dev/null
+++ b/deno_js_dist/internal_state.ts
@@ -0,0 +1,43 @@
+import { type ObjID, type Heads, Automerge } from "https://deno.land/x/automerge_wasm@0.1.21/automerge_wasm.js";
+
+import { STATE, OBJECT_ID, TRACE, IS_PROXY } from "./constants.ts"
+
+import type { Doc, PatchCallback } from "./types.ts"
+
+export interface InternalState {
+ handle: Automerge
+ heads: Heads | undefined
+ freeze: boolean
+ patchCallback?: PatchCallback
+ textV2: boolean
+}
+
+export function _state(doc: Doc, checkroot = true): InternalState {
+ if (typeof doc !== "object") {
+ throw new RangeError("must be the document root")
+ }
+ const state = Reflect.get(doc, STATE) as InternalState
+ if (
+ state === undefined ||
+ state == null ||
+ (checkroot && _obj(doc) !== "_root")
+ ) {
+ throw new RangeError("must be the document root")
+ }
+ return state
+}
+
+export function _trace(doc: Doc): string | undefined {
+ return Reflect.get(doc, TRACE) as string
+}
+
+export function _obj(doc: Doc): ObjID | null {
+ if (!(typeof doc === "object") || doc === null) {
+ return null
+ }
+ return Reflect.get(doc, OBJECT_ID) as ObjID
+}
+
+export function _is_proxy(doc: Doc): boolean {
+ return !!Reflect.get(doc, IS_PROXY)
+}
diff --git a/deno_js_dist/low_level.ts b/deno_js_dist/low_level.ts
new file mode 100644
index 00000000..934a813b
--- /dev/null
+++ b/deno_js_dist/low_level.ts
@@ -0,0 +1,57 @@
+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.21/automerge_wasm.js";
+export type { ChangeToEncode } from "https://deno.land/x/automerge_wasm@0.1.21/index.d.ts";
+
+export function UseApi(api: API) {
+ for (const k in api) {
+ ;(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 */
diff --git a/deno_js_dist/numbers.ts b/deno_js_dist/numbers.ts
new file mode 100644
index 00000000..207b0961
--- /dev/null
+++ b/deno_js_dist/numbers.ts
@@ -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)
+ }
+}
diff --git a/deno_js_dist/proxies.ts b/deno_js_dist/proxies.ts
new file mode 100644
index 00000000..9292a808
--- /dev/null
+++ b/deno_js_dist/proxies.ts
@@ -0,0 +1,912 @@
+// @ts-nocheck
+import { Text } from "./text.ts"
+import {
+ Automerge,
+ type Heads,
+ type ObjID,
+ type Prop,
+} from "https://deno.land/x/automerge_wasm@0.1.21/automerge_wasm.js";
+
+import type {
+ AutomergeValue,
+ ScalarValue,
+ MapValue,
+ ListValue,
+ TextValue,
+} from "./types.ts"
+import { Counter, getWriteableCounter } from "./counter.ts"
+import {
+ STATE,
+ TRACE,
+ IS_PROXY,
+ OBJECT_ID,
+ COUNTER,
+ INT,
+ UINT,
+ F64,
+} from "./constants.ts"
+import { RawString } from "./raw_string.ts"
+
+type Target = {
+ context: Automerge
+ objectId: ObjID
+ path: Array
+ readonly: boolean
+ heads?: Array
+ cache: {}
+ trace?: any
+ frozen: boolean
+ textV2: boolean
+}
+
+function parseListIndex(key) {
+ if (typeof key === "string" && /^[0-9]+$/.test(key)) key = parseInt(key, 10)
+ if (typeof key !== "number") {
+ return key
+ }
+ if (key < 0 || isNaN(key) || key === Infinity || key === -Infinity) {
+ throw new RangeError("A list index must be positive, but you passed " + key)
+ }
+ return key
+}
+
+function valueAt(target: Target, prop: Prop): AutomergeValue | undefined {
+ const { context, objectId, path, readonly, heads, textV2 } = target
+ const value = context.getWithType(objectId, prop, heads)
+ if (value === null) {
+ return
+ }
+ const datatype = value[0]
+ const val = value[1]
+ switch (datatype) {
+ case undefined:
+ return
+ case "map":
+ return mapProxy(
+ context,
+ val as ObjID,
+ textV2,
+ [...path, prop],
+ readonly,
+ heads
+ )
+ case "list":
+ return listProxy(
+ context,
+ val as ObjID,
+ textV2,
+ [...path, prop],
+ readonly,
+ heads
+ )
+ case "text":
+ if (textV2) {
+ return context.text(val as ObjID, heads)
+ } else {
+ return textProxy(
+ context,
+ val as ObjID,
+ [...path, prop],
+ readonly,
+ heads
+ )
+ }
+ case "str":
+ return val
+ case "uint":
+ return val
+ case "int":
+ return val
+ case "f64":
+ return val
+ case "boolean":
+ return val
+ case "null":
+ return null
+ case "bytes":
+ return val
+ case "timestamp":
+ return val
+ case "counter": {
+ if (readonly) {
+ return new Counter(val as number)
+ } else {
+ return getWriteableCounter(val as number, context, path, objectId, prop)
+ }
+ }
+ default:
+ throw RangeError(`datatype ${datatype} unimplemented`)
+ }
+}
+
+function import_value(value: any, textV2: boolean) {
+ switch (typeof value) {
+ case "object":
+ if (value == null) {
+ return [null, "null"]
+ } else if (value[UINT]) {
+ return [value.value, "uint"]
+ } else if (value[INT]) {
+ return [value.value, "int"]
+ } else if (value[F64]) {
+ return [value.value, "f64"]
+ } else if (value[COUNTER]) {
+ return [value.value, "counter"]
+ } else if (value instanceof Date) {
+ return [value.getTime(), "timestamp"]
+ } else if (value instanceof RawString) {
+ return [value.val, "str"]
+ } else if (value instanceof Text) {
+ return [value, "text"]
+ } else if (value instanceof Uint8Array) {
+ return [value, "bytes"]
+ } else if (value instanceof Array) {
+ return [value, "list"]
+ } else if (Object.getPrototypeOf(value) === Object.getPrototypeOf({})) {
+ return [value, "map"]
+ } else if (value[OBJECT_ID]) {
+ throw new RangeError(
+ "Cannot create a reference to an existing document object"
+ )
+ } else {
+ throw new RangeError(`Cannot assign unknown object: ${value}`)
+ }
+ case "boolean":
+ return [value, "boolean"]
+ case "number":
+ if (Number.isInteger(value)) {
+ return [value, "int"]
+ } else {
+ return [value, "f64"]
+ }
+ case "string":
+ if (textV2) {
+ return [value, "text"]
+ } else {
+ return [value, "str"]
+ }
+ default:
+ throw new RangeError(`Unsupported type of value: ${typeof value}`)
+ }
+}
+
+const MapHandler = {
+ get(target: Target, key): AutomergeValue | { handle: Automerge } {
+ const { context, objectId, cache } = target
+ if (key === Symbol.toStringTag) {
+ return target[Symbol.toStringTag]
+ }
+ if (key === OBJECT_ID) return objectId
+ if (key === IS_PROXY) return true
+ if (key === TRACE) return target.trace
+ if (key === STATE) return { handle: context }
+ if (!cache[key]) {
+ cache[key] = valueAt(target, key)
+ }
+ return cache[key]
+ },
+
+ set(target: Target, key, val) {
+ const { context, objectId, path, readonly, frozen, textV2 } = target
+ target.cache = {} // reset cache on set
+ if (val && val[OBJECT_ID]) {
+ throw new RangeError(
+ "Cannot create a reference to an existing document object"
+ )
+ }
+ if (key === TRACE) {
+ target.trace = val
+ return true
+ }
+ const [value, datatype] = import_value(val, textV2)
+ if (frozen) {
+ throw new RangeError("Attempting to use an outdated Automerge document")
+ }
+ if (readonly) {
+ throw new RangeError(`Object property "${key}" cannot be modified`)
+ }
+ switch (datatype) {
+ case "list": {
+ const list = context.putObject(objectId, key, [])
+ const proxyList = listProxy(
+ context,
+ list,
+ textV2,
+ [...path, key],
+ readonly
+ )
+ for (let i = 0; i < value.length; i++) {
+ proxyList[i] = value[i]
+ }
+ break
+ }
+ case "text": {
+ if (textV2) {
+ context.putObject(objectId, key, value)
+ } else {
+ const text = context.putObject(objectId, key, "")
+ const proxyText = textProxy(context, text, [...path, key], readonly)
+ for (let i = 0; i < value.length; i++) {
+ proxyText[i] = value.get(i)
+ }
+ }
+ break
+ }
+ case "map": {
+ const map = context.putObject(objectId, key, {})
+ const proxyMap = mapProxy(
+ context,
+ map,
+ textV2,
+ [...path, key],
+ readonly
+ )
+ for (const key in value) {
+ proxyMap[key] = value[key]
+ }
+ break
+ }
+ default:
+ context.put(objectId, key, value, datatype)
+ }
+ return true
+ },
+
+ deleteProperty(target: Target, key) {
+ const { context, objectId, readonly } = target
+ target.cache = {} // reset cache on delete
+ if (readonly) {
+ throw new RangeError(`Object property "${key}" cannot be modified`)
+ }
+ context.delete(objectId, key)
+ return true
+ },
+
+ has(target: Target, key) {
+ const value = this.get(target, key)
+ return value !== undefined
+ },
+
+ getOwnPropertyDescriptor(target: Target, key) {
+ // const { context, objectId } = target
+ const value = this.get(target, key)
+ if (typeof value !== "undefined") {
+ return {
+ configurable: true,
+ enumerable: true,
+ value,
+ }
+ }
+ },
+
+ ownKeys(target: Target) {
+ const { context, objectId, heads } = target
+ // FIXME - this is a tmp workaround until fix the dupe key bug in keys()
+ const keys = context.keys(objectId, heads)
+ return [...new Set(keys)]
+ },
+}
+
+const ListHandler = {
+ get(target: Target, index) {
+ const { context, objectId, heads } = target
+ index = parseListIndex(index)
+ if (index === Symbol.hasInstance) {
+ return instance => {
+ return Array.isArray(instance)
+ }
+ }
+ if (index === Symbol.toStringTag) {
+ return target[Symbol.toStringTag]
+ }
+ if (index === OBJECT_ID) return objectId
+ if (index === IS_PROXY) return true
+ if (index === TRACE) return target.trace
+ if (index === STATE) return { handle: context }
+ if (index === "length") return context.length(objectId, heads)
+ if (typeof index === "number") {
+ return valueAt(target, index)
+ } else {
+ return listMethods(target)[index]
+ }
+ },
+
+ set(target: Target, index, val) {
+ const { context, objectId, path, readonly, frozen, textV2 } = target
+ index = parseListIndex(index)
+ if (val && val[OBJECT_ID]) {
+ throw new RangeError(
+ "Cannot create a reference to an existing document object"
+ )
+ }
+ if (index === TRACE) {
+ target.trace = val
+ return true
+ }
+ if (typeof index == "string") {
+ throw new RangeError("list index must be a number")
+ }
+ const [value, datatype] = import_value(val, textV2)
+ if (frozen) {
+ throw new RangeError("Attempting to use an outdated Automerge document")
+ }
+ if (readonly) {
+ throw new RangeError(`Object property "${index}" cannot be modified`)
+ }
+ switch (datatype) {
+ case "list": {
+ let list
+ if (index >= context.length(objectId)) {
+ list = context.insertObject(objectId, index, [])
+ } else {
+ list = context.putObject(objectId, index, [])
+ }
+ const proxyList = listProxy(
+ context,
+ list,
+ textV2,
+ [...path, index],
+ readonly
+ )
+ proxyList.splice(0, 0, ...value)
+ break
+ }
+ case "text": {
+ if (textV2) {
+ if (index >= context.length(objectId)) {
+ context.insertObject(objectId, index, value)
+ } else {
+ context.putObject(objectId, index, value)
+ }
+ } else {
+ let text
+ if (index >= context.length(objectId)) {
+ text = context.insertObject(objectId, index, "")
+ } else {
+ text = context.putObject(objectId, index, "")
+ }
+ const proxyText = textProxy(context, text, [...path, index], readonly)
+ proxyText.splice(0, 0, ...value)
+ }
+ break
+ }
+ case "map": {
+ let map
+ if (index >= context.length(objectId)) {
+ map = context.insertObject(objectId, index, {})
+ } else {
+ map = context.putObject(objectId, index, {})
+ }
+ const proxyMap = mapProxy(
+ context,
+ map,
+ textV2,
+ [...path, index],
+ readonly
+ )
+ for (const key in value) {
+ proxyMap[key] = value[key]
+ }
+ break
+ }
+ default:
+ if (index >= context.length(objectId)) {
+ context.insert(objectId, index, value, datatype)
+ } else {
+ context.put(objectId, index, value, datatype)
+ }
+ }
+ return true
+ },
+
+ deleteProperty(target: Target, index) {
+ const { context, objectId } = target
+ index = parseListIndex(index)
+ const elem = context.get(objectId, index)
+ if (elem != null && elem[0] == "counter") {
+ throw new TypeError(
+ "Unsupported operation: deleting a counter from a list"
+ )
+ }
+ context.delete(objectId, index)
+ return true
+ },
+
+ has(target: Target, index) {
+ const { context, objectId, heads } = target
+ index = parseListIndex(index)
+ if (typeof index === "number") {
+ return index < context.length(objectId, heads)
+ }
+ return index === "length"
+ },
+
+ getOwnPropertyDescriptor(target: Target, index) {
+ const { context, objectId, heads } = target
+
+ if (index === "length")
+ return { writable: true, value: context.length(objectId, heads) }
+ if (index === OBJECT_ID)
+ return { configurable: false, enumerable: false, value: objectId }
+
+ index = parseListIndex(index)
+
+ const value = valueAt(target, index)
+ return { configurable: true, enumerable: true, value }
+ },
+
+ getPrototypeOf(target) {
+ return Object.getPrototypeOf(target)
+ },
+ ownKeys(/*target*/): string[] {
+ const keys: string[] = []
+ // uncommenting this causes assert.deepEqual() to fail when comparing to a pojo array
+ // but not uncommenting it causes for (i in list) {} to not enumerate values properly
+ //const {context, objectId, heads } = target
+ //for (let i = 0; i < target.context.length(objectId, heads); i++) { keys.push(i.toString()) }
+ keys.push("length")
+ return keys
+ },
+}
+
+const TextHandler = Object.assign({}, ListHandler, {
+ get(target: Target, index: any) {
+ const { context, objectId, heads } = target
+ index = parseListIndex(index)
+ if (index === Symbol.hasInstance) {
+ return (instance: any) => {
+ return Array.isArray(instance)
+ }
+ }
+ if (index === Symbol.toStringTag) {
+ return target[Symbol.toStringTag]
+ }
+ if (index === OBJECT_ID) return objectId
+ if (index === IS_PROXY) return true
+ if (index === TRACE) return target.trace
+ if (index === STATE) return { handle: context }
+ if (index === "length") return context.length(objectId, heads)
+ if (typeof index === "number") {
+ return valueAt(target, index)
+ } else {
+ return textMethods(target)[index] || listMethods(target)[index]
+ }
+ },
+ getPrototypeOf(/*target*/) {
+ return Object.getPrototypeOf(new Text())
+ },
+})
+
+export function mapProxy(
+ context: Automerge,
+ objectId: ObjID,
+ textV2: boolean,
+ path?: Prop[],
+ readonly?: boolean,
+ heads?: Heads
+): MapValue {
+ const target: Target = {
+ context,
+ objectId,
+ path: path || [],
+ readonly: !!readonly,
+ frozen: false,
+ heads,
+ cache: {},
+ textV2,
+ }
+ const proxied = {}
+ Object.assign(proxied, target)
+ let result = new Proxy(proxied, MapHandler)
+ // conversion through unknown is necessary because the types are so different
+ return result as unknown as MapValue
+}
+
+export function listProxy(
+ context: Automerge,
+ objectId: ObjID,
+ textV2: boolean,
+ path?: Prop[],
+ readonly?: boolean,
+ heads?: Heads
+): ListValue {
+ const target: Target = {
+ context,
+ objectId,
+ path: path || [],
+ readonly: !!readonly,
+ frozen: false,
+ heads,
+ cache: {},
+ textV2,
+ }
+ const proxied = []
+ Object.assign(proxied, target)
+ // @ts-ignore
+ return new Proxy(proxied, ListHandler) as unknown as ListValue
+}
+
+export function textProxy(
+ context: Automerge,
+ objectId: ObjID,
+ path?: Prop[],
+ readonly?: boolean,
+ heads?: Heads
+): TextValue {
+ const target: Target = {
+ context,
+ objectId,
+ path: path || [],
+ readonly: !!readonly,
+ frozen: false,
+ heads,
+ cache: {},
+ textV2: false,
+ }
+ return new Proxy(target, TextHandler) as unknown as TextValue
+}
+
+export function rootProxy(
+ context: Automerge,
+ textV2: boolean,
+ readonly?: boolean
+): T {
+ /* eslint-disable-next-line */
+ return mapProxy(context, "_root", textV2, [], !!readonly)
+}
+
+function listMethods(target: Target) {
+ const { context, objectId, path, readonly, frozen, heads, textV2 } = target
+ const methods = {
+ deleteAt(index, numDelete) {
+ if (typeof numDelete === "number") {
+ context.splice(objectId, index, numDelete)
+ } else {
+ context.delete(objectId, index)
+ }
+ return this
+ },
+
+ fill(val: ScalarValue, start: number, end: number) {
+ const [value, datatype] = import_value(val, textV2)
+ const length = context.length(objectId)
+ start = parseListIndex(start || 0)
+ end = parseListIndex(end || length)
+ for (let i = start; i < Math.min(end, length); i++) {
+ if (datatype === "text" || datatype === "list" || datatype === "map") {
+ context.putObject(objectId, i, value)
+ } else {
+ context.put(objectId, i, value, datatype)
+ }
+ }
+ return this
+ },
+
+ indexOf(o, start = 0) {
+ const length = context.length(objectId)
+ for (let i = start; i < length; i++) {
+ const value = context.getWithType(objectId, i, heads)
+ if (value && (value[1] === o[OBJECT_ID] || value[1] === o)) {
+ return i
+ }
+ }
+ return -1
+ },
+
+ insertAt(index, ...values) {
+ this.splice(index, 0, ...values)
+ return this
+ },
+
+ pop() {
+ const length = context.length(objectId)
+ if (length == 0) {
+ return undefined
+ }
+ const last = valueAt(target, length - 1)
+ context.delete(objectId, length - 1)
+ return last
+ },
+
+ push(...values) {
+ const len = context.length(objectId)
+ this.splice(len, 0, ...values)
+ return context.length(objectId)
+ },
+
+ shift() {
+ if (context.length(objectId) == 0) return
+ const first = valueAt(target, 0)
+ context.delete(objectId, 0)
+ return first
+ },
+
+ splice(index, del, ...vals) {
+ index = parseListIndex(index)
+ del = parseListIndex(del)
+ for (const val of vals) {
+ if (val && val[OBJECT_ID]) {
+ throw new RangeError(
+ "Cannot create a reference to an existing document object"
+ )
+ }
+ }
+ if (frozen) {
+ throw new RangeError("Attempting to use an outdated Automerge document")
+ }
+ if (readonly) {
+ throw new RangeError(
+ "Sequence object cannot be modified outside of a change block"
+ )
+ }
+ const result: AutomergeValue[] = []
+ for (let i = 0; i < del; i++) {
+ const value = valueAt(target, index)
+ if (value !== undefined) {
+ result.push(value)
+ }
+ context.delete(objectId, index)
+ }
+ const values = vals.map(val => import_value(val, textV2))
+ for (const [value, datatype] of values) {
+ switch (datatype) {
+ case "list": {
+ const list = context.insertObject(objectId, index, [])
+ const proxyList = listProxy(
+ context,
+ list,
+ textV2,
+ [...path, index],
+ readonly
+ )
+ proxyList.splice(0, 0, ...value)
+ break
+ }
+ case "text": {
+ if (textV2) {
+ context.insertObject(objectId, index, value)
+ } else {
+ const text = context.insertObject(objectId, index, "")
+ const proxyText = textProxy(
+ context,
+ text,
+ [...path, index],
+ readonly
+ )
+ proxyText.splice(0, 0, ...value)
+ }
+ break
+ }
+ case "map": {
+ const map = context.insertObject(objectId, index, {})
+ const proxyMap = mapProxy(
+ context,
+ map,
+ textV2,
+ [...path, index],
+ readonly
+ )
+ for (const key in value) {
+ proxyMap[key] = value[key]
+ }
+ break
+ }
+ default:
+ context.insert(objectId, index, value, datatype)
+ }
+ index += 1
+ }
+ return result
+ },
+
+ unshift(...values) {
+ this.splice(0, 0, ...values)
+ return context.length(objectId)
+ },
+
+ entries() {
+ const i = 0
+ const iterator = {
+ next: () => {
+ const value = valueAt(target, i)
+ if (value === undefined) {
+ return { value: undefined, done: true }
+ } else {
+ return { value: [i, value], done: false }
+ }
+ },
+ }
+ return iterator
+ },
+
+ keys() {
+ let i = 0
+ const len = context.length(objectId, heads)
+ const iterator = {
+ next: () => {
+ let value: undefined | number = undefined
+ if (i < len) {
+ value = i
+ i++
+ }
+ return { value, done: true }
+ },
+ }
+ return iterator
+ },
+
+ values() {
+ const i = 0
+ const iterator = {
+ next: () => {
+ const value = valueAt(target, i)
+ if (value === undefined) {
+ return { value: undefined, done: true }
+ } else {
+ return { value, done: false }
+ }
+ },
+ }
+ return iterator
+ },
+
+ toArray(): AutomergeValue[] {
+ const list: AutomergeValue = []
+ let value
+ do {
+ value = valueAt(target, list.length)
+ if (value !== undefined) {
+ list.push(value)
+ }
+ } while (value !== undefined)
+
+ return list
+ },
+
+ map(f: (AutomergeValue, number) => T): T[] {
+ return this.toArray().map(f)
+ },
+
+ toString(): string {
+ return this.toArray().toString()
+ },
+
+ toLocaleString(): string {
+ return this.toArray().toLocaleString()
+ },
+
+ forEach(f: (AutomergeValue, number) => undefined) {
+ return this.toArray().forEach(f)
+ },
+
+ // todo: real concat function is different
+ concat(other: AutomergeValue[]): AutomergeValue[] {
+ return this.toArray().concat(other)
+ },
+
+ every(f: (AutomergeValue, number) => boolean): boolean {
+ return this.toArray().every(f)
+ },
+
+ filter(f: (AutomergeValue, number) => boolean): AutomergeValue[] {
+ return this.toArray().filter(f)
+ },
+
+ find(f: (AutomergeValue, number) => boolean): AutomergeValue | undefined {
+ let index = 0
+ for (const v of this) {
+ if (f(v, index)) {
+ return v
+ }
+ index += 1
+ }
+ },
+
+ findIndex(f: (AutomergeValue, number) => boolean): number {
+ let index = 0
+ for (const v of this) {
+ if (f(v, index)) {
+ return index
+ }
+ index += 1
+ }
+ return -1
+ },
+
+ includes(elem: AutomergeValue): boolean {
+ return this.find(e => e === elem) !== undefined
+ },
+
+ join(sep?: string): string {
+ return this.toArray().join(sep)
+ },
+
+ // todo: remove the any
+ reduce(f: (any, AutomergeValue) => T, initalValue?: T): T | undefined {
+ return this.toArray().reduce(f, initalValue)
+ },
+
+ // todo: remove the any
+ reduceRight(
+ f: (any, AutomergeValue) => T,
+ initalValue?: T
+ ): T | undefined {
+ return this.toArray().reduceRight(f, initalValue)
+ },
+
+ lastIndexOf(search: AutomergeValue, fromIndex = +Infinity): number {
+ // this can be faster
+ return this.toArray().lastIndexOf(search, fromIndex)
+ },
+
+ slice(index?: number, num?: number): AutomergeValue[] {
+ return this.toArray().slice(index, num)
+ },
+
+ some(f: (AutomergeValue, number) => boolean): boolean {
+ let index = 0
+ for (const v of this) {
+ if (f(v, index)) {
+ return true
+ }
+ index += 1
+ }
+ return false
+ },
+
+ [Symbol.iterator]: function* () {
+ let i = 0
+ let value = valueAt(target, i)
+ while (value !== undefined) {
+ yield value
+ i += 1
+ value = valueAt(target, i)
+ }
+ },
+ }
+ return methods
+}
+
+function textMethods(target: Target) {
+ const { context, objectId, heads } = target
+ const methods = {
+ set(index: number, value) {
+ return (this[index] = value)
+ },
+ get(index: number): AutomergeValue {
+ return this[index]
+ },
+ toString(): string {
+ return context.text(objectId, heads).replace(//g, "")
+ },
+ toSpans(): AutomergeValue[] {
+ const spans: AutomergeValue[] = []
+ let chars = ""
+ const length = context.length(objectId)
+ for (let i = 0; i < length; i++) {
+ const value = this[i]
+ if (typeof value === "string") {
+ chars += value
+ } else {
+ if (chars.length > 0) {
+ spans.push(chars)
+ chars = ""
+ }
+ spans.push(value)
+ }
+ }
+ if (chars.length > 0) {
+ spans.push(chars)
+ }
+ return spans
+ },
+ toJSON(): string {
+ return this.toString()
+ },
+ indexOf(o, start = 0) {
+ const text = context.text(objectId)
+ return text.indexOf(o, start)
+ },
+ }
+ return methods
+}
diff --git a/deno_js_dist/raw_string.ts b/deno_js_dist/raw_string.ts
new file mode 100644
index 00000000..7fc02084
--- /dev/null
+++ b/deno_js_dist/raw_string.ts
@@ -0,0 +1,6 @@
+export class RawString {
+ val: string
+ constructor(val: string) {
+ this.val = val
+ }
+}
diff --git a/deno_js_dist/stable.ts b/deno_js_dist/stable.ts
new file mode 100644
index 00000000..d7ff5d5d
--- /dev/null
+++ b/deno_js_dist/stable.ts
@@ -0,0 +1,958 @@
+/** @hidden **/
+export { /** @hidden */ uuid } from "./uuid.ts"
+
+import { rootProxy, listProxy, mapProxy, textProxy } 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,
+ Text,
+} from "./types.ts"
+
+import { 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.21/index.d.ts";
+export type {
+ PutPatch,
+ DelPatch,
+ SplicePatch,
+ IncPatch,
+ SyncMessage,
+} from "https://deno.land/x/automerge_wasm@0.1.21/index.d.ts";
+import { ApiHandler, type ChangeToEncode, UseApi } from "./low_level.ts"
+
+import { Automerge } from "https://deno.land/x/automerge_wasm@0.1.21/automerge_wasm.js";
+
+import { RawString } from "./raw_string.ts"
+
+import { _state, _is_proxy, _trace, _obj } from "./internal_state.ts"
+
+/** Options passed to {@link change}, and {@link emptyChange}
+ * @typeParam T - The type of value contained in the document
+ */
+export type ChangeOptions = {
+ /** 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
+}
+
+/** Options passed to {@link loadIncremental}, {@link applyChanges}, and {@link receiveSyncMessage}
+ * @typeParam T - The type of value contained in the document
+ */
+export type ApplyOptions = { patchCallback?: PatchCallback }
+
+/**
+ * Function which is called by {@link change} when making changes to a `Doc`
+ * @typeParam T - The type of value contained in the document
+ *
+ * This function may mutate `doc`
+ */
+export type ChangeFn = (doc: T) => void
+
+/** @hidden **/
+export interface State {
+ change: DecodedChange
+ snapshot: T
+}
+
+/** @hidden **/
+export function use(api: API) {
+ UseApi(api)
+}
+
+import * as wasm from "https://deno.land/x/automerge_wasm@0.1.21/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 = {
+ /** 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
+ /** @hidden */
+ enableTextV2?: boolean
+}
+
+/** @hidden */
+export function getBackend(doc: Doc): Automerge {
+ return _state(doc).handle
+}
+
+function importOpts(_actor?: ActorId | InitOptions): InitOptions {
+ 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(_opts?: ActorId | InitOptions): Doc {
+ 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: any) => new Counter(n))
+ let textV2 = opts.enableTextV2 || false
+ if (textV2) {
+ handle.registerDatatype("str", (n: string) => new RawString(n))
+ } else {
+ handle.registerDatatype("text", (n: any) => new Text(n))
+ }
+ const doc = handle.materialize("/", undefined, {
+ handle,
+ heads: undefined,
+ freeze,
+ patchCallback,
+ textV2,
+ }) as Doc
+ 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(doc: Doc, heads: Heads): Doc {
+ const state = _state(doc)
+ const handle = state.handle
+ return state.handle.materialize("/", heads, {
+ ...state,
+ handle,
+ heads,
+ }) as Doc
+}
+
+/**
+ * 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(
+ doc: Doc,
+ _opts?: ActorId | InitOptions
+): Doc {
+ 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(doc: Doc) {
+ 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>(
+ initialState: T | Doc,
+ _opts?: ActorId | InitOptions
+): Doc {
+ 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(
+ doc: Doc,
+ options: string | ChangeOptions | ChangeFn,
+ callback?: ChangeFn
+): Doc {
+ 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(
+ doc: Doc,
+ heads: Heads | null,
+ callback?: PatchCallback
+): Doc {
+ 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(
+ doc: Doc,
+ options: ChangeOptions,
+ callback: ChangeFn
+): Doc {
+ 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)
+ 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(
+ doc: Doc,
+ options: string | ChangeOptions | 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(
+ data: Uint8Array,
+ _opts?: ActorId | InitOptions
+): Doc {
+ 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
+ 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(
+ doc: Doc,
+ data: Uint8Array,
+ opts?: ApplyOptions
+): Doc {
+ 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(doc: Doc): 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(local: Doc, remote: Doc): Doc {
+ 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(doc: Doc): 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 }
+
+function conflictAt(
+ context: Automerge,
+ objectId: ObjID,
+ prop: Prop,
+ textV2: boolean
+): Conflicts | undefined {
+ const values = context.getAll(objectId, prop)
+ if (values.length <= 1) {
+ return
+ }
+ const result: Conflicts = {}
+ for (const fullVal of values) {
+ switch (fullVal[0]) {
+ case "map":
+ result[fullVal[1]] = mapProxy(context, fullVal[1], textV2, [prop], true)
+ break
+ case "list":
+ result[fullVal[1]] = listProxy(
+ context,
+ fullVal[1],
+ textV2,
+ [prop],
+ true
+ )
+ break
+ case "text":
+ if (textV2) {
+ result[fullVal[1]] = context.text(fullVal[1])
+ } else {
+ result[fullVal[1]] = textProxy(context, objectId, [prop], true)
+ }
+ break
+ //case "table":
+ //case "cursor":
+ case "str":
+ case "uint":
+ case "int":
+ case "f64":
+ case "boolean":
+ case "bytes":
+ case "null":
+ result[fullVal[2]] = fullVal[1]
+ break
+ case "counter":
+ result[fullVal[2]] = new Counter(fullVal[1])
+ break
+ case "timestamp":
+ result[fullVal[2]] = new Date(fullVal[1])
+ break
+ default:
+ throw RangeError(`datatype ${fullVal[0]} unimplemented`)
+ }
+ }
+ return result
+}
+
+/**
+ * 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("aaaa")
+ * doc1 = automerge.change(doc1, d => {
+ * d.pets = [{name: "Lassie", type: "dog"}]
+ * })
+ * let doc2 = automerge.init("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(
+ doc: Doc,
+ prop: Prop
+): Conflicts | undefined {
+ const state = _state(doc, false)
+ const objectId = _obj(doc)
+ if (objectId != null) {
+ return conflictAt(state.handle, objectId, prop, state.textV2)
+ } 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(doc: Doc): 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.
+ */
+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(oldState: Doc, newState: Doc): 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(doc: Doc): 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(
+ doc: Doc,
+ changes: Change[],
+ opts?: ApplyOptions
+): [Doc] {
+ 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(doc: Doc): State[] {
+ 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 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(
+ doc: Doc,
+ 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(
+ doc: Doc,
+ inState: SyncState,
+ message: SyncMessage,
+ opts?: ApplyOptions
+): [Doc, 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(doc: Doc, heads: Heads): Heads {
+ const state = _state(doc)
+ return state.handle.getMissingDeps(heads)
+}
+
+/**
+ * Get the hashes of the heads of this document
+ */
+export function getHeads(doc: Doc): Heads {
+ const state = _state(doc)
+ return state.heads || state.handle.getHeads()
+}
+
+/** @hidden */
+export function dump(doc: Doc) {
+ const state = _state(doc)
+ state.handle.dump()
+}
+
+/** @hidden */
+export function toJS(doc: Doc): 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 {
+ return typeof obj === "object" && obj !== null
+}
+
+export type {
+ API,
+ SyncState,
+ ActorId,
+ Conflicts,
+ Prop,
+ Change,
+ ObjID,
+ DecodedChange,
+ DecodedSyncMessage,
+ Heads,
+ MaterializeValue,
+}
diff --git a/deno_js_dist/text.ts b/deno_js_dist/text.ts
new file mode 100644
index 00000000..70c00700
--- /dev/null
+++ b/deno_js_dist/text.ts
@@ -0,0 +1,220 @@
+import type { Value } from "https://deno.land/x/automerge_wasm@0.1.21/index.d.ts";
+import { TEXT, STATE } from "./constants.ts"
+import type { InternalState } from "./internal_state.ts"
+
+export class Text {
+ elems: Array
+ str: string | undefined
+ spans: Array | undefined;
+ [STATE]?: InternalState
+
+ 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
+ }
+
+ 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 {
+ 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) {
+ 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(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()
+ }
+}
diff --git a/deno_js_dist/types.ts b/deno_js_dist/types.ts
new file mode 100644
index 00000000..678536bc
--- /dev/null
+++ b/deno_js_dist/types.ts
@@ -0,0 +1,45 @@
+export { 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.21/index.d.ts";
+export type { Patch } from "https://deno.land/x/automerge_wasm@0.1.21/index.d.ts";
+
+export type AutomergeValue =
+ | ScalarValue
+ | { [key: string]: AutomergeValue }
+ | Array
+export type MapValue = { [key: string]: AutomergeValue }
+export type ListValue = Array
+export type TextValue = Array
+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 = { 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 = (
+ patches: Array,
+ before: Doc,
+ after: Doc
+) => void
diff --git a/deno_js_dist/unstable.ts b/deno_js_dist/unstable.ts
new file mode 100644
index 00000000..c82a6416
--- /dev/null
+++ b/deno_js_dist/unstable.ts
@@ -0,0 +1,300 @@
+/**
+ * # 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 a `future` document is the same as reading any other
+ * javascript string
+ * * To modify strings in a `future` 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
+ */
+import { Counter } from "./types.ts"
+
+export {
+ Counter,
+ type Doc,
+ Int,
+ Uint,
+ Float64,
+ type Patch,
+ type PatchCallback,
+} from "./types.ts"
+
+import type { PatchCallback } from "./stable.ts"
+
+export type AutomergeValue =
+ | ScalarValue
+ | { [key: string]: AutomergeValue }
+ | Array
+export type MapValue = { [key: string]: AutomergeValue }
+export type ListValue = Array
+export type ScalarValue =
+ | string
+ | number
+ | null
+ | boolean
+ | Date
+ | Counter
+ | Uint8Array
+ | RawString
+
+export type Conflicts = { [key: string]: AutomergeValue }
+
+export type {
+ PutPatch,
+ DelPatch,
+ SplicePatch,
+ IncPatch,
+ SyncMessage,
+} from "https://deno.land/x/automerge_wasm@0.1.21/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 = {
+ /** 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
+}
+
+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"
+import { RawString } from "./raw_string.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(_opts?: ActorId | InitOptions): Doc {
+ let 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(
+ doc: Doc,
+ _opts?: ActorId | InitOptions
+): Doc {
+ let 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>(
+ initialState: T | Doc,
+ _opts?: ActorId | InitOptions
+): Doc {
+ 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(
+ data: Uint8Array,
+ _opts?: ActorId | InitOptions
+): Doc {
+ const opts = importOpts(_opts)
+ opts.enableTextV2 = true
+ return stable.load(data, opts)
+}
+
+function importOpts(
+ _actor?: ActorId | InitOptions
+): stable.InitOptions {
+ if (typeof _actor === "object") {
+ return _actor
+ } else {
+ return { actor: _actor }
+ }
+}
+
+export function splice(
+ doc: Doc,
+ 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("aaaa")
+ * doc1 = automerge.change(doc1, d => {
+ * d.pets = [{name: "Lassie", type: "dog"}]
+ * })
+ * let doc2 = automerge.init("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(
+ doc: Doc,
+ prop: stable.Prop
+): Conflicts | undefined {
+ // this function only exists to get the types to line up with future.AutomergeValue
+ return stable.getConflicts(doc, prop)
+}
diff --git a/deno_js_dist/uuid.ts b/deno_js_dist/uuid.ts
new file mode 100644
index 00000000..04c9b93d
--- /dev/null
+++ b/deno_js_dist/uuid.ts
@@ -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
+}
diff --git a/flake.nix b/flake.nix
index 37835738..4f9ba1fe 100644
--- a/flake.nix
+++ b/flake.nix
@@ -54,7 +54,6 @@
nodejs
yarn
- deno
# c deps
cmake
diff --git a/javascript/.eslintrc.cjs b/javascript/.eslintrc.cjs
index 88776271..5d11eb94 100644
--- a/javascript/.eslintrc.cjs
+++ b/javascript/.eslintrc.cjs
@@ -3,13 +3,4 @@ module.exports = {
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint"],
extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
- rules: {
- "@typescript-eslint/no-unused-vars": [
- "error",
- {
- argsIgnorePattern: "^_",
- varsIgnorePattern: "^_",
- },
- ],
- },
}
diff --git a/javascript/.prettierignore b/javascript/.prettierignore
index 6ab2f796..c2dcd4bb 100644
--- a/javascript/.prettierignore
+++ b/javascript/.prettierignore
@@ -1,4 +1,3 @@
e2e/verdacciodb
dist
docs
-deno_dist
diff --git a/javascript/examples/create-react-app/yarn.lock b/javascript/examples/create-react-app/yarn.lock
index ec83af3b..d6e5d93f 100644
--- a/javascript/examples/create-react-app/yarn.lock
+++ b/javascript/examples/create-react-app/yarn.lock
@@ -5845,9 +5845,9 @@ json-stable-stringify-without-jsonify@^1.0.1:
integrity sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw==
json5@^1.0.1:
- version "1.0.2"
- resolved "https://registry.yarnpkg.com/json5/-/json5-1.0.2.tgz#63d98d60f21b313b77c4d6da18bfa69d80e1d593"
- integrity sha512-g1MWMLBiz8FKi1e4w0UyVL3w+iJceWAFBAaBnnGKOpNa5f8TLktkbre1+s6oICydWAm+HRUGTmI+//xv2hvXYA==
+ version "1.0.1"
+ resolved "http://localhost:4873/json5/-/json5-1.0.1.tgz#779fb0018604fa854eacbf6252180d83543e3dbe"
+ integrity sha512-aKS4WQjPenRxiQsC93MNfjx+nbF4PAdYzmd/1JIj8HYzqfbu86beTuNgXDzPknWk0n0uARlyewZo4s++ES36Ow==
dependencies:
minimist "^1.2.0"
@@ -6165,9 +6165,9 @@ minimatch@^5.0.1:
brace-expansion "^2.0.1"
minimist@^1.2.0, minimist@^1.2.6:
- version "1.2.7"
- resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.7.tgz#daa1c4d91f507390437c6a8bc01078e7000c4d18"
- integrity sha512-bzfL1YUZsP41gmu/qjrEk0Q6i2ix/cVeAhbCbqH9u3zYutS1cLg00qhrD0M2MVdCcx4Sc0UpP2eBWo9rotpq6g==
+ version "1.2.6"
+ resolved "http://localhost:4873/minimist/-/minimist-1.2.6.tgz#8637a5b759ea0d6e98702cfb3a9283323c93af44"
+ integrity sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q==
mkdirp@~0.5.1:
version "0.5.6"
diff --git a/javascript/package.json b/javascript/package.json
index 79309907..39464fac 100644
--- a/javascript/package.json
+++ b/javascript/package.json
@@ -4,7 +4,7 @@
"Orion Henry ",
"Martin Kleppmann"
],
- "version": "2.0.2",
+ "version": "2.0.1-alpha.4",
"description": "Javascript implementation of automerge, backed by @automerge/automerge-wasm",
"homepage": "https://github.com/automerge/automerge-rs/tree/main/wrappers/javascript",
"repository": "github:automerge/automerge-rs",
@@ -47,7 +47,7 @@
"typescript": "^4.9.4"
},
"dependencies": {
- "@automerge/automerge-wasm": "0.1.25",
+ "@automerge/automerge-wasm": "0.1.21",
"uuid": "^9.0.0"
}
}
diff --git a/javascript/scripts/denoify-replacer.mjs b/javascript/scripts/denoify-replacer.mjs
index e183ba0d..fcf4bc45 100644
--- a/javascript/scripts/denoify-replacer.mjs
+++ b/javascript/scripts/denoify-replacer.mjs
@@ -12,7 +12,7 @@ makeThisModuleAnExecutableReplacer(
case "@automerge/automerge-wasm":
{
const moduleRoot =
- process.env.ROOT_MODULE ||
+ process.env.MODULE_ROOT ||
`https://deno.land/x/automerge_wasm@${version}`
/*
*We expect not to run against statements like
diff --git a/javascript/src/conflicts.ts b/javascript/src/conflicts.ts
deleted file mode 100644
index 52af23e1..00000000
--- a/javascript/src/conflicts.ts
+++ /dev/null
@@ -1,100 +0,0 @@
-import { Counter, type AutomergeValue } from "./types"
-import { Text } from "./text"
-import { type AutomergeValue as UnstableAutomergeValue } from "./unstable_types"
-import { type Target, Text1Target, Text2Target } from "./proxies"
-import { mapProxy, listProxy, ValueType } from "./proxies"
-import type { Prop, ObjID } from "@automerge/automerge-wasm"
-import { Automerge } from "@automerge/automerge-wasm"
-
-export type ConflictsF = { [key: string]: ValueType }
-export type Conflicts = ConflictsF
-export type UnstableConflicts = ConflictsF
-
-export function stableConflictAt(
- context: Automerge,
- objectId: ObjID,
- prop: Prop
-): Conflicts | undefined {
- return conflictAt(
- 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(
- context,
- objectId,
- prop,
- true,
- (context: Automerge, conflictId: ObjID): UnstableAutomergeValue => {
- return context.text(conflictId)
- }
- )
-}
-
-function conflictAt(
- context: Automerge,
- objectId: ObjID,
- prop: Prop,
- textV2: boolean,
- handleText: (a: Automerge, conflictId: ObjID) => ValueType
-): ConflictsF | undefined {
- const values = context.getAll(objectId, prop)
- if (values.length <= 1) {
- return
- }
- const result: ConflictsF = {}
- for (const fullVal of values) {
- switch (fullVal[0]) {
- case "map":
- result[fullVal[1]] = mapProxy(
- context,
- fullVal[1],
- textV2,
- [prop],
- true
- )
- break
- case "list":
- result[fullVal[1]] = listProxy(
- 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
- break
- case "counter":
- result[fullVal[2]] = new Counter(fullVal[1]) as ValueType
- break
- case "timestamp":
- result[fullVal[2]] = new Date(fullVal[1]) as ValueType
- break
- default:
- throw RangeError(`datatype ${fullVal[0]} unimplemented`)
- }
- }
- return result
-}
diff --git a/javascript/src/counter.ts b/javascript/src/counter.ts
index 88adb840..873fa157 100644
--- a/javascript/src/counter.ts
+++ b/javascript/src/counter.ts
@@ -100,7 +100,7 @@ export function getWriteableCounter(
path: Prop[],
objectId: ObjID,
key: Prop
-): WriteableCounter {
+) {
return new WriteableCounter(value, context, path, objectId, key)
}
diff --git a/javascript/src/low_level.ts b/javascript/src/low_level.ts
index f44f3a32..63ef5546 100644
--- a/javascript/src/low_level.ts
+++ b/javascript/src/low_level.ts
@@ -14,7 +14,6 @@ export type { ChangeToEncode } from "@automerge/automerge-wasm"
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]
}
}
diff --git a/javascript/src/proxies.ts b/javascript/src/proxies.ts
index 54a8dd71..7a99cf80 100644
--- a/javascript/src/proxies.ts
+++ b/javascript/src/proxies.ts
@@ -1,4 +1,3 @@
-/* eslint-disable @typescript-eslint/no-explicit-any */
import { Text } from "./text"
import {
Automerge,
@@ -7,12 +6,13 @@ import {
type Prop,
} from "@automerge/automerge-wasm"
-import type { AutomergeValue, ScalarValue, MapValue, ListValue } from "./types"
-import {
- type AutomergeValue as UnstableAutomergeValue,
- MapValue as UnstableMapValue,
- ListValue as UnstableListValue,
-} from "./unstable_types"
+import type {
+ AutomergeValue,
+ ScalarValue,
+ MapValue,
+ ListValue,
+ TextValue,
+} from "./types"
import { Counter, getWriteableCounter } from "./counter"
import {
STATE,
@@ -26,38 +26,19 @@ import {
} from "./constants"
import { RawString } from "./raw_string"
-type TargetCommon = {
+type Target = {
context: Automerge
objectId: ObjID
path: Array
readonly: boolean
heads?: Array
- cache: object
+ cache: {}
trace?: any
frozen: boolean
+ textV2: boolean
}
-export type Text2Target = TargetCommon & { textV2: true }
-export type Text1Target = TargetCommon & { textV2: false }
-export type Target = Text1Target | Text2Target
-
-export type ValueType = T extends Text2Target
- ? UnstableAutomergeValue
- : T extends Text1Target
- ? AutomergeValue
- : never
-type MapValueType = T extends Text2Target
- ? UnstableMapValue
- : T extends Text1Target
- ? MapValue
- : never
-type ListValueType = T extends Text2Target
- ? UnstableListValue
- : T extends Text1Target
- ? ListValue
- : never
-
-function parseListIndex(key: any) {
+function parseListIndex(key) {
if (typeof key === "string" && /^[0-9]+$/.test(key)) key = parseInt(key, 10)
if (typeof key !== "number") {
return key
@@ -68,10 +49,7 @@ function parseListIndex(key: any) {
return key
}
-function valueAt(
- target: T,
- prop: Prop
-): ValueType | undefined {
+function valueAt(target: Target, prop: Prop): AutomergeValue | undefined {
const { context, objectId, path, readonly, heads, textV2 } = target
const value = context.getWithType(objectId, prop, heads)
if (value === null) {
@@ -83,7 +61,7 @@ function valueAt(
case undefined:
return
case "map":
- return mapProxy(
+ return mapProxy(
context,
val as ObjID,
textV2,
@@ -92,7 +70,7 @@ function valueAt(
heads
)
case "list":
- return listProxy(
+ return listProxy(
context,
val as ObjID,
textV2,
@@ -102,7 +80,7 @@ function valueAt(
)
case "text":
if (textV2) {
- return context.text(val as ObjID, heads) as ValueType
+ return context.text(val as ObjID, heads)
} else {
return textProxy(
context,
@@ -110,36 +88,29 @@ function valueAt(
[...path, prop],
readonly,
heads
- ) as unknown as ValueType
+ )
}
case "str":
- return val as ValueType
+ return val
case "uint":
- return val as ValueType
+ return val
case "int":
- return val as ValueType
+ return val
case "f64":
- return val as ValueType
+ return val
case "boolean":
- return val as ValueType
+ return val
case "null":
- return null as ValueType
+ return null
case "bytes":
- return val as ValueType
+ return val
case "timestamp":
- return val as ValueType
+ return val
case "counter": {
if (readonly) {
- return new Counter(val as number) as ValueType
+ return new Counter(val as number)
} else {
- const counter: Counter = getWriteableCounter(
- val as number,
- context,
- path,
- objectId,
- prop
- )
- return counter as ValueType
+ return getWriteableCounter(val as number, context, path, objectId, prop)
}
}
default:
@@ -147,21 +118,7 @@ function valueAt(
}
}
-type ImportedValue =
- | [null, "null"]
- | [number, "uint"]
- | [number, "int"]
- | [number, "f64"]
- | [number, "counter"]
- | [number, "timestamp"]
- | [string, "str"]
- | [Text | string, "text"]
- | [Uint8Array, "bytes"]
- | [Array, "list"]
- | [Record, "map"]
- | [boolean, "boolean"]
-
-function import_value(value: any, textV2: boolean): ImportedValue {
+function import_value(value: any, textV2: boolean) {
switch (typeof value) {
case "object":
if (value == null) {
@@ -213,10 +170,7 @@ function import_value(value: any, textV2: boolean): ImportedValue {
}
const MapHandler = {
- get(
- target: T,
- key: any
- ): ValueType | ObjID | boolean | { handle: Automerge } {
+ get(target: Target, key): AutomergeValue | { handle: Automerge } {
const { context, objectId, cache } = target
if (key === Symbol.toStringTag) {
return target[Symbol.toStringTag]
@@ -231,7 +185,7 @@ const MapHandler = {
return cache[key]
},
- set(target: Target, key: any, val: any) {
+ set(target: Target, key, val) {
const { context, objectId, path, readonly, frozen, textV2 } = target
target.cache = {} // reset cache on set
if (val && val[OBJECT_ID]) {
@@ -267,10 +221,8 @@ const MapHandler = {
}
case "text": {
if (textV2) {
- assertString(value)
context.putObject(objectId, key, value)
} else {
- assertText(value)
const text = context.putObject(objectId, key, "")
const proxyText = textProxy(context, text, [...path, key], readonly)
for (let i = 0; i < value.length; i++) {
@@ -299,7 +251,7 @@ const MapHandler = {
return true
},
- deleteProperty(target: Target, key: any) {
+ deleteProperty(target: Target, key) {
const { context, objectId, readonly } = target
target.cache = {} // reset cache on delete
if (readonly) {
@@ -309,12 +261,12 @@ const MapHandler = {
return true
},
- has(target: Target, key: any) {
+ has(target: Target, key) {
const value = this.get(target, key)
return value !== undefined
},
- getOwnPropertyDescriptor(target: Target, key: any) {
+ getOwnPropertyDescriptor(target: Target, key) {
// const { context, objectId } = target
const value = this.get(target, key)
if (typeof value !== "undefined") {
@@ -335,20 +287,11 @@ const MapHandler = {
}
const ListHandler = {
- get(
- target: T,
- index: any
- ):
- | ValueType
- | boolean
- | ObjID
- | { handle: Automerge }
- | number
- | ((_: any) => boolean) {
+ get(target: Target, index) {
const { context, objectId, heads } = target
index = parseListIndex(index)
if (index === Symbol.hasInstance) {
- return (instance: any) => {
+ return instance => {
return Array.isArray(instance)
}
}
@@ -361,13 +304,13 @@ const ListHandler = {
if (index === STATE) return { handle: context }
if (index === "length") return context.length(objectId, heads)
if (typeof index === "number") {
- return valueAt(target, index) as ValueType
+ return valueAt(target, index)
} else {
return listMethods(target)[index]
}
},
- set(target: Target, index: any, val: any) {
+ set(target: Target, index, val) {
const { context, objectId, path, readonly, frozen, textV2 } = target
index = parseListIndex(index)
if (val && val[OBJECT_ID]) {
@@ -391,7 +334,7 @@ const ListHandler = {
}
switch (datatype) {
case "list": {
- let list: ObjID
+ let list
if (index >= context.length(objectId)) {
list = context.insertObject(objectId, index, [])
} else {
@@ -409,15 +352,13 @@ const ListHandler = {
}
case "text": {
if (textV2) {
- assertString(value)
if (index >= context.length(objectId)) {
context.insertObject(objectId, index, value)
} else {
context.putObject(objectId, index, value)
}
} else {
- let text: ObjID
- assertText(value)
+ let text
if (index >= context.length(objectId)) {
text = context.insertObject(objectId, index, "")
} else {
@@ -429,7 +370,7 @@ const ListHandler = {
break
}
case "map": {
- let map: ObjID
+ let map
if (index >= context.length(objectId)) {
map = context.insertObject(objectId, index, {})
} else {
@@ -457,7 +398,7 @@ const ListHandler = {
return true
},
- deleteProperty(target: Target, index: any) {
+ deleteProperty(target: Target, index) {
const { context, objectId } = target
index = parseListIndex(index)
const elem = context.get(objectId, index)
@@ -470,7 +411,7 @@ const ListHandler = {
return true
},
- has(target: Target, index: any) {
+ has(target: Target, index) {
const { context, objectId, heads } = target
index = parseListIndex(index)
if (typeof index === "number") {
@@ -479,7 +420,7 @@ const ListHandler = {
return index === "length"
},
- getOwnPropertyDescriptor(target: Target, index: any) {
+ getOwnPropertyDescriptor(target: Target, index) {
const { context, objectId, heads } = target
if (index === "length")
@@ -493,7 +434,7 @@ const ListHandler = {
return { configurable: true, enumerable: true, value }
},
- getPrototypeOf(target: Target) {
+ getPrototypeOf(target) {
return Object.getPrototypeOf(target)
},
ownKeys(/*target*/): string[] {
@@ -535,14 +476,14 @@ const TextHandler = Object.assign({}, ListHandler, {
},
})
-export function mapProxy(
+export function mapProxy(
context: Automerge,
objectId: ObjID,
textV2: boolean,
path?: Prop[],
readonly?: boolean,
heads?: Heads
-): MapValueType {
+): MapValue {
const target: Target = {
context,
objectId,
@@ -555,19 +496,19 @@ export function mapProxy(
}
const proxied = {}
Object.assign(proxied, target)
- const result = new Proxy(proxied, MapHandler)
+ let result = new Proxy(proxied, MapHandler)
// conversion through unknown is necessary because the types are so different
- return result as unknown as MapValueType
+ return result as unknown as MapValue
}
-export function listProxy(
+export function listProxy(
context: Automerge,
objectId: ObjID,
textV2: boolean,
path?: Prop[],
readonly?: boolean,
heads?: Heads
-): ListValueType {
+): ListValue {
const target: Target = {
context,
objectId,
@@ -580,22 +521,17 @@ export function listProxy(
}
const proxied = []
Object.assign(proxied, target)
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return new Proxy(proxied, ListHandler) as unknown as ListValue
}
-interface TextProxy extends Text {
- splice: (index: any, del: any, ...vals: any[]) => void
-}
-
export function textProxy(
context: Automerge,
objectId: ObjID,
path?: Prop[],
readonly?: boolean,
heads?: Heads
-): TextProxy {
+): TextValue {
const target: Target = {
context,
objectId,
@@ -606,9 +542,7 @@ export function textProxy(
cache: {},
textV2: false,
}
- const proxied = {}
- Object.assign(proxied, target)
- return new Proxy(proxied, TextHandler) as unknown as TextProxy
+ return new Proxy(target, TextHandler) as unknown as TextValue
}
export function rootProxy(
@@ -620,10 +554,10 @@ export function rootProxy(
return mapProxy(context, "_root", textV2, [], !!readonly)
}
-function listMethods(target: T) {
+function listMethods(target: Target) {
const { context, objectId, path, readonly, frozen, heads, textV2 } = target
const methods = {
- deleteAt(index: number, numDelete: number) {
+ deleteAt(index, numDelete) {
if (typeof numDelete === "number") {
context.splice(objectId, index, numDelete)
} else {
@@ -638,20 +572,8 @@ function listMethods(target: T) {
start = parseListIndex(start || 0)
end = parseListIndex(end || length)
for (let i = start; i < Math.min(end, length); i++) {
- if (datatype === "list" || datatype === "map") {
+ if (datatype === "text" || datatype === "list" || datatype === "map") {
context.putObject(objectId, i, value)
- } else if (datatype === "text") {
- if (textV2) {
- assertString(value)
- context.putObject(objectId, i, value)
- } else {
- assertText(value)
- const text = context.putObject(objectId, i, "")
- const proxyText = textProxy(context, text, [...path, i], readonly)
- for (let i = 0; i < value.length; i++) {
- proxyText[i] = value.get(i)
- }
- }
} else {
context.put(objectId, i, value, datatype)
}
@@ -659,7 +581,7 @@ function listMethods(target: T) {
return this
},
- indexOf(o: any, start = 0) {
+ indexOf(o, start = 0) {
const length = context.length(objectId)
for (let i = start; i < length; i++) {
const value = context.getWithType(objectId, i, heads)
@@ -670,7 +592,7 @@ function listMethods(target: T) {
return -1
},
- insertAt(index: number, ...values: any[]) {
+ insertAt(index, ...values) {
this.splice(index, 0, ...values)
return this
},
@@ -685,7 +607,7 @@ function listMethods(target: T) {
return last
},
- push(...values: any[]) {
+ push(...values) {
const len = context.length(objectId)
this.splice(len, 0, ...values)
return context.length(objectId)
@@ -698,7 +620,7 @@ function listMethods(target: T) {
return first
},
- splice(index: any, del: any, ...vals: any[]) {
+ splice(index, del, ...vals) {
index = parseListIndex(index)
del = parseListIndex(del)
for (const val of vals) {
@@ -716,9 +638,9 @@ function listMethods(target: T) {
"Sequence object cannot be modified outside of a change block"
)
}
- const result: ValueType[] = []
+ const result: AutomergeValue[] = []
for (let i = 0; i < del; i++) {
- const value = valueAt(target, index)
+ const value = valueAt(target, index)
if (value !== undefined) {
result.push(value)
}
@@ -741,7 +663,6 @@ function listMethods(target: T) {
}
case "text": {
if (textV2) {
- assertString(value)
context.insertObject(objectId, index, value)
} else {
const text = context.insertObject(objectId, index, "")
@@ -777,7 +698,7 @@ function listMethods(target: T) {
return result
},
- unshift(...values: any) {
+ unshift(...values) {
this.splice(0, 0, ...values)
return context.length(objectId)
},
@@ -828,11 +749,11 @@ function listMethods(target: T) {
return iterator
},
- toArray(): ValueType[] {
- const list: Array> = []
- let value: ValueType | undefined
+ toArray(): AutomergeValue[] {
+ const list: AutomergeValue = []
+ let value
do {
- value = valueAt(target, list.length)
+ value = valueAt(target, list.length)
if (value !== undefined) {
list.push(value)
}
@@ -841,7 +762,7 @@ function listMethods(target: T) {
return list
},
- map(f: (_a: ValueType, _n: number) => U): U[] {
+ map(f: (AutomergeValue, number) => T): T[] {
return this.toArray().map(f)
},
@@ -853,26 +774,24 @@ function listMethods(target: T) {
return this.toArray().toLocaleString()
},
- forEach(f: (_a: ValueType, _n: number) => undefined) {
+ forEach(f: (AutomergeValue, number) => undefined) {
return this.toArray().forEach(f)
},
// todo: real concat function is different
- concat(other: ValueType[]): ValueType[] {
+ concat(other: AutomergeValue[]): AutomergeValue[] {
return this.toArray().concat(other)
},
- every(f: (_a: ValueType, _n: number) => boolean): boolean {
+ every(f: (AutomergeValue, number) => boolean): boolean {
return this.toArray().every(f)
},
- filter(f: (_a: ValueType, _n: number) => boolean): ValueType[] {
+ filter(f: (AutomergeValue, number) => boolean): AutomergeValue[] {
return this.toArray().filter(f)
},
- find(
- f: (_a: ValueType, _n: number) => boolean
- ): ValueType