Skip to content

Commit

Permalink
Fixed formatting in events
Browse files Browse the repository at this point in the history
  • Loading branch information
josephg committed Oct 31, 2011
1 parent b68e976 commit 292d34f
Showing 1 changed file with 47 additions and 49 deletions.
96 changes: 47 additions & 49 deletions src/server/events.coffee
Expand Up @@ -15,58 +15,56 @@ module.exports = (model) ->
else
emitters[docName]

{
# Hook for model code. This is called every time an op is committed to the document.
onApplyOp: (docName, opData) ->
p "onApplyOp #{docName} #{i opData} - #{emitterForDoc(docName)?.listeners('op')}"
emitterForDoc(docName)?.emit('op', opData)

# Registers a listener for ops on a particular document.
# callback(startingVersion) is called when the listener is first applied. All ops
# from then on are sent to the user.
# Listeners are of the form listener(op, appliedAt)
listen: (docName, listener, callback) ->
model.getVersion docName, (error, version) ->
# Hook for model code. This is called every time an op is committed to the document.
onApplyOp: (docName, opData) ->
p "onApplyOp #{docName} #{i opData} - #{emitterForDoc(docName)?.listeners('op')}"
emitterForDoc(docName)?.emit('op', opData)

# Registers a listener for ops on a particular document.
# callback(startingVersion) is called when the listener is first applied. All ops
# from then on are sent to the user.
# Listeners are of the form listener(op, appliedAt)
listen: (docName, listener, callback) ->
model.getVersion docName, (error, version) ->
return callback? error if error

# Only attach the listener if the document exists (ie, if version != null)
emitterForDoc(docName, yes).on 'op', listener
callback? null, version

# Remove a listener from a particular document.
removeListener: (docName, listener) ->
emitterForDoc(docName)?.removeListener('op', listener)
p 'Listeners: ' + (i emitterForDoc(docName)?.listeners('op'))

removeAllListeners: (docName) ->
emitterForDoc(docName)?.removeAllListeners 'op'

# Listen to all ops from the specified version. The version cannot be in the
# future.
# The callback is called once the listener is attached. removeListener() will be
# ineffective before then.
# Callback(version) if the listener is attached
# Callback(null) if the document doesn't exist
listenFromVersion: (docName, version, listener, callback) ->
model.getVersion docName, (error, docVersion) ->
return callback? error if error

version = docVersion if version > docVersion

# The listener isn't attached until we have the historical ops from the database.
model.getOps docName, version, null, (error, data) ->
return callback? error if error

# Only attach the listener if the document exists (ie, if version != null)
emitterForDoc(docName, yes).on 'op', listener
emitter = emitterForDoc(docName, yes)
emitter.on 'op', listener
callback? null, version
p 'Listener added -> ' + (i emitterForDoc(docName)?.listeners('op'))

# Remove a listener from a particular document.
removeListener: (docName, listener) ->
emitterForDoc(docName)?.removeListener('op', listener)
p 'Listeners: ' + (i emitterForDoc(docName)?.listeners('op'))

removeAllListeners: (docName) ->
emitterForDoc(docName)?.removeAllListeners 'op'

# Listen to all ops from the specified version. The version cannot be in the
# future.
# The callback is called once the listener is attached. removeListener() will be
# ineffective before then.
# Callback(version) if the listener is attached
# Callback(null) if the document doesn't exist
listenFromVersion: (docName, version, listener, callback) ->
model.getVersion docName, (error, docVersion) ->
return callback? error if error

version = docVersion if version > docVersion

# The listener isn't attached until we have the historical ops from the database.
model.getOps docName, version, null, (error, data) ->
return callback? error if error

emitter = emitterForDoc(docName, yes)
emitter.on 'op', listener
callback? null, version
p 'Listener added -> ' + (i emitterForDoc(docName)?.listeners('op'))

for op_data in data
listener op_data
for op_data in data
listener op_data

# The listener may well remove itself during the catchup phase. If this happens, break early.
# This is done in a quite inefficient way. (O(n) where n = #listeners on doc)
break unless listener in emitter.listeners('op')
}
# The listener may well remove itself during the catchup phase. If this happens, break early.
# This is done in a quite inefficient way. (O(n) where n = #listeners on doc)
break unless listener in emitter.listeners('op')

0 comments on commit 292d34f

Please sign in to comment.