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
isFirstRun = $location.search().hasOwnProperty('firstrun')
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
if action == 'past'
action = 'create'
......@@ -32,28 +32,21 @@ class AppController
plugins.Store?.unregisterAnnotation(annotation)
annotator.deleteAnnotation(annotation)
streamer.onmessage = (msg) ->
data = msg.data
unless data.type? and data.type is 'annotation-notification'
streamer.onmessage = (data) ->
if !data or data.type != 'annotation-notification'
return
payload = data.payload
action = data.options.action
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]
payload = data.payload
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()
initStore = ->
"""Initialize the storage component."""
# Initialize the storage component.
Store = plugins.Store
delete plugins.Store
......
......@@ -59,7 +59,7 @@ class Streamer
setTimeout((-> self.open()), backoff(self._failCount, 10))
this._sock.onmessage = (msg) ->
self.onmessage(JSON.parse(msg))
self.onmessage(JSON.parse(msg.data))
###*
# @ngdoc method
......
......@@ -107,14 +107,14 @@ describe 'streamer', ->
clock.tick(500)
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.open()
fakeSock.onmessage(JSON.stringify({animal: 'baboon'}))
fakeSock.onmessage(data: JSON.stringify({animal: 'baboon'}))
assert.called(streamer.onmessage)
it 'calls the onmessage handler with parsed JSON', ->
streamer.onmessage = sinon.spy()
streamer.open()
fakeSock.onmessage(JSON.stringify({animal: 'baboon'}))
fakeSock.onmessage(data: JSON.stringify({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