mirror of
https://github.com/cudr/slate-collaborative.git
synced 2024-10-27 20:34:06 +00:00
fix: large collab ops
This commit is contained in:
parent
ecceda91a4
commit
f985772afc
@ -7,6 +7,7 @@ import flatten from 'lodash/flatten'
|
|||||||
import { SyncDoc, CollabAction, toJS } from '@hiveteams/collab-bridge'
|
import { SyncDoc, CollabAction, toJS } from '@hiveteams/collab-bridge'
|
||||||
import { debugCollabBackend } from './utils/debug'
|
import { debugCollabBackend } from './utils/debug'
|
||||||
import AutomergeBackend from './AutomergeBackend'
|
import AutomergeBackend from './AutomergeBackend'
|
||||||
|
import getActiveConnections from './utils/getActiveConnections'
|
||||||
|
|
||||||
export interface IAutomergeMetaData {
|
export interface IAutomergeMetaData {
|
||||||
docId: string
|
docId: string
|
||||||
@ -253,6 +254,10 @@ export default class AutomergeCollaboration {
|
|||||||
opCount: collabActions.length
|
opCount: collabActions.length
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (collabActions.length > 10) {
|
||||||
|
console.log(JSON.stringify(data.payload.changes, null, 2))
|
||||||
|
}
|
||||||
|
|
||||||
this.onTrace(metaData, () => {
|
this.onTrace(metaData, () => {
|
||||||
switch (data.type) {
|
switch (data.type) {
|
||||||
case 'operation':
|
case 'operation':
|
||||||
@ -344,11 +349,7 @@ export default class AutomergeCollaboration {
|
|||||||
garbageNsp = (socket: SocketIO.Socket) => {
|
garbageNsp = (socket: SocketIO.Socket) => {
|
||||||
const { name: docId } = socket.nsp
|
const { name: docId } = socket.nsp
|
||||||
|
|
||||||
// This is the only way to synchronously check the number of active Automerge.Connections
|
const activeConnectionsCount = getActiveConnections(this.backend, docId)
|
||||||
// for this docId.
|
|
||||||
// @ts-ignore
|
|
||||||
const activeConnectionsCount = this.backend.documentSetMap[docId]?.handlers
|
|
||||||
.size
|
|
||||||
|
|
||||||
debugCollabBackend(
|
debugCollabBackend(
|
||||||
'Garbage namespace activeConnections=%s',
|
'Garbage namespace activeConnections=%s',
|
||||||
|
12
packages/backend/src/utils/getActiveConnections.ts
Normal file
12
packages/backend/src/utils/getActiveConnections.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import AutomergeBackend from '../AutomergeBackend'
|
||||||
|
|
||||||
|
const getActiveConnections = (backend: AutomergeBackend, docId: string) => {
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
export default getActiveConnections
|
@ -8,6 +8,7 @@ import AutomergeCollaboration from '@hiveteams/collab-backend/lib/AutomergeColla
|
|||||||
import withIOCollaboration from './withIOCollaboration'
|
import withIOCollaboration from './withIOCollaboration'
|
||||||
import { AutomergeOptions, SocketIOPluginOptions } from './interfaces'
|
import { AutomergeOptions, SocketIOPluginOptions } from './interfaces'
|
||||||
import { getTarget } from '@hiveteams/collab-bridge/src/path'
|
import { getTarget } from '@hiveteams/collab-bridge/src/path'
|
||||||
|
import getActiveConnections from '@hiveteams/collab-backend/src/utils/getActiveConnections'
|
||||||
|
|
||||||
const connectionSlug = 'test'
|
const connectionSlug = 'test'
|
||||||
const docId = `/${connectionSlug}`
|
const docId = `/${connectionSlug}`
|
||||||
@ -50,6 +51,12 @@ const collabBackend = new AutomergeCollaboration({
|
|||||||
},
|
},
|
||||||
async onDocumentLoad(pathname) {
|
async onDocumentLoad(pathname) {
|
||||||
return defaultSlateJson
|
return defaultSlateJson
|
||||||
|
},
|
||||||
|
onTrace(metaData, computationFn) {
|
||||||
|
if (metaData.opCount && metaData.opCount > 100) {
|
||||||
|
}
|
||||||
|
console.log('metaData', metaData)
|
||||||
|
computationFn()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -60,6 +67,10 @@ describe('automerge editor client tests', () => {
|
|||||||
server.listen(5000, () => done())
|
server.listen(5000, () => done())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
afterEach(done => {
|
||||||
|
waitForCondition(() => !collabBackend.backend.getDocument(docId)).then(done)
|
||||||
|
})
|
||||||
|
|
||||||
const createCollabEditor = async (
|
const createCollabEditor = async (
|
||||||
editorOptions?: Partial<AutomergeOptions> & Partial<SocketIOPluginOptions>
|
editorOptions?: Partial<AutomergeOptions> & Partial<SocketIOPluginOptions>
|
||||||
) => {
|
) => {
|
||||||
@ -232,6 +243,8 @@ describe('automerge editor client tests', () => {
|
|||||||
expect(editor.children.length).toEqual(2)
|
expect(editor.children.length).toEqual(2)
|
||||||
expect(Node.string(editor.children[0])).toEqual('new')
|
expect(Node.string(editor.children[0])).toEqual('new')
|
||||||
expect(Node.string(editor.children[1])).toEqual('nodes')
|
expect(Node.string(editor.children[1])).toEqual('nodes')
|
||||||
|
|
||||||
|
editor.destroy()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('set node for children with missing value should not throw error', () => {
|
it('set node for children with missing value should not throw error', () => {
|
||||||
@ -265,6 +278,37 @@ describe('automerge editor client tests', () => {
|
|||||||
expect(target).toEqual(null)
|
expect(target).toEqual(null)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should work with concurrent insert text operations', async () => {
|
||||||
|
const editor1 = await createCollabEditor()
|
||||||
|
const editor2 = await createCollabEditor()
|
||||||
|
|
||||||
|
editor1.disconnect()
|
||||||
|
|
||||||
|
await waitForCondition(() => {
|
||||||
|
return getActiveConnections(collabBackend.backend, docId) === 1
|
||||||
|
})
|
||||||
|
|
||||||
|
editor2.insertNode({ type: 'paragraph', children: [{ text: 'hi' }] })
|
||||||
|
|
||||||
|
await waitForCondition(() => {
|
||||||
|
return collabBackend.backend.getDocument(docId)?.children.length === 2
|
||||||
|
})
|
||||||
|
|
||||||
|
editor1.connect()
|
||||||
|
|
||||||
|
await waitForCondition(() => {
|
||||||
|
return editor1.children.length === 2
|
||||||
|
})
|
||||||
|
|
||||||
|
editor2.destroy()
|
||||||
|
|
||||||
|
await waitForCondition(() => {
|
||||||
|
return getActiveConnections(collabBackend.backend, docId) === 1
|
||||||
|
})
|
||||||
|
|
||||||
|
editor1.destroy()
|
||||||
|
})
|
||||||
|
|
||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
collabBackend.destroy()
|
collabBackend.destroy()
|
||||||
server.close()
|
server.close()
|
||||||
|
Loading…
Reference in New Issue
Block a user