Skip to content

Commit 5f9f3e4

Browse files
committed
Merge pull request #2425 from hypothesis/frame-rpc
Switch out jschannel for frame-rpc
2 parents 4371daa + f5b16aa commit 5f9f3e4

20 files changed

+318
-1157
lines changed

LICENSE

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -71,41 +71,6 @@ For the annotator subcomponent:
7171
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
7272
THE SOFTWARE.
7373

74-
For the jschannel subcomponent:
75-
76-
Version: MPL 1.1/GPL 2.0/LGPL 2.1
77-
78-
The contents of this file are subject to the Mozilla Public License Version
79-
1.1 (the "License"); you may not use this file except in compliance with
80-
the License. You may obtain a copy of the License at
81-
http://www.mozilla.org/MPL/
82-
83-
Software distributed under the License is distributed on an "AS IS" basis,
84-
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
85-
for the specific language governing rights and limitations under the
86-
License.
87-
88-
The Original Code is jschannel.
89-
90-
The Initial Developer of the Original Code is Lloyd Hilaiel.
91-
92-
Portions created by the Initial Developer are Copyright (C) 2010
93-
the Initial Developer. All Rights Reserved.
94-
95-
Contributor(s):
96-
97-
Alternatively, the contents of this file may be used under the terms of
98-
either the GNU General Public License Version 2 or later (the "GPL"), or
99-
the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
100-
in which case the provisions of the GPL or the LGPL are applicable instead
101-
of those above. If you wish to allow use of your version of this file only
102-
under the terms of either the GPL or the LGPL, and not to allow others to
103-
use your version of this file under the terms of the MPL, indicate your
104-
decision by deleting the provisions above and replace them with the notice
105-
and other provisions required by the GPL or the LGPL. If you do not delete
106-
the provisions above, a recipient may use your version of this file under
107-
the terms of any one of the MPL, the GPL or the LGPL.
108-
10974
For the jwz.js subcomponent:
11075

11176
Copyright (c) 2011 Frederik Dietz & Max Ogden

NOTICE

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ This product also includes the following third-party components:
1414

1515
Copyright 2012 Aron Carroll, Rufus Pollock, and Nick Stenning
1616

17-
* jschannel (https://github.com/mozilla/jschannel)
18-
19-
Copyright (C) 2010, Lloyd Hilaiel
20-
2117
* Gentleface icon set (http://www.gentleface.com/free_icon_set.html)
2218

2319
Copyright © 2006-2012 Gentleface

h/static/scripts/annotation-sync.coffee

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -58,49 +58,47 @@ module.exports = class AnnotationSync
5858
getAnnotationForTag: (tag) ->
5959
@cache[tag] or null
6060

61-
sync: (annotations, cb) ->
61+
sync: (annotations) ->
6262
annotations = (this._format a for a in annotations)
63-
@bridge.call
64-
method: 'sync'
65-
params: annotations
66-
callback: cb
63+
@bridge.call 'sync', annotations, (err, annotations = []) =>
64+
for a in annotations
65+
this._parse(a)
6766
this
6867

6968
# Handlers for messages arriving through a channel
7069
_channelListeners:
71-
'beforeCreateAnnotation': (txn, body) ->
70+
'beforeCreateAnnotation': (body, cb) ->
7271
annotation = this._parse(body)
7372
delete @cache[annotation.$$tag]
7473
@_emit 'beforeAnnotationCreated', annotation
7574
@cache[annotation.$$tag] = annotation
76-
this._format annotation
75+
cb(null, this._format(annotation))
7776

78-
'createAnnotation': (txn, body) ->
77+
'createAnnotation': (body, cb) ->
7978
annotation = this._parse(body)
8079
delete @cache[annotation.$$tag]
8180
@_emit 'annotationCreated', annotation
8281
@cache[annotation.$$tag] = annotation
83-
this._format annotation
82+
cb(null, this._format(annotation))
8483

85-
'updateAnnotation': (txn, body) ->
84+
'updateAnnotation': (body, cb) ->
8685
annotation = this._parse(body)
8786
delete @cache[annotation.$$tag]
8887
@_emit('beforeAnnotationUpdated', annotation)
8988
@_emit('annotationUpdated', annotation)
9089
@cache[annotation.$$tag] = annotation
91-
this._format annotation
90+
cb(null, this._format(annotation))
9291

93-
'deleteAnnotation': (txn, body) ->
92+
'deleteAnnotation': (body, cb) ->
9493
annotation = this._parse(body)
9594
delete @cache[annotation.$$tag]
9695
@_emit('annotationDeleted', annotation)
97-
res = this._format(annotation)
98-
res
96+
cb(null, this._format(annotation))
9997

100-
'sync': (ctx, bodies) ->
101-
(this._format(this._parse(b)) for b in bodies)
98+
'sync': (bodies, cb) ->
99+
cb(null, (this._format(this._parse(b)) for b in bodies))
102100

103-
'loadAnnotations': (txn, bodies) ->
101+
'loadAnnotations': (bodies) ->
104102
annotations = (this._parse(a) for a in bodies)
105103
@_emit('loadAnnotations', annotations)
106104

@@ -127,17 +125,13 @@ module.exports = class AnnotationSync
127125
'annotationsLoaded': (annotations) ->
128126
bodies = (this._format a for a in annotations when not a.$$tag)
129127
return unless bodies.length
130-
@bridge.notify
131-
method: 'loadAnnotations'
132-
params: bodies
128+
@bridge.call('loadAnnotations', bodies)
133129

134130
_syncCache: (channel) ->
135131
# Synchronise (here to there) the items in our cache
136132
annotations = (this._format a for t, a of @cache)
137133
if annotations.length
138-
channel.notify
139-
method: 'loadAnnotations'
140-
params: annotations
134+
channel.call('loadAnnotations', annotations)
141135

142136
_mkCallRemotelyAndParseResults: (method, callBack) ->
143137
(annotation) =>
@@ -148,11 +142,7 @@ module.exports = class AnnotationSync
148142
callBack? failure, results
149143

150144
# Call the remote method
151-
options =
152-
method: method
153-
callback: wrappedCallback
154-
params: this._format(annotation)
155-
@bridge.call(options)
145+
@bridge.call(method, this._format(annotation), wrappedCallback)
156146

157147
# Parse returned message bodies to update cache with any changes made remotely
158148
_parseResults: (results) ->

h/static/scripts/annotation-ui-sync.coffee

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ module.exports = class AnnotationUISync
1818
tags.map(annotationSync.getAnnotationForTag, annotationSync)
1919

2020
channelListeners =
21-
showAnnotations: (ctx, tags=[]) ->
21+
showAnnotations: (tags=[]) ->
2222
annotations = getAnnotationsByTags(tags)
2323
annotationUI.selectAnnotations(annotations)
24-
focusAnnotations: (ctx, tags=[]) ->
24+
focusAnnotations: (tags=[]) ->
2525
annotations = getAnnotationsByTags(tags)
2626
annotationUI.focusAnnotations(annotations)
27-
toggleAnnotationSelection: (ctx, tags=[]) ->
27+
toggleAnnotationSelection: (tags=[]) ->
2828
annotations = getAnnotationsByTags(tags)
2929
annotationUI.xorSelectedAnnotations(annotations)
30-
setVisibleHighlights: (ctx, state) ->
30+
setVisibleHighlights: (state) ->
3131
annotationUI.visibleHighlights = Boolean(state)
32-
bridge.notify(method: 'setVisibleHighlights', params: state)
32+
bridge.call('setVisibleHighlights', state)
3333

3434
# Because the channel events are all outside of the angular framework we
3535
# need to inform Angular that it needs to re-check it's state and re-draw
@@ -45,8 +45,6 @@ module.exports = class AnnotationUISync
4545
onConnect = (channel, source) ->
4646
# Allow the host to define its own state
4747
unless source is $window.parent
48-
channel.notify
49-
method: 'setVisibleHighlights'
50-
params: annotationUI.visibleHighlights
48+
channel.call('setVisibleHighlights', annotationUI.visibleHighlights)
5149

5250
bridge.onConnect(onConnect)

h/static/scripts/annotator/guest.coffee

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,6 @@ module.exports = class Guest extends Annotator
7272
formatted = {}
7373
for k, v of annotation when k isnt 'anchors'
7474
formatted[k] = v
75-
# Work around issue in jschannel where a repeated object is considered
76-
# recursive, even if it is not its own ancestor.
77-
if formatted.document?.title
78-
formatted.document.title = formatted.document.title.slice()
7975
formatted
8076

8177
this.addPlugin('CrossFrame', cfOptions)
@@ -115,21 +111,20 @@ module.exports = class Guest extends Annotator
115111
crossframe.onConnect(=> this.publish('panelReady'))
116112
crossframe.on('onEditorHide', this.onEditorHide)
117113
crossframe.on('onEditorSubmit', this.onEditorSubmit)
118-
crossframe.on 'focusAnnotations', (ctx, tags=[]) =>
114+
crossframe.on 'focusAnnotations', (tags=[]) =>
119115
for anchor in @anchors when anchor.highlights?
120116
toggle = anchor.annotation.$$tag in tags
121117
$(anchor.highlights).toggleClass('annotator-hl-focused', toggle)
122-
crossframe.on 'scrollToAnnotation', (ctx, tag) =>
118+
crossframe.on 'scrollToAnnotation', (tag) =>
123119
for anchor in @anchors when anchor.highlights?
124120
if anchor.annotation.$$tag is tag
125121
$(anchor.highlights).scrollintoview()
126122
return
127-
crossframe.on 'getDocumentInfo', (trans) =>
128-
trans.delayReturn(true)
123+
crossframe.on 'getDocumentInfo', (cb) =>
129124
this.getDocumentInfo()
130-
.then((info) -> trans.complete(info))
131-
.catch((reason) -> trans.error(reason))
132-
crossframe.on 'setVisibleHighlights', (ctx, state) =>
125+
.then((info) -> cb(null, info))
126+
.catch((reason) -> cb(reason))
127+
crossframe.on 'setVisibleHighlights', (state) =>
133128
this.publish 'setVisibleHighlights', state
134129

135130
_setupWrapper: ->
@@ -315,24 +310,20 @@ module.exports = class Guest extends Annotator
315310
return annotation
316311

317312
showAnnotations: (annotations) =>
318-
@crossframe?.notify
319-
method: "showAnnotations"
320-
params: (a.$$tag for a in annotations)
313+
tags = (a.$$tag for a in annotations)
314+
@crossframe?.call('showAnnotations', tags)
321315

322316
toggleAnnotationSelection: (annotations) =>
323-
@crossframe?.notify
324-
method: "toggleAnnotationSelection"
325-
params: (a.$$tag for a in annotations)
317+
tags = (a.$$tag for a in annotations)
318+
@crossframe?.call('toggleAnnotationSelection', tags)
326319

327320
updateAnnotations: (annotations) =>
328-
@crossframe?.notify
329-
method: "updateAnnotations"
330-
params: (a.$$tag for a in annotations)
321+
tags = (a.$$tag for a in annotations)
322+
@crossframe?.call('updateAnnotations', tags)
331323

332324
focusAnnotations: (annotations) =>
333-
@crossframe?.notify
334-
method: "focusAnnotations"
335-
params: (a.$$tag for a in annotations)
325+
tags = (a.$$tag for a in annotations)
326+
@crossframe?.call('focusAnnotations', tags)
336327

337328
onSuccessfulSelection: (event, immediate) ->
338329
unless event?
@@ -396,11 +387,7 @@ module.exports = class Guest extends Annotator
396387
# Pass true to show the highlights in the frame or false to disable.
397388
setVisibleHighlights: (shouldShowHighlights) ->
398389
return if @visibleHighlights == shouldShowHighlights
399-
400-
@crossframe?.notify
401-
method: 'setVisibleHighlights'
402-
params: shouldShowHighlights
403-
390+
@crossframe?.call('setVisibleHighlights', shouldShowHighlights)
404391
this.toggleHighlightClass(shouldShowHighlights)
405392

406393
toggleHighlightClass: (shouldShowHighlights) ->
@@ -413,11 +400,11 @@ module.exports = class Guest extends Annotator
413400

414401
# Open the sidebar
415402
showFrame: ->
416-
@crossframe?.notify method: 'open'
403+
@crossframe?.call('open')
417404

418405
# Close the sidebar
419406
hideFrame: ->
420-
@crossframe?.notify method: 'back'
407+
@crossframe?.call('back')
421408

422409
onAdderMouseup: (event) ->
423410
event.preventDefault()

h/static/scripts/annotator/plugin/cross-frame.coffee

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extract = extract = (obj, keys...) ->
1212
# as keeping the annotation state in sync with the sidebar application, this
1313
# frame acts as the bridge client, the sidebar is the server. This plugin
1414
# can also be used to send messages through to the sidebar using the
15-
# notify method.
15+
# call method.
1616
module.exports = class CrossFrame extends Annotator.Plugin
1717
constructor: (elem, options) ->
1818
super
@@ -41,8 +41,8 @@ module.exports = class CrossFrame extends Annotator.Plugin
4141
this.on = (event, fn) ->
4242
bridge.on(event, fn)
4343

44-
this.notify = (message) ->
45-
bridge.notify(message)
44+
this.call = (message, args...) ->
45+
bridge.call(message, args...)
4646

4747
this.onConnect = (fn) ->
4848
bridge.onConnect(fn)

h/static/scripts/annotator/plugin/test/cross-frame-test.coffee

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe 'Annotator.Plugin.CrossFrame', ->
2121
fakeBridge =
2222
createChannel: sandbox.stub()
2323
onConnect: sandbox.stub()
24-
notify: sandbox.stub()
24+
call: sandbox.stub()
2525
on: sandbox.stub()
2626

2727
fakeAnnotationSync =
@@ -91,11 +91,11 @@ describe 'Annotator.Plugin.CrossFrame', ->
9191
bridge.on('event', 'arg')
9292
assert.calledWith(fakeBridge.on, 'event', 'arg')
9393

94-
describe '.notify', ->
94+
describe '.call', ->
9595
it 'proxies the call to the bridge', ->
9696
bridge = createCrossFrame()
97-
bridge.notify(method: 'method')
98-
assert.calledWith(fakeBridge.notify, method: 'method')
97+
bridge.call('method', 'arg1', 'arg2')
98+
assert.calledWith(fakeBridge.call, 'method', 'arg1', 'arg2')
9999

100100
describe '.onConnect', ->
101101
it 'proxies the call to the bridge', ->

0 commit comments

Comments
 (0)