Merge pull request #3 from hiveteams/feature/add-tracing

Feature/add tracing
This commit is contained in:
emaciel10 2021-03-03 10:23:03 -05:00 committed by GitHub
commit ecceda91a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 80 additions and 22 deletions

View File

@ -2,8 +2,16 @@
Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
#### [v0.7.27](https://github.com/hiveteams/slate-collaborative/compare/v0.7.27...v0.7.27)
- feat: add tracing [`e3ce984`](https://github.com/hiveteams/slate-collaborative/commit/e3ce9849413ed33cc4d1324d52f808da7dd59822)
- chore: fix collab-backend description [`2681254`](https://github.com/hiveteams/slate-collaborative/commit/2681254c7a08240e8a7ee9d491735c218d7f0fa2)
- chore: fix collab-client description [`5cdcfc6`](https://github.com/hiveteams/slate-collaborative/commit/5cdcfc68577baabdb680ed4c8547374ee85e686b)
#### [v0.7.27](https://github.com/hiveteams/slate-collaborative/compare/v0.7.26...v0.7.27) #### [v0.7.27](https://github.com/hiveteams/slate-collaborative/compare/v0.7.26...v0.7.27)
> 29 January 2021
- chore: code and type cleanup [`#2`](https://github.com/hiveteams/slate-collaborative/pull/2) - chore: code and type cleanup [`#2`](https://github.com/hiveteams/slate-collaborative/pull/2)
#### [v0.7.26](https://github.com/hiveteams/slate-collaborative/compare/v0.7.25...v0.7.26) #### [v0.7.26](https://github.com/hiveteams/slate-collaborative/compare/v0.7.25...v0.7.26)

View File

@ -1,6 +1,6 @@
{ {
"lerna": "2.7.1", "lerna": "2.7.1",
"version": "0.7.27", "version": "0.7.28",
"npmClient": "yarn", "npmClient": "yarn",
"useWorkspaces": true "useWorkspaces": true
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "@hiveteams/collab-backend", "name": "@hiveteams/collab-backend",
"version": "0.7.27", "version": "0.7.28",
"files": [ "files": [
"lib" "lib"
], ],

View File

@ -3,10 +3,24 @@ import * as Automerge from 'automerge'
import { Node } from 'slate' import { Node } from 'slate'
import { Server } from 'http' import { Server } from 'http'
import throttle from 'lodash/throttle' import throttle from 'lodash/throttle'
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'
export interface IAutomergeMetaData {
docId: string
userId: string
type: string
opText?: string
opCount?: number
}
export type ITraceFunction = (
metaData: IAutomergeMetaData,
computationFunction: () => void
) => void
export interface IAutomergeCollaborationOptions { export interface IAutomergeCollaborationOptions {
entry: Server entry: Server
connectOpts?: SocketIO.ServerOptions connectOpts?: SocketIO.ServerOptions
@ -21,6 +35,11 @@ export interface IAutomergeCollaborationOptions {
) => Promise<void> | void ) => Promise<void> | void
onDisconnect?: (docId: string, user: any) => Promise<void> | void onDisconnect?: (docId: string, user: any) => Promise<void> | void
onError?: (error: Error, data: any) => Promise<void> | void onError?: (error: Error, data: any) => Promise<void> | void
onTrace?: ITraceFunction
}
const defaultOnTrace: ITraceFunction = (metaData, computation) => {
computation()
} }
export default class AutomergeCollaboration { export default class AutomergeCollaboration {
@ -29,6 +48,7 @@ export default class AutomergeCollaboration {
public backend: AutomergeBackend public backend: AutomergeBackend
private userMap: { [key: string]: any | undefined } private userMap: { [key: string]: any | undefined }
private autoSaveDoc: (socket: SocketIO.Socket, docId: string) => void private autoSaveDoc: (socket: SocketIO.Socket, docId: string) => void
private onTrace: ITraceFunction
/** /**
* Constructor * Constructor
@ -45,6 +65,8 @@ export default class AutomergeCollaboration {
this.userMap = {} this.userMap = {}
this.onTrace = options.onTrace || defaultOnTrace
/** /**
* Save document with throttle * Save document with throttle
*/ */
@ -182,11 +204,20 @@ export default class AutomergeCollaboration {
return return
} }
// Emit the socket message needed for receiving the automerge document const user = this.userMap[id]
// on connect and reconnect const metaData: IAutomergeMetaData = {
socket.emit('msg', { type: 'connect',
type: 'document', userId: user?._id,
payload: Automerge.save<SyncDoc>(doc) docId
}
this.onTrace(metaData, () => {
// Emit the socket message needed for receiving the automerge document
// on connect and reconnect
socket.emit('msg', {
type: 'document',
payload: Automerge.save<SyncDoc>(doc)
})
}) })
debugCollabBackend('Open connection %s', id) debugCollabBackend('Open connection %s', id)
@ -205,18 +236,37 @@ export default class AutomergeCollaboration {
data: any data: any
) => { ) => {
const { id } = socket const { id } = socket
switch (data.type) { const user = this.userMap[id]
case 'operation':
try {
this.backend.receiveOperation(id, data)
this.autoSaveDoc(socket, docId) // parse out the changes contained in this automerge change
const collabActions = flatten(
this.garbageCursors(socket) data.payload.changes?.map((change: Automerge.Change) =>
} catch (err) { change.ops.map(op => op.action)
this.handleError(socket, err, { onMessageData: data }) )
} )
const metaData: IAutomergeMetaData = {
type: 'operation',
userId: user?._id,
docId,
// cap max operation text by 1000 chars
opText: collabActions.join(',').slice(0, 1000),
opCount: collabActions.length
} }
this.onTrace(metaData, () => {
switch (data.type) {
case 'operation':
try {
this.backend.receiveOperation(id, data)
this.autoSaveDoc(socket, docId)
this.garbageCursors(socket)
} catch (err) {
this.handleError(socket, err, { onMessageData: data })
}
}
})
} }
/** /**

View File

@ -1,6 +1,6 @@
{ {
"name": "@hiveteams/collab-client", "name": "@hiveteams/collab-client",
"version": "0.7.27", "version": "0.7.28",
"files": [ "files": [
"lib" "lib"
], ],
@ -42,7 +42,7 @@
"@babel/plugin-proposal-object-rest-spread": "^7.5.5", "@babel/plugin-proposal-object-rest-spread": "^7.5.5",
"@babel/preset-env": "^7.6.0", "@babel/preset-env": "^7.6.0",
"@babel/preset-typescript": "^7.6.0", "@babel/preset-typescript": "^7.6.0",
"@hiveteams/collab-backend": "^0.7.27", "@hiveteams/collab-backend": "^0.7.28",
"@types/jest": "^24.9.0", "@types/jest": "^24.9.0",
"@types/react": "^16.9.34", "@types/react": "^16.9.34",
"@types/socket.io-client": "^1.4.32", "@types/socket.io-client": "^1.4.32",

View File

@ -1,12 +1,12 @@
{ {
"name": "@hiveteams/collab-example", "name": "@hiveteams/collab-example",
"version": "0.7.27", "version": "0.7.28",
"private": true, "private": true,
"dependencies": { "dependencies": {
"@emotion/core": "^10.0.17", "@emotion/core": "^10.0.17",
"@emotion/styled": "^10.0.17", "@emotion/styled": "^10.0.17",
"@hiveteams/collab-backend": "^0.7.27", "@hiveteams/collab-backend": "^0.7.28",
"@hiveteams/collab-client": "^0.7.27", "@hiveteams/collab-client": "^0.7.28",
"@types/faker": "^4.1.5", "@types/faker": "^4.1.5",
"@types/is-url": "^1.2.28", "@types/is-url": "^1.2.28",
"@types/jest": "24.0.18", "@types/jest": "24.0.18",