Thanks for your reply.
In my package, several users (for example, Alice and Bob) edit a file at the same time online. Whenever a user modifies the text (e.g. Alice appends a ‘hello world’), the package sends the change to other users (Bob). I want other users (Bob) apply this change in his local TextBuffer. However, if I use setTextInRange() to apply this change, this change will trigger a didChange event for this user (Bob). Then the didChange callback will treat the change as the user’s action and send the change to other users(Alice) to ask them to apply the change again…
I think I should identify whether the change is did by the user or by the package. Currently, I set a bool flag after the setTextInRange() and skip the didChange callback if the flag is true. But I think this method is not reliable since I can’t guarantee the next didChange event is the one triggered by the setTextInRange()
see the code below:
console.log 'BirdwayAtom was toggled!'
if @watching == false
@watching = true
@socketlog = fs.openSync '/tmp/socketlog', 'w'
# watch the text change event
if editor = atom.workspace.getActiveTextEditor()
if buffer = editor.getBuffer()
@handler = buffer.onDidChange (diff) =>
console.log "skip = false"
BirdwayAtom.skip = false
for k,v of diff
console.log k + ":" + v
jsondata = JSON.stringify diff
fs.writeSync @socketlog, (jsondata + "\n")
@sock.write (jsondata + "\n")
# open the socket to the client
@sock = new net.Socket()
@sock.on 'error', (err)=>
console.log ("socket error: " + err)
@watching = false
# @sock.on 'data', (data) =>
# console.log data
carrier.carry(@sock).on 'line', (line)=>
console.log "got one line:" + line
diff = JSON.parse line
cBuffer = atom.workspace.getActiveTextEditor().getBuffer()
console.log "skip = true"
BirdwayAtom.skip = true
cBuffer.setTextInRange diff.oldRange, diff.newText
@sock.connect 9527, '127.0.0.1', ->
console.log "connected to local client"