Skip to content

Commit

Permalink
Upgrade to automerge 2.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
acurrieclark committed May 1, 2023
1 parent 93c7343 commit ac52dca
Show file tree
Hide file tree
Showing 7 changed files with 380 additions and 289 deletions.
539 changes: 274 additions & 265 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onsetsoftware/automerge-store",
"version": "0.5.1",
"version": "0.6.0-alpha.0",
"type": "module",
"scripts": {
"dev": "vite",
Expand All @@ -24,10 +24,10 @@
"vitest": "^0.29.0"
},
"peerDependencies": {
"automerge-repo": "https://gitpkg.now.sh/onsetsoftware/automerge-repo/packages/automerge-repo?0.0.51-dummy-release"
"automerge-repo": "https://gitpkg.now.sh/onsetsoftware/automerge-repo/packages/automerge-repo?dummy-release-0.0.51-2"
},
"dependencies": {
"@automerge/automerge": "^2.0.2",
"@automerge/automerge": "^2.0.3",
"@onsetsoftware/automerge-patcher": "^0.7.0",
"dot-prop": "^7.2.0"
},
Expand Down
71 changes: 62 additions & 9 deletions src/automerge-repo-store.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,82 @@
import { AutomergeStore, AutomergeStoreOptions } from "./automerge-store";
import { DocHandle } from "automerge-repo";
import type { ChangeFn, ChangeOptions, Doc } from "@automerge/automerge";
import { DocHandle, DocHandlePatchPayload } from "automerge-repo";
import type {
ChangeFn,
ChangeOptions,
Doc,
PatchCallback,
} from "@automerge/automerge";
import type { PatchInfo } from "@automerge/automerge-wasm";

export class AutomergeRepoStore<T> extends AutomergeStore<T> {
private patchCallbacks: Set<PatchCallback<T>> = new Set();
private subscriberCount = 0;

constructor(
private handle: DocHandle<T>,
options: AutomergeStoreOptions = {},
) {
super(handle.documentId, handle.value(), options);

this._ready = false;

const listener = async ({ handle }: { handle: DocHandle<T> }) => {
this.doc = await handle.value();
};

handle.on("change", listener);
}

protected makeChange(
callback: ChangeFn<T>,
options: ChangeOptions<T> = {},
): Doc<T> {
this.handle.change(callback, options);
const { patchCallback, ...rest } = options;

if (patchCallback) {
if (this.subscriberCount === 0) {
this.handle.once(
"patch",
({ patches, handle, ...patchInfo }: DocHandlePatchPayload<T>) => {
patchCallback(patches, patchInfo as PatchInfo<T>);
},
);
} else {
this.patchCallbacks.add(patchCallback);
}
}

this.handle.change(callback, rest);

return this._doc;
}

public subscribe(
callback: (doc: T) => void,
fireImmediately: boolean = true,
) {
if (fireImmediately) {
this.handle.value().then((doc) => {
this._doc = doc;
callback(doc);
});
}

const listener = async ({
patches,
handle,
...patchInfo
}: DocHandlePatchPayload<T>) => {
this._doc = patchInfo.after;

this.patchCallbacks.forEach((cb) => {
cb(patches, patchInfo as PatchInfo<T>);
this.patchCallbacks.delete(cb);
});

callback(patchInfo.after);
};

this.handle.on("patch", listener);
this.subscriberCount++;

return () => {
this.handle.off("patch", listener);
this.subscriberCount--;
};
}
}
8 changes: 5 additions & 3 deletions src/automerge-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,16 @@ export class AutomergeStore<T> {
}

protected patchCallback(options: ChangeOptions<T>): PatchCallback<T> {
return (patches, old, updated) => {
return (patches, info) => {
this.undoStack.push({
undo: [...patches].reverse().map((patch) => unpatch(old as any, patch)),
undo: [...patches]
.reverse()
.map((patch) => unpatch(info.before as any, patch)),
redo: patches,
});

if (options.patchCallback) {
options.patchCallback(patches, old, updated);
options.patchCallback(patches, info);
}
};
}
Expand Down
12 changes: 8 additions & 4 deletions tests/dev-tools.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { devToolsMock } from "./helpers/dev-tools.mock";
import { AutomergeRepoStore, AutomergeStore } from "../src";
import { Repo } from "automerge-repo";
import { MemoryStorageAdapter } from "automerge-repo-storage-memory";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { AutomergeRepoStore, AutomergeStore } from "../src";
import { devToolsMock } from "./helpers/dev-tools.mock";

describe("Dev tools integration", () => {
beforeEach(() => {
Expand Down Expand Up @@ -54,6 +54,8 @@ describe("Dev tools integration", () => {
doc.foo = "bar";
});

await new Promise((resolve) => setTimeout(resolve));

const repo2 = new Repo({
storage: memoryStorage,
network: [],
Expand All @@ -65,7 +67,9 @@ describe("Dev tools integration", () => {

await store.ready();

expect(devToolsMock.connect).toBeCalledWith({ name: handle.documentId });
expect(devToolsMock.connect).toBeCalledWith({
name: handle.documentId,
});
});

test("store does not connect to dev tools when not required", () => {
Expand Down
25 changes: 24 additions & 1 deletion tests/repo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,29 @@ describe("Repo tests", () => {
]);
expect(first).toEqual("handle");

expect(store.doc).toEqual({ count: 0, string: "hello" });
return new Promise<void>((done) => {
store.ready().then(() => {
const sub = store.subscribe((doc) => {
expect(doc).toEqual({ count: 0, string: "hello" });
sub();
done();
});
});
});
});

test("delayed initial subscribe yeilds the correct value", async () => {
await handle.change((doc) => {
Object.assign(doc, { count: 1, string: "hello world" });
});

await new Promise((done) => setTimeout(done, 100));

return new Promise<void>((done) => {
store.subscribe((doc) => {
expect(doc).toEqual({ count: 1, string: "hello world" });
done();
});
});
});
});
8 changes: 4 additions & 4 deletions tests/undo-redo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,6 @@ describe("automerge document manager tests", () => {

await manager.ready();

manager.change((doc) => {
doc.hello.push("world");
});

let calls = 0;

const expectations = [
Expand All @@ -352,6 +348,10 @@ describe("automerge document manager tests", () => {
expectations[calls](doc);
calls++;
}, false);

manager.change((doc) => {
doc.hello.push("world");
});
});
});
});

0 comments on commit ac52dca

Please sign in to comment.