mirror of
https://github.com/cudr/slate-collaborative.git
synced 2024-10-27 20:34:06 +00:00
Merge pull request #3 from hiveteams/feature/add-tracing
Feature/add tracing
This commit is contained in:
commit
ecceda91a4
@ -2,8 +2,16 @@
|
||||
|
||||
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)
|
||||
|
||||
> 29 January 2021
|
||||
|
||||
- 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)
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"lerna": "2.7.1",
|
||||
"version": "0.7.27",
|
||||
"version": "0.7.28",
|
||||
"npmClient": "yarn",
|
||||
"useWorkspaces": true
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@hiveteams/collab-backend",
|
||||
"version": "0.7.27",
|
||||
"version": "0.7.28",
|
||||
"files": [
|
||||
"lib"
|
||||
],
|
||||
|
@ -3,10 +3,24 @@ import * as Automerge from 'automerge'
|
||||
import { Node } from 'slate'
|
||||
import { Server } from 'http'
|
||||
import throttle from 'lodash/throttle'
|
||||
import flatten from 'lodash/flatten'
|
||||
import { SyncDoc, CollabAction, toJS } from '@hiveteams/collab-bridge'
|
||||
import { debugCollabBackend } from './utils/debug'
|
||||
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 {
|
||||
entry: Server
|
||||
connectOpts?: SocketIO.ServerOptions
|
||||
@ -21,6 +35,11 @@ export interface IAutomergeCollaborationOptions {
|
||||
) => Promise<void> | void
|
||||
onDisconnect?: (docId: string, user: any) => Promise<void> | void
|
||||
onError?: (error: Error, data: any) => Promise<void> | void
|
||||
onTrace?: ITraceFunction
|
||||
}
|
||||
|
||||
const defaultOnTrace: ITraceFunction = (metaData, computation) => {
|
||||
computation()
|
||||
}
|
||||
|
||||
export default class AutomergeCollaboration {
|
||||
@ -29,6 +48,7 @@ export default class AutomergeCollaboration {
|
||||
public backend: AutomergeBackend
|
||||
private userMap: { [key: string]: any | undefined }
|
||||
private autoSaveDoc: (socket: SocketIO.Socket, docId: string) => void
|
||||
private onTrace: ITraceFunction
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
@ -45,6 +65,8 @@ export default class AutomergeCollaboration {
|
||||
|
||||
this.userMap = {}
|
||||
|
||||
this.onTrace = options.onTrace || defaultOnTrace
|
||||
|
||||
/**
|
||||
* Save document with throttle
|
||||
*/
|
||||
@ -182,11 +204,20 @@ export default class AutomergeCollaboration {
|
||||
return
|
||||
}
|
||||
|
||||
// Emit the socket message needed for receiving the automerge document
|
||||
// on connect and reconnect
|
||||
socket.emit('msg', {
|
||||
type: 'document',
|
||||
payload: Automerge.save<SyncDoc>(doc)
|
||||
const user = this.userMap[id]
|
||||
const metaData: IAutomergeMetaData = {
|
||||
type: 'connect',
|
||||
userId: user?._id,
|
||||
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)
|
||||
@ -205,18 +236,37 @@ export default class AutomergeCollaboration {
|
||||
data: any
|
||||
) => {
|
||||
const { id } = socket
|
||||
switch (data.type) {
|
||||
case 'operation':
|
||||
try {
|
||||
this.backend.receiveOperation(id, data)
|
||||
const user = this.userMap[id]
|
||||
|
||||
this.autoSaveDoc(socket, docId)
|
||||
|
||||
this.garbageCursors(socket)
|
||||
} catch (err) {
|
||||
this.handleError(socket, err, { onMessageData: data })
|
||||
}
|
||||
// parse out the changes contained in this automerge change
|
||||
const collabActions = flatten(
|
||||
data.payload.changes?.map((change: Automerge.Change) =>
|
||||
change.ops.map(op => op.action)
|
||||
)
|
||||
)
|
||||
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 })
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@hiveteams/collab-client",
|
||||
"version": "0.7.27",
|
||||
"version": "0.7.28",
|
||||
"files": [
|
||||
"lib"
|
||||
],
|
||||
@ -42,7 +42,7 @@
|
||||
"@babel/plugin-proposal-object-rest-spread": "^7.5.5",
|
||||
"@babel/preset-env": "^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/react": "^16.9.34",
|
||||
"@types/socket.io-client": "^1.4.32",
|
||||
|
@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "@hiveteams/collab-example",
|
||||
"version": "0.7.27",
|
||||
"version": "0.7.28",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"@emotion/core": "^10.0.17",
|
||||
"@emotion/styled": "^10.0.17",
|
||||
"@hiveteams/collab-backend": "^0.7.27",
|
||||
"@hiveteams/collab-client": "^0.7.27",
|
||||
"@hiveteams/collab-backend": "^0.7.28",
|
||||
"@hiveteams/collab-client": "^0.7.28",
|
||||
"@types/faker": "^4.1.5",
|
||||
"@types/is-url": "^1.2.28",
|
||||
"@types/jest": "24.0.18",
|
||||
|
Loading…
Reference in New Issue
Block a user