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
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,27 +32,21 @@ class AppController
plugins.Store?.unregisterAnnotation(annotation)
annotator.deleteAnnotation(annotation)
streamer.onmessage = (msg) ->
data = JSON.parse(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]
action = data.options.action
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(msg)
self.onmessage(JSON.parse(msg.data))
###*
# @ngdoc method
......
......@@ -106,3 +106,15 @@ describe 'streamer', ->
clock.tick(500)
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