Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support removeDocument from synchronizer and isDocSubscribedTo #2

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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