Commit 780ab36f authored by Nick Stenning's avatar Nick Stenning

Merge pull request #1804 from hypothesis/parse-streamer-json

Clean up Streamer.onmessage
parents 18569a70 8e097758
...@@ -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,27 +32,21 @@ class AppController ...@@ -32,27 +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 = JSON.parse(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
unless payload instanceof Array then payload = [payload]
p = auth.user action = data.options.action
user = if p? then "acct:" + p.username + "@" + p.provider else '' payload = data.payload
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(msg) self.onmessage(JSON.parse(msg.data))
###* ###*
# @ngdoc method # @ngdoc method
......
...@@ -106,3 +106,15 @@ describe 'streamer', -> ...@@ -106,3 +106,15 @@ describe 'streamer', ->
clock.tick(500) clock.tick(500)
assert.calledOnce(WebSocket) assert.calledOnce(WebSocket)
it 'calls the onmessage handler when the socket receives a message', ->
streamer.onmessage = sinon.spy()
streamer.open()
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(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