Skip to content

Commit

Permalink
update to 2.1.0-alpha.7 and add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjg committed Jun 27, 2023
1 parent 2a8221b commit 66300a9
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
yarn-error.log
dist/
cypress/videos
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"dist/**/*.js"
],
"dependencies": {
"@automerge/automerge": "^2.1.0-alpha.5",
"@automerge/automerge": "^2.1.0-alpha.7",
"@codemirror/state": "^6.2.1",
"@codemirror/view": "^6.13.2",
"automerge-repo": "^0.1.0",
Expand Down
14 changes: 1 addition & 13 deletions src/PatchSemaphore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ export class PatchSemaphore {
this._inReconcile = true

const path = getPath(view.state, this._field)
const remoteHeads = automerge.getHeads(doc)
const oldHeads = getLastHeads(view.state, this._field)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
let selection = view.state.selection

const transactions = view.state
Expand All @@ -59,16 +57,12 @@ export class PatchSemaphore {
}

// now apply the unreconciled transactions to the document
let newHeads = codeMirrorToAm(
const newHeads = codeMirrorToAm(
this._field,
change,
transactions,
view.state
)
if (headsEqual(oldHeads, newHeads)) {
// No changes were made, so we're just applying remote changes
newHeads = remoteHeads
}

// now get the diff between the updated state of the document and the heads
// and apply that to the codemirror doc
Expand All @@ -84,9 +78,3 @@ export class PatchSemaphore {
}
}
}

function headsEqual(a: Heads, b: Heads): boolean {
return (
a.length == b.length && a.every(head => b.some(other => head === other))
)
}
17 changes: 12 additions & 5 deletions test/DocHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { unstable as automerge, getHeads, Heads } from "@automerge/automerge"
export type PatchListener = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
doc: automerge.Doc<any>,
patches: Array<automerge.Patch>
patches: Array<automerge.Patch>,
source: string
) => void
type Listener = {
heads: automerge.Heads
Expand All @@ -27,17 +28,23 @@ export class DocHandle {
fn: (doc: automerge.Doc<any>) => void
): Heads => {
this.doc = automerge.changeAt(this.doc, atHeads, fn)
this._notifyListeners()
this._notifyListeners("changeAt")
return getHeads(this.doc)
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
change = (fn: (doc: automerge.Doc<any>) => void): Heads => {
this.doc = automerge.change(this.doc, fn)
this._notifyListeners()
this._notifyListeners("change")
return getHeads(this.doc)
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
merge = (other: automerge.Doc<any>) => {
this.doc = automerge.merge(this.doc, other)
this._notifyListeners("merge")
}

addListener = (listener: PatchListener) => {
const heads = automerge.getHeads(this.doc)
this.listeners.push({ heads, callback: listener })
Expand All @@ -47,13 +54,13 @@ export class DocHandle {
this.listeners = []
}

_notifyListeners = () => {
_notifyListeners = (source: string) => {
const newHeads = automerge.getHeads(this.doc)
for (const listener of this.listeners) {
if (listener.heads !== newHeads) {
const diff = automerge.diff(this.doc, listener.heads, newHeads)
if (diff.length > 0) {
listener.callback(this.doc, diff)
listener.callback(this.doc, diff, source)
}
listener.heads = newHeads
}
Expand Down
31 changes: 27 additions & 4 deletions test/Editor.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,48 @@ describe("<Editor />", () => {
const doc = automerge.from({ text: "Hello World!" })
const handle = new DocHandle(doc)
mount(<Editor handle={handle} path={["text"]} />)

// Create a local change
cy.get("div.cm-content")
.type("!")
.then(() => {
cy.get("div.cm-content").should(
"have.html",
expectedHtml(["Hello World!!"])
)
})

// eslint-disable-next-line @typescript-eslint/no-explicit-any
let branch: automerge.Doc<any>
// create a remote change then merge it in
cy.wait(100)
.then(() => {
handle.change(d => {
branch = automerge.clone(handle.doc)
branch = automerge.change(branch, d => {
automerge.splice(d, ["text"], 5, 0, " Happy")
})
handle.merge(branch)
})
.wait(100)
.then(() => {
cy.get("div.cm-content").should(
"have.html",
expectedHtml(["Hello Happy World!"])
expectedHtml(["Hello Happy World!!"])
)
})

// Now create another remote change and receive that
cy.wait(100)
.then(() => {
cy.get("div.cm-content").type("!")
branch = automerge.change(branch, d => {
automerge.splice(d, ["text"], 5, 0, " hello")
})
handle.merge(branch)
})
.then(() => {
cy.get("div.cm-content").should(
"have.html",
expectedHtml(["Hello Happy World!!"])
expectedHtml(["Hello hello Happy World!!"])
)
})
})
Expand Down
3 changes: 2 additions & 1 deletion test/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export function Editor({ handle, path }: EditorProps) {
parent: containerRef.current,
}))

handle.addListener((doc, _patches) => {
handle.addListener((doc, _patches, source) => {
console.log("patch from ", source)
semaphore.reconcile(doc, handle.changeAt, view)
})

Expand Down
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
"@jridgewell/gen-mapping" "^0.3.0"
"@jridgewell/trace-mapping" "^0.3.9"

"@automerge/automerge-wasm@^0.2.4":
version "0.2.4"
resolved "https://registry.yarnpkg.com/@automerge/automerge-wasm/-/automerge-wasm-0.2.4.tgz#ed6070c9477c8f3ea1b2520776e84c1dd9a9d77b"
integrity sha512-qbXpBbdMb/t+ufWCvn5ZLFkDpWhO2eko+dgv1NCfS9Z9DMLI9cvr+foxZ6ekTc6iDEVtgftbhZJiDcN1LPkDcw==
"@automerge/automerge-wasm@^0.2.6":
version "0.2.6"
resolved "https://registry.yarnpkg.com/@automerge/automerge-wasm/-/automerge-wasm-0.2.6.tgz#0e8ab9a0516cce3cf1a27ddc98ef978c768986df"
integrity sha512-pbH2ETAq891BDNb4VgeAem9EGkhm9uuetRiRcBvNA0BaW8lqheAu2Ll4E32+RRQL7WXCM172FLQdu/de/kkUBg==

"@automerge/automerge@^2.1.0-alpha.5":
version "2.1.0-alpha.5"
resolved "https://registry.yarnpkg.com/@automerge/automerge/-/automerge-2.1.0-alpha.5.tgz#789fb80fd80783eaeee83a172b37ae236cb47bdb"
integrity sha512-b9GKSja7cBbBpY1ydI5vT2ibm/9iLp+KD+1VoTEIVRCrrSFosmD7h6e27++LSYHQoZx5fTS411vcDUW9uvHZaQ==
"@automerge/automerge@^2.1.0-alpha.7":
version "2.1.0-alpha.7"
resolved "https://registry.yarnpkg.com/@automerge/automerge/-/automerge-2.1.0-alpha.7.tgz#0553f7035e9fa88e6a9716b65a322e9ebd73e81e"
integrity sha512-pPJFOeeuX0vsuXQRs+w6uQtU7Ocvon86r7fk5vuaCbW145iLIkfzlfU7PionJdkOmTkVOq+5vAr1ApEBP7PXsQ==
dependencies:
"@automerge/automerge-wasm" "^0.2.4"
"@automerge/automerge-wasm" "^0.2.6"
uuid "^9.0.0"

"@babel/code-frame@^7.22.5":
Expand Down

0 comments on commit 66300a9

Please sign in to comment.