Skip to content

Instantly share code, notes, and snippets.

@josephg
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephg/4e5df0b5190d48159d6f to your computer and use it in GitHub Desktop.
Save josephg/4e5df0b5190d48159d6f to your computer and use it in GitHub Desktop.
ShareJS query bug repro
{Duplex} = require 'stream'
livedb = require 'livedb'
sharejs = require './lib'
backend = livedb.client livedb.memory()
#backend = livedb.client livedbMongo('localhost:27017/test?auto_reconnect', safe:false)
share = sharejs.server.createClient {backend}
share.use 'after submit', (req, callback) ->
console.log 'after submit', req.opData
if req.docName is 'doc'
backend.submit 'c', 'doc', {op:['x'], v:1}, {}, (err) ->
throw err if err
callback()
else
callback()
client = {readyState: 1}
stream = new Duplex objectMode:yes
stream._write = (chunk, encoding, callback) ->
console.log 's->c ', JSON.stringify(chunk)
if client.state isnt 'closed' # silently drop messages after the session is closed
client.onmessage? data:chunk
callback()
stream._read = ->
client.send = (data) ->
data = JSON.parse data if typeof data is 'string'
console.log 'c->s ', JSON.stringify(data)
stream.push data
connection = new sharejs.client.Connection(client)
share.listen stream
connection.get('c', 'doc2').create('text', 'foo')
connection.get('c', 'doc3').create('text', 'foo')
connection.get('c', 'doc4').create('text', 'foo')
connection.get('c', 'doc5').create('text', 'foo')
connection.get('c', 'doc6').create('text', 'foo')
setTimeout ->
connection.createSubscribeQuery 'c', {}, {docMode:'sub'}, (err, q) ->
#console.log 'q', q
doc = connection.get 'c', 'doc'
doc.create 'text', 'hi', ->
console.log 'created'
, 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment