Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
socket.on('join_document', function({clientId, docId}, callback) {
// Permissions check
if (!hasPermission(clientId, docId)) { return }
docId = Number(docId)
if (!docSet.getDoc(docId)) {
createNewDocument(docId)
}
if (!connections[clientId]) {
connections[clientId] = new Automerge.Connection(
docSet,
(message) => {
if (!hasPermission(clientId, message.docId)) { return }
socket.emit("send_operation", message)
}
)
connections[clientId].open()
}
socket.join(docId)
callback(true)
});
private onConnect = socket => {
const { id, conn } = socket
const { name } = socket.nsp
const doc = this.docSet.getDoc(name)
const data = Automerge.save(doc)
this.connections[id] = new Automerge.Connection(this.docSet, data => {
socket.emit('operation', { id: conn.id, ...data })
})
socket.join(id, () => {
this.connections[id].open()
socket.emit('document', data)
})
socket.on('operation', this.onOperation(id, name))
socket.on('disconnect', this.onDisconnect(id, socket))
this.garbageCursors(name)
}
constructor(props) {
super(props)
this.doc = Automerge.init();
this.docSet = new Automerge.DocSet()
this.onChange = this.onChange.bind(this)
this.connection = new Automerge.Connection(
this.docSet,
(msg) => {
this.props.sendMessage(this.props.clientId, msg)
}
)
const initialValue = automergeJsonToSlate({
"document": { ...this.doc.note }
})
const initialSlateValue = Value.fromJSON(initialValue)
this.state = {
value: initialSlateValue,
online: true,
}
}
getOrCreatePeer(id, name, handler) {
if (!this.Peers[id]) {
let peer = new Peer(id, name, handler, this.wrtc)
this.Peers[id] = peer
this.connections[id] = new Automerge.Connection(this.docSet, msg => {
console.log('send to ' + id + ':', msg)
peer.send(msg)
})
peer.on('message', msg => {
console.log('receive from ' + id + ':', msg)
this.connections[id].receiveMsg(msg)
})
peer.on('closed', () => {
this.connections[id].close()
delete this.connections[id]
delete this.Peers[id]
})
this.connections[id].open()
const Automerge = require('automerge')
const CodeMirror = require('codemirror')
const automergeCodeMirror = require('../../index')
const docId = 'doc-id'
let leftConnection
let rightConnection
const leftDocSet = new Automerge.DocSet()
leftConnection = new Automerge.Connection(leftDocSet, msg =>
rightConnection.receiveMsg(msg)
)
leftConnection.open()
const rightDocSet = new Automerge.DocSet()
rightConnection = new Automerge.Connection(rightDocSet, msg =>
leftConnection.receiveMsg(msg)
)
rightConnection.open()
function createCodeMirror(docSet, domId) {
const $e = document.getElementById(domId)
const codeMirror = CodeMirror($e)
const getDocText = doc => doc.text
const updateDoc = doc => docSet.setDoc(docId, doc)
const { automergeHandler, codeMirrorHandler } = automergeCodeMirror({
this.socket.on('connect', () => {
this.connection = new Automerge.Connection(this.docSet, this.sendData)
this.socket.on('document', this.recieveDocument)
this.socket.on('operation', this.recieveData)
this.socket.on('disconnect', this.disconnect)
})
}
function createConnectedDocs(count: number, id: string): WatchableDoc[] {
const docSets: DocSet[] = []
for (let i = 0; i < count; i++) {
const docSet = new DocSet()
docSets.push(docSet)
if (i > 0) {
let connectionA: Connection
let connectionB: Connection
connectionA = new Connection(docSet, msg => connectionB.receiveMsg(msg))
connectionB = new Connection(docSets[i - 1], msg =>
connectionA.receiveMsg(msg)
)
connectionA.open()
connectionB.open()
}
}
return docSets.map(docSet => new DocSetWatchableDoc(docSet, id))
}
function createConnectedDocs(count: number, id: string): WatchableDoc[] {
const docSets: DocSet[] = []
for (let i = 0; i < count; i++) {
const docSet = new DocSet()
docSets.push(docSet)
if (i > 0) {
let connectionA: Connection
let connectionB: Connection
connectionA = new Connection(docSet, msg => connectionB.receiveMsg(msg))
connectionB = new Connection(docSets[i - 1], msg =>
connectionA.receiveMsg(msg)
)
connectionA.open()
connectionB.open()
}
}
return docSets.map(docSet => new DocSetWatchableDoc(docSet, id))
}
constructor (docSet, socket) {
this.automerge = new Automerge.Connection(docSet, msg => this.sendMsg(msg))
this.socket = socket
this.buffer = Buffer.alloc(0)
this.automerge.open()
}