diff --git a/packages/backend/src/AutomergeCollaboration.ts b/packages/backend/src/AutomergeCollaboration.ts index c529f6c..a7e4ceb 100644 --- a/packages/backend/src/AutomergeCollaboration.ts +++ b/packages/backend/src/AutomergeCollaboration.ts @@ -254,9 +254,7 @@ export default class AutomergeCollaboration { opCount: collabActions.length } - if (collabActions.length > 10) { - console.log(JSON.stringify(data.payload.changes, null, 2)) - } + console.log(JSON.stringify(data.payload, null, 2)) this.onTrace(metaData, () => { switch (data.type) { diff --git a/packages/backend/src/utils/getActiveConnections.ts b/packages/backend/src/utils/getActiveConnections.ts index e6b5f42..bc85d45 100644 --- a/packages/backend/src/utils/getActiveConnections.ts +++ b/packages/backend/src/utils/getActiveConnections.ts @@ -1,12 +1,14 @@ import AutomergeBackend from '../AutomergeBackend' const getActiveConnections = (backend: AutomergeBackend, docId: string) => { + const automergeDocument = backend.documentSetMap[docId] + + if (!automergeDocument) return 0 + // This is the only way to synchronously check the number of active Automerge.Connections // for this docId. // @ts-ignore - const activeConnectionsCount = backend.documentSetMap[docId]?.handlers.size - - return activeConnectionsCount + return automergeDocument.handlers.size } export default getActiveConnections diff --git a/packages/client/src/client.spec.ts b/packages/client/src/client.spec.ts index b0eb16b..f84f5c6 100644 --- a/packages/client/src/client.spec.ts +++ b/packages/client/src/client.spec.ts @@ -278,7 +278,7 @@ describe('automerge editor client tests', () => { expect(target).toEqual(null) }) - it('should work with concurrent insert text operations', async () => { + it.only('should work with concurrent insert text operations', async () => { const editor1 = await createCollabEditor() const editor2 = await createCollabEditor() @@ -288,17 +288,20 @@ describe('automerge editor client tests', () => { return getActiveConnections(collabBackend.backend, docId) === 1 }) - editor2.insertNode({ type: 'paragraph', children: [{ text: 'hi' }] }) - - await waitForCondition(() => { - return collabBackend.backend.getDocument(docId)?.children.length === 2 - }) + // editor2.insertNode({ type: 'paragraph', children: [{ text: 'hi' }] }) + // await waitForCondition(() => { + // return collabBackend.backend.getDocument(docId)?.children.length === 1 + // }) editor1.connect() - await waitForCondition(() => { - return editor1.children.length === 2 - }) + await waitForCondition( + () => getActiveConnections(collabBackend.backend, docId) === 2 + ) + + // await waitForCondition(() => { + // return editor1.children.length === 1 + // }) editor2.destroy() @@ -306,7 +309,13 @@ describe('automerge editor client tests', () => { return getActiveConnections(collabBackend.backend, docId) === 1 }) + console.log('destroying last editor') + editor1.destroy() + + await waitForCondition(() => { + return getActiveConnections(collabBackend.backend, docId) === 0 + }) }) afterAll(() => {