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')) |
Author
Author
False alarm. I was expecting the _onChange handler of client doc to fire when submitChange 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 :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed https://gist.github.com/vvatikiotis/e112a9d239d8f9f73e84dcaac34ae98c#file-server-js-L10, cheers :)
As for updating the
contentvariable. The client_onChangelistener doesn't fire, ever, and Im wondering why. The client document handlers (_onChange,_onBeforeChangeand_setContent) are implemented as per Gulf's tests. Andcontentvar is updated after the OT transform (https://gist.github.com/vvatikiotis/e112a9d239d8f9f73e84dcaac34ae98c#file-client-js-L19), but the_onChangedoesn't fire at all, so no change.One thing that might shed some light in my head: server and client are run within their own process.