Last active
August 23, 2017 12:24
-
-
Save vvatikiotis/e112a9d239d8f9f73e84dcaac34ae98c to your computer and use it in GitHub Desktop.
WIP: Client-server PoC using gulf. Incomplete so far. Changes flow from server to client but not the opposite direction.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const textOT = require('ot-text').type | |
const gulf = require('gulf') | |
const net = require('net') | |
var doc = new gulf.EditableDocument({ | |
storageAdapter: new gulf.MemoryAdapter, | |
ottype: textOT, | |
}) | |
let content | |
doc._onBeforeChange = function() { | |
console.log('onBeforeChange') | |
return Promise.resolve() | |
} | |
doc._onChange = function(cs) { | |
console.log(`onChange: ${cs}`) | |
content = textOT.apply(content, cs) | |
return Promise.resolve() | |
} | |
doc._setContent = function(newcontent) { | |
console.log(`setContent: ${newcontent}`) | |
content = newcontent | |
return Promise.resolve() | |
} | |
var master = doc.masterLink() | |
const socket = net.connect(8080, function() { | |
socket.pipe(master).pipe(socket) | |
setTimeout(() => doc.submitChange([1, 'r']), 3000) | |
}) | |
socket.on('pipe', (src) => console.log(`Pipping to client`)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const gulf = require('gulf') | |
var textOT = require('ot-text').type | |
const net = require('net') | |
var doc = new gulf.Document({ | |
storageAdapter: new gulf.MemoryAdapter, | |
ottype: textOT, | |
}) | |
doc.initializeFromStorage('abc') | |
const sock = net.createServer((socket) => { | |
console.log('starting net server') | |
const slave = doc.slaveLink() | |
doc.on('commit', (a, b) => console.log(`server: ${JSON.stringify(a)}, ${b}`)) | |
socket.pipe(slave).pipe(socket) | |
}).listen(8080) | |
sock.on('pipe', (src) => console.log(`1. pipping to server`)) | |
sock.on('error', (err) => console.log('error')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
False alarm. I was expecting the
_onChange
handler of client doc to fire whensubmitChange
is called (within that same doc), whereas the documentation specifically states that_onChange
fires "for every change that is received from master".I have synced docs :)