Skip to content

Commit

Permalink
Issue 330: Support removeDocument from synchronizer and isDocSubscrib…
Browse files Browse the repository at this point in the history
…edTo (#2)

Co-authored-by: George Su <[email protected]>
  • Loading branch information
georgewsu and George Su authored Aug 1, 2024
1 parent 8dbc3aa commit 1e1c528
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
7 changes: 7 additions & 0 deletions packages/automerge-repo/src/RemoteHeadsSubscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,13 @@ export class RemoteHeadsSubscriptions extends EventEmitter<RemoteHeadsSubscripti
}
}

isDocSubscribedTo(documentId: DocumentId) {
for (const [peerId, subscribedDocs] of this.#subscribedDocsByPeer) {
if (subscribedDocs.has(documentId)) return true
}
return false
}

#isPeerSubscribedToDoc(peerId: PeerId, documentId: DocumentId) {
const subscribedDocs = this.#subscribedDocsByPeer.get(peerId)
return subscribedDocs && subscribedDocs.has(documentId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,9 @@ export class CollectionSynchronizer extends Synchronizer {
})
}

// TODO: implement this
// eslint-disable-next-line @typescript-eslint/no-unused-vars
removeDocument(documentId: DocumentId) {
throw new Error("not implemented")
delete this.#docSynchronizers[documentId]
delete this.#docSetUp[documentId]
}

/** Adds a peer and maybe starts synchronizing with them */
Expand Down
16 changes: 16 additions & 0 deletions packages/automerge-repo/test/CollectionSynchronizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ describe("CollectionSynchronizer", () => {

synchronizer.addDocument(handle.documentId)

setTimeout(done)
}))

it("should not synchronize a document removed from synchronizer", () =>
new Promise<void>((done, reject) => {
const handle = repo.create()

synchronizer.addDocument(handle.documentId)
synchronizer.removeDocument(handle.documentId)

synchronizer.once("message", () => {
reject(new Error("Should not have sent a message"))
})

synchronizer.addPeer("peer2" as PeerId)

setTimeout(done)
}))
})
12 changes: 8 additions & 4 deletions packages/automerge-repo/test/RemoteHeadsSubscriptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ describe("RepoHeadsSubscriptions", () => {
event: "change-remote-subs",
})

// unsubsscribe from storage B
// unsubscribe from storage B
remoteHeadsSubscriptions.unsubscribeFromRemotes([storageB])

// should forward unsubscribe to generous peer
Expand Down Expand Up @@ -223,8 +223,12 @@ describe("RepoHeadsSubscriptions", () => {
emitter: remoteHeadsSubscriptions,
event: "notify-remote-heads",
})
assert(!remoteHeadsSubscriptions.isDocSubscribedTo(docA))
assert(!remoteHeadsSubscriptions.isDocSubscribedTo(docC))
remoteHeadsSubscriptions.subscribePeerToDoc(peerC, docA)
remoteHeadsSubscriptions.subscribePeerToDoc(peerC, docC)
assert(remoteHeadsSubscriptions.isDocSubscribedTo(docA))
assert(remoteHeadsSubscriptions.isDocSubscribedTo(docC))

// change message for docA in storageB
remoteHeadsSubscriptions.handleRemoteHeads(docAHeadsChangedForStorageB)
Expand All @@ -248,16 +252,16 @@ describe("RepoHeadsSubscriptions", () => {

// unsubscribe peer C
remoteHeadsSubscriptions.handleControlMessage(unsubscribePeerCFromStorageB)
const messagesAfteUnsubscribePromise = collectMessages({
const messagesAfterUnsubscribePromise = collectMessages({
emitter: remoteHeadsSubscriptions,
event: "notify-remote-heads",
})

// heads of docB for storageB change
remoteHeadsSubscriptions.handleRemoteHeads(docBHeadsChangedForStorageB)

// expect not to be be notified
messages = await messagesAfteUnsubscribePromise
// expect not to be notified
messages = await messagesAfterUnsubscribePromise
assert.strictEqual(messages.length, 0)
})

Expand Down

0 comments on commit 1e1c528

Please sign in to comment.