Commit a161abcc authored by Gerben's avatar Gerben Committed by Aron Carroll

Shuffle around lines of code

parent 49318aa4
......@@ -24,8 +24,37 @@ class AnnotationSync
on: (event, handler) ->
throw new Error('options.on unspecified for AnnotationSync.')
# Cache of annotations which have crossed the bridge for fast, encapsulated
# association of annotations received in arguments to window-local copies.
cache: null
constructor: (options, bridge) ->
@options = $.extend(true, {}, @options, options)
@cache = {}
@_on = @options.on
@_emit = @options.emit
# Listen locally for interesting events
for event, handler of @_eventListeners
this._on(event, handler)
# Register remotely invokable methods
for method, func in @_channelListeners
bridge.on(method, func)
# Upon new connections, send over the items in our cache
onConnect = (channel) =>
this._syncCache(channel)
bridge.onConnect(onConnect)
getAnnotationForTag: (tag) ->
@cache[tag] or null
# Handlers for messages arriving through a channel
channelListeners:
_channelListeners:
'beforeCreateAnnotation': (txn, annotation) =>
annotation = this._parse annotation
delete @cache[annotation.$$tag]
......@@ -63,30 +92,38 @@ class AnnotationSync
annotations = (this._parse a for a in annotations)
@_emit('loadAnnotations', annotations)
# Cache of annotations which have crossed the bridge for fast, encapsulated
# association of annotations received in arguments to window-local copies.
cache: null
constructor: (options, bridge) ->
@options = $.extend(true, {}, @options, options)
@cache = {}
@_on = @options.on
@_emit = @options.emit
# Listen locally for interesting events
for event, handler of @eventListeners
this._on(event, handler)
onConnect = (channel) =>
# Upon new connections, send over the items in our cache
this._syncCache(channel)
bridge.onConnect(onConnect)
# Register remotely invokable methods
for method, func in @channelListeners
bridge.on(method, func)
# Handlers for events coming from this frame, to send them across the channel
_eventListeners:
'beforeAnnotationCreated': (event, annotation) =>
return if annotation.$$tag?
this._mkCallRemotelyAndParseResults('beforeCreateAnnotation')(annotation)
this
'annotationCreated': (event, annotation) =>
return unless annotation.$$tag? and @cache[annotation.$$tag]
this._mkCallRemotelyAndParseResults('createAnnotation')(annotation)
this
'annotationUpdated': (event, annotation) =>
return unless annotation.$$tag? and @cache[annotation.$$tag]
this._mkCallRemotelyAndParseResults('updateAnnotation')(annotation)
this
'annotationDeleted': (event, annotation) =>
return unless annotation.$$tag? and @cache[annotation.$$tag]
onFailure = (err) =>
if err then @annotator.setupAnnotation annotation # TODO
else delete @cache[annotation.$$tag]
this._mkCallRemotelyAndParseResults('deleteAnnotation', onFailure)(annotation)
this
'annotationsLoaded': (event, annotations) =>
annotations = (this._format a for a in annotations when not a.$$tag)
return unless annotations.length
this._notify
method: 'loadAnnotations'
params: annotations
this
_syncCache: (channel) =>
# Synchronise (here to there) the items in our cache
......@@ -96,18 +133,6 @@ class AnnotationSync
method: 'loadAnnotations'
params: annotations
getAnnotationForTag: (tag) ->
@cache[tag] or null
# Handlers for events coming from this frame, to send them across the channel
eventListeners:
'beforeAnnotationCreated': @beforeAnnotationCreated
'annotationCreated': @annotationCreated
'annotationUpdated': @annotationUpdated
'annotationDeleted': @annotationDeleted
'annotationsLoaded': @annotationsLoaded
_mkCallRemotelyAndParseResults: (method, callBack) ->
fn = (annotation) ->
# Wrap the callback function to first parse returned items
......@@ -123,37 +148,6 @@ class AnnotationSync
params: this._format(annotation)
@bridge.call(options)
beforeAnnotationCreated: (event, annotation) =>
return if annotation.$$tag?
this._mkCallRemotelyAndParseResults('beforeCreateAnnotation')(annotation)
this
annotationCreated: (event, annotation) =>
return unless annotation.$$tag? and @cache[annotation.$$tag]
this._mkCallRemotelyAndParseResults('createAnnotation')(annotation)
this
annotationUpdated: (event, annotation) =>
return unless annotation.$$tag? and @cache[annotation.$$tag]
this._mkCallRemotelyAndParseResults('updateAnnotation')(annotation)
this
annotationDeleted: (event, annotation) =>
return unless annotation.$$tag? and @cache[annotation.$$tag]
onFailure = (err) =>
if err then @annotator.setupAnnotation annotation # TODO
else delete @cache[annotation.$$tag]
this._mkCallRemotelyAndParseResults('deleteAnnotation', onFailure)(annotation)
this
annotationsLoaded: (event, annotations) =>
annotations = (this._format a for a in annotations when not a.$$tag)
return unless annotations.length
this._notify
method: 'loadAnnotations'
params: annotations
this
# Parse returned annotations to update cache with any changes made remotely
_parseResults: (results) ->
for annotations in results
......
class CrossFrameBridge
# Connected links to other frames
links: null
options:
# Scope identifier to distinguish this channel from any others
scope: 'crossFrameBridge'
......@@ -16,6 +12,9 @@ class CrossFrameBridge
# Any callbacks for messages on the channel. Max one callback per method.
channelListeners: {}
# Connected links to other frames
links: null
constructor: (options) ->
@options = $.extend(true, {}, @options, options)
@onConnectListeners = [@options.onConnect]
......@@ -32,8 +31,6 @@ class CrossFrameBridge
onReady: (channel) =>
for callback in @onConnectListeners
callback.call(this, channel, source)
# Create the channel
channel = this._buildChannel channelOptions
# Attach channel message listeners
......@@ -47,14 +44,6 @@ class CrossFrameBridge
channel
# Construct a channel to another frame
_buildChannel: (options) ->
# jschannel chokes on FF and Chrome extension origins.
if (options.origin.match /^chrome-extension:\/\//) or
(options.origin.match /^resource:\/\//)
options = $.extend {}, options, {origin: '*'}
channel = Channel.build(options)
# Make a method call on all links, collect the results and pass them to a
# callback when all results are collected. Parameters:
# - options.method (required): name of remote method to call
......@@ -109,3 +98,12 @@ class CrossFrameBridge
# Add a function to be called upon a new connection
onConnect: (callback) ->
@onConnectListeners.push(callback)
# Construct a channel to another frame
_buildChannel: (options) ->
# jschannel chokes on FF and Chrome extension origins.
if (options.origin.match /^chrome-extension:\/\//) or
(options.origin.match /^resource:\/\//)
options = $.extend {}, options, {origin: '*'}
channel = Channel.build(options)
......@@ -33,6 +33,7 @@ class CrossFrameDiscovery
$(window).off 'message', this._onMessage
return
# Send out a beacon to discover frames to connect with
_beacon: ->
beaconMessage = if @options.server
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment