feat: garbage cursors on client

pull/4/head
cudr 5 years ago
parent 6ec91963c6
commit 8d7a10be1b

@ -16,7 +16,7 @@ class Connection {
private options: ConnectionOptions
constructor(options: ConnectionOptions = defaultOptions) {
this.io = io(options.port, options.connectOpts)
this.io = io(options.entry, options.connectOpts)
this.docSet = new Automerge.DocSet()
this.connections = {}
this.options = merge(defaultOptions, options)

@ -1,7 +1,8 @@
import { ValueJSON } from 'slate'
import { Server } from 'http'
export interface ConnectionOptions {
port: number
entry: number | Server
connectOpts?: SocketIO.ServerOptions
defaultValue?: ValueJSON
saveTreshold?: number

@ -6,7 +6,7 @@ export const getClients = (io, nsp) =>
})
export const defaultOptions = {
port: 9000,
entry: 9000,
saveTreshold: 2000,
cursorAnnotationType: 'collaborative_selection'
}

@ -63,14 +63,14 @@ class Connection {
return
}
const currentDoc = this.docSet.getDoc(this.docId)
const docNew = this.connection.receiveMsg(data)
try {
const currentDoc = this.docSet.getDoc(this.docId)
const docNew = this.connection.receiveMsg(data)
if (!docNew) {
return
}
if (!docNew) {
return
}
try {
const operations = Automerge.diff(currentDoc, docNew)
if (operations.length !== 0) {
@ -87,12 +87,42 @@ class Connection {
await Promise.resolve()
this.editor.remote = false
this.garbageCursors()
}
} catch (e) {
console.error(e)
}
}
garbageCursors = async () => {
const doc = this.docSet.getDoc(this.docId)
const { value } = this.editor
if (value.annotations.size === Object.keys(doc.annotations).length) {
return
}
const garbage = []
value.annotations.forEach(annotation => {
if (
annotation.type === this.cursorAnnotationType &&
!doc.annotations[annotation.key]
) {
garbage.push(annotation)
}
})
if (garbage.length) {
this.editor.withoutSaving(() => {
garbage.forEach(annotation => {
this.editor.removeAnnotation(annotation)
})
})
}
}
receiveSlateOps = (operations: Immutable.List<Operation>) => {
const doc = this.docSet.getDoc(this.docId)
const message = `change from ${this.socket.id}`

@ -1,8 +1,9 @@
const Connection = require('@slate-collaborative/backend')
const defaultValue = require('./src/defaultValue')
const server = require('http').createServer()
const config = {
port: 9000,
entry: server, // or specify port to start io server
defaultValue,
saveTreshold: 2000,
onAuthRequest: async (query, socket) => {
@ -19,3 +20,5 @@ const config = {
}
const connection = new Connection(config)
server.listen(9000)

Loading…
Cancel
Save