Skip to content
This repository was archived by the owner on Mar 7, 2018. It is now read-only.

Commit 3f58bbd

Browse files
pvandecjolly
authored andcommitted
Adding application messaging.
While widgets are the most common target for events, there is good reason to sometimes target the application as a whole. This patch adds support for such events using the /-prefixed notation. (This convention is safe for use since it represents an extremely uncommon widget ID, and one which data could not be POSTed to.) Such events are NOT subject to replay, and are handled on the client-side by firing corresponding events on the Dashing application class. As a proof of concept (and to fufill a feature request), this patch also introduces a POST /reload endpoint, which when provided with a valid authkey (and optionally a dashboard name) fires the appropriate 'reload' event on all of the loaded dashboards. The 'reload' event handler then reloads the dashboard, unless a different dashboard was specified.
1 parent 326c5b9 commit 3f58bbd

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

javascripts/dashing.coffee

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ Batman.Filters.shortenedNumber = (num) ->
2525
num
2626

2727
class window.Dashing extends Batman.App
28+
@on 'reload', (data) ->
29+
if data.dashboard?
30+
location.reload() if window.location.pathname is "/#{data.dashboard}"
31+
else
32+
location.reload()
33+
2834
@root ->
2935
Dashing.params = Batman.URI.paramsFromQuery(window.location.search.slice(1));
3036

@@ -83,7 +89,6 @@ Dashing.AnimatedValue =
8389
@[k] = num
8490
@set k, to
8591
, 10
86-
@[k] = num
8792

8893
Dashing.widgets = widgets = {}
8994
Dashing.lastEvents = lastEvents = {}
@@ -98,9 +103,13 @@ source.addEventListener 'error', (e)->
98103
if (e.readyState == EventSource.CLOSED)
99104
console.log("Connection closed")
100105

101-
source.addEventListener 'message', (e) =>
106+
source.addEventListener 'message', (e) ->
102107
data = JSON.parse(e.data)
103108
if lastEvents[data.id]?.updatedAt != data.updatedAt
109+
# /messages are internal messages, and cannot correspond to widgets.
110+
# We will handle them as events on the Dashing application.
111+
return Dashing.fire(data.id.slice(1), data) if data.id[0] is '/'
112+
104113
if Dashing.debugMode
105114
console.log("Received data for #{data.id}", data)
106115
lastEvents[data.id] = data

lib/dashing.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,22 @@ def protected!
8181
end
8282
end
8383

84+
post '/reload' do
85+
request.body.rewind
86+
body = JSON.parse(request.body.read)
87+
auth_token = body.delete("auth_token")
88+
if !settings.auth_token || settings.auth_token == auth_token
89+
send_event('/reload', body)
90+
204 # response without entity body
91+
else
92+
status 401
93+
"Invalid API key\n"
94+
end
95+
end
96+
8497
post '/widgets/:id' do
8598
request.body.rewind
86-
body = JSON.parse(request.body.read)
99+
body = JSON.parse(request.body.read)
87100
auth_token = body.delete("auth_token")
88101
if !settings.auth_token || settings.auth_token == auth_token
89102
send_event(params['id'], body)
@@ -110,7 +123,7 @@ def send_event(id, body)
110123
body[:id] = id
111124
body[:updatedAt] ||= Time.now.to_i
112125
event = format_event(body.to_json)
113-
Sinatra::Application.settings.history[id] = event
126+
Sinatra::Application.settings.history[id] = event unless id =~ /^\//
114127
Sinatra::Application.settings.connections.each { |out| out << event }
115128
end
116129

0 commit comments

Comments
 (0)