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

Commit 5f8cbcb

Browse files
committed
dashboard events - use SSE event names
1 parent 3f58bbd commit 5f8cbcb

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

javascripts/dashing.coffee

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,18 @@ source.addEventListener 'error', (e)->
106106
source.addEventListener 'message', (e) ->
107107
data = JSON.parse(e.data)
108108
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-
113109
if Dashing.debugMode
114110
console.log("Received data for #{data.id}", data)
115111
lastEvents[data.id] = data
116112
if widgets[data.id]?.length > 0
117113
for widget in widgets[data.id]
118114
widget.receiveData(data)
119115

116+
source.addEventListener 'dashboards', (e) ->
117+
data = JSON.parse(e.data)
118+
if Dashing.debugMode
119+
console.log("Received data for dashboards", data)
120+
Dashing.fire data.event, data
120121

121122
$(document).ready ->
122123
Dashing.run()

lib/dashing.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,13 @@ def protected!
8181
end
8282
end
8383

84-
post '/reload' do
84+
post '/dashboards/:id' do
8585
request.body.rewind
8686
body = JSON.parse(request.body.read)
87+
body['dashboard'] ||= params['id']
8788
auth_token = body.delete("auth_token")
8889
if !settings.auth_token || settings.auth_token == auth_token
89-
send_event('/reload', body)
90+
send_event(params['id'], body, 'dashboards')
9091
204 # response without entity body
9192
else
9293
status 401
@@ -119,16 +120,18 @@ def production?
119120
ENV['RACK_ENV'] == 'production'
120121
end
121122

122-
def send_event(id, body)
123+
def send_event(id, body, target=nil)
123124
body[:id] = id
124125
body[:updatedAt] ||= Time.now.to_i
125-
event = format_event(body.to_json)
126-
Sinatra::Application.settings.history[id] = event unless id =~ /^\//
126+
event = format_event(body.to_json, target)
127+
Sinatra::Application.settings.history[id] = event unless target == 'dashboards'
127128
Sinatra::Application.settings.connections.each { |out| out << event }
128129
end
129130

130-
def format_event(body)
131-
"data: #{body}\n\n"
131+
def format_event(body, name=nil)
132+
str = ""
133+
str << "event: #{name}\n" if name
134+
str << "data: #{body}\n\n"
132135
end
133136

134137
def latest_events

test/app_test.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ def test_get_events
4141
assert_equal 8, parse_data(@connection[0])['value']
4242
end
4343

44+
def test_dashboard_events
45+
post '/dashboards/my_super_sweet_dashboard', JSON.generate({event: 'reload'})
46+
assert_equal 204, last_response.status
47+
48+
get '/events'
49+
assert_equal 200, last_response.status
50+
assert_equal 'dashboards', parse_event(@connection[0])
51+
assert_equal 'reload', parse_data(@connection[0])['event']
52+
end
53+
4454
def test_redirect_to_default_dashboard
4555
with_generated_project do
4656
Sinatra::Application.settings.default_dashboard = 'test1'
@@ -141,4 +151,8 @@ def app
141151
def parse_data(string)
142152
JSON.parse string[/data: (.+)/, 1]
143153
end
144-
end
154+
155+
def parse_event(string)
156+
string[/event: (.+)/, 1]
157+
end
158+
end

0 commit comments

Comments
 (0)