Skip to content

Commit

Permalink
Add vendored Robin.js and reactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
nybblr committed Sep 16, 2013
1 parent d4cd856 commit 06848be
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
70 changes: 70 additions & 0 deletions vendor/assets/javascripts/robin/reactor.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Utility class for updating models in memory.
Batman.Reactor =
# Private queue for jQuery
_q: $({})

# Valid batch operations
_verbs: ["updated", "created", "destroyed", "flushed", "batched"]

# Public interface for updating objects in Batman in a bulk/one off fashion
process: (verb, model, data) ->
if _.contains @_verbs, verb
@['_'+verb](model, data)
else
Batman.developer.warn("unrecognized batch operation: " + verb)

_enqueue: (model, batch_item) ->
@_q.queue (next) =>
@process(batch_item[0], model, batch_item[1])
next()

_getObject: (model, data) ->
# model = Batman.currentApp[pushed_data.model_name]
# data = pushed_data.model_data
obj = new model()
obj._withoutDirtyTracking -> obj.fromJSON(data)
return obj

# Flush every object of a certain model that matches the criterion (all comments for a post)
# used when you have too much data to pass through Pusher but want Batman to request updates
_flushed: (model, data) ->
# model = Batman.currentApp[reload_data.model_name]
match_key = data.match_key
match_value = data.match_value
Batman.developer.log("FLUSH #{model.name} - #{match_key} => #{match_value}")
recordsToRemove = model.get('loaded').indexedBy(match_key).get(match_value).toArray()
recordsToRemove.forEach (existing) =>
model.get('loaded').remove(existing)
if match_key == 'id'
model.find match_value, ->
else
options = {}
options["#{match_key}"] = match_value
model.load options

_batched: (model, batch) ->
return if batch == undefined
Batman.developer.log("BATCH: " + batch.length)
for batched_item in batch
@_enqueue(model, batched_item)

_created: (model, data) ->
Batman.developer.log("created: #{JSON.stringify(data)}")
obj = model.get('loaded.indexedByUnique.id').get(data["id"])
if obj # If object already in memory, update it
obj._withoutDirtyTracking -> obj.fromJSON(data)
else # create object in memory
obj = @_getObject(model, data)
model._mapIdentity(obj)

_updated: (model, data) ->
Batman.developer.log("updated #{JSON.stringify(data)}")
obj = model.get('loaded.indexedByUnique.id').get(data["id"])
if obj
obj._withoutDirtyTracking -> obj.fromJSON(data)

_destroyed: (model, data) ->
Batman.developer.log("destroyed #{JSON.stringify(data)}")
existing = model.get('loaded.indexedByUnique.id').get(data["id"])
if existing
model.get('loaded').remove(existing)
44 changes: 44 additions & 0 deletions vendor/assets/javascripts/robin/robin.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Push-persistence backed by Faye.
class Batman.Robin extends Batman.Object
@_nest: []
@_verbs: ["updated", "created", "destroyed", "flushed", "batched"]

@on 'socket:ready', ->
@socket = Batman.currentApp.socket
bird() for bird in @_nest

constructor: (@model) ->
if @socket
# Go ahead and subscribe.
@subscribe()
else
# Add it to the nest.
Batman.Robin._nest.push =>
@socket = Batman.Robin.socket
@subscribe()

subscribe: ->
channel = @model.storageKey
Batman.developer.log "Subscribing to /#{channel}..."
for verb in @constructor._verbs
do (verb) =>
@socket.subscribe "/#{channel}/#{verb}", (data) => @delayIfXhrRequests(verb, data)

delayIfXhrRequestsWithoutDecompress: (method, data) ->
if Batman.Robin.activeXhrCount == 0
setTimeout =>
Batman.developer.log("Processing #{method} with data #{JSON.stringify(data)}")
Batman.Reactor.process(method, @model, data)
, 0
else
Batman.developer.log("Delaying #{method}")
setTimeout =>
@delayIfXhrRequestsWithoutDecompress(method, data)
, 500

delayIfXhrRequests: (method, data) ->
@delayIfXhrRequestsWithoutDecompress(method, data)

@Robin = Batman.Robin
@Robin.activeXhrCount = 0
$(document).ajaxSend(=> @Robin.activeXhrCount++).ajaxComplete(=> @Robin.activeXhrCount--)

0 comments on commit 06848be

Please sign in to comment.