Commit 8e097758 authored by Aron Carroll's avatar Aron Carroll

Clean up the streamer.onmessage handler

- Fix "single player mode" filtering of highlights. It was assuming
  that $scope.persona was an object rather than a string.
- Remove multiple checks to ensure that payload is an array, the
  backend only sends arrays[1][2].
- Convert python style method docstrings to coffeescript comments.

[1]: https://github.com/hypothesis/h/blob/master/h/streamer.py#L562
[2]: https://github.com/hypothesis/h/blob/master/h/streamer.py#L488
parent 026e550b
...@@ -17,7 +17,7 @@ class AppController ...@@ -17,7 +17,7 @@ class AppController
isFirstRun = $location.search().hasOwnProperty('firstrun') isFirstRun = $location.search().hasOwnProperty('firstrun')
applyUpdates = (action, data) -> applyUpdates = (action, data) ->
"""Update the application with new data from the websocket.""" # Update the application with new data from the websocket.
return unless data?.length return unless data?.length
if action == 'past' if action == 'past'
action = 'create' action = 'create'
...@@ -32,28 +32,21 @@ class AppController ...@@ -32,28 +32,21 @@ class AppController
plugins.Store?.unregisterAnnotation(annotation) plugins.Store?.unregisterAnnotation(annotation)
annotator.deleteAnnotation(annotation) annotator.deleteAnnotation(annotation)
streamer.onmessage = (msg) -> streamer.onmessage = (data) ->
data = msg.data if !data or data.type != 'annotation-notification'
unless data.type? and data.type is 'annotation-notification'
return return
payload = data.payload
action = data.options.action action = data.options.action
payload = data.payload
unless payload instanceof Array then payload = [payload]
p = auth.user
user = if p? then "acct:" + p.username + "@" + p.provider else ''
unless payload instanceof Array then payload = [payload]
if $scope.socialView.name is 'single-player' if $scope.socialView.name is 'single-player'
payload = payload.filter (d) -> d.user is user payload = payload.filter (ann) -> ann.user is auth.user
applyUpdates action, payload applyUpdates(action, payload)
$scope.$digest() $scope.$digest()
initStore = -> initStore = ->
"""Initialize the storage component.""" # Initialize the storage component.
Store = plugins.Store Store = plugins.Store
delete plugins.Store delete plugins.Store
......
...@@ -59,7 +59,7 @@ class Streamer ...@@ -59,7 +59,7 @@ class Streamer
setTimeout((-> self.open()), backoff(self._failCount, 10)) setTimeout((-> self.open()), backoff(self._failCount, 10))
this._sock.onmessage = (msg) -> this._sock.onmessage = (msg) ->
self.onmessage(JSON.parse(msg)) self.onmessage(JSON.parse(msg.data))
###* ###*
# @ngdoc method # @ngdoc method
......
...@@ -107,14 +107,14 @@ describe 'streamer', -> ...@@ -107,14 +107,14 @@ describe 'streamer', ->
clock.tick(500) clock.tick(500)
assert.calledOnce(WebSocket) assert.calledOnce(WebSocket)
it 'calls the onmessage handler when the socket recieves a message', -> it 'calls the onmessage handler when the socket receives a message', ->
streamer.onmessage = sinon.spy() streamer.onmessage = sinon.spy()
streamer.open() streamer.open()
fakeSock.onmessage(JSON.stringify({animal: 'baboon'})) fakeSock.onmessage(data: JSON.stringify({animal: 'baboon'}))
assert.called(streamer.onmessage) assert.called(streamer.onmessage)
it 'calls the onmessage handler with parsed JSON', -> it 'calls the onmessage handler with parsed JSON', ->
streamer.onmessage = sinon.spy() streamer.onmessage = sinon.spy()
streamer.open() streamer.open()
fakeSock.onmessage(JSON.stringify({animal: 'baboon'})) fakeSock.onmessage(data: JSON.stringify({animal: 'baboon'}))
assert.calledWith(streamer.onmessage, {animal: 'baboon'}) assert.calledWith(streamer.onmessage, {animal: 'baboon'})
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