Skip to content

Commit 1739bf0

Browse files
committed
Merge branch 'pr-173'
This closes #173
2 parents bdc2305 + e3c4e7e commit 1739bf0

File tree

4 files changed

+82
-45
lines changed

4 files changed

+82
-45
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- [179](https://github.com/Smashing/smashing/pull/179) Replace Travis CI by GH actions
66
- [178](https://github.com/Smashing/smashing/pull/178) Relax dependencies; pinned minor, free patch version (thanks @dylanratcliffe)
7+
- [173](https://github.com/Smashing/smashing/pull/173) Send only data that is needed on the dashboard (thanks @toy)
78

89
## 2021-01-30 - 1.3.1
910

javascripts/dashing.coffee

+35-30
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ class Dashing.Widget extends Batman.View
5353
super
5454

5555
@mixin($(@node).data())
56-
Dashing.widgets[@id] ||= []
57-
Dashing.widgets[@id].push(@)
56+
if @id # skip widgets without id
57+
Dashing.widgets[@id] ||= []
58+
Dashing.widgets[@id].push(@)
5859

5960
type = Batman.Filters.dashize(@view)
6061
$(@node).addClass("widget widget-#{type} #{@id}")
@@ -121,34 +122,38 @@ Dashing.widgets = widgets = {}
121122
Dashing.lastEvents = lastEvents = {}
122123
Dashing.debugMode = false
123124

124-
source = new EventSource('events')
125-
source.addEventListener 'open', (e) ->
126-
console.log("Connection opened", e)
127-
128-
source.addEventListener 'error', (e)->
129-
console.log("Connection error", e)
130-
if (e.currentTarget.readyState == EventSource.CLOSED)
131-
console.log("Connection closed")
132-
setTimeout (->
133-
window.location.reload()
134-
), 5*60*1000
135-
136-
source.addEventListener 'message', (e) ->
137-
data = JSON.parse(e.data)
138-
if lastEvents[data.id]?.updatedAt != data.updatedAt
139-
if Dashing.debugMode
140-
console.log("Received data for #{data.id}", data)
141-
lastEvents[data.id] = data
142-
if widgets[data.id]?.length > 0
143-
for widget in widgets[data.id]
144-
widget.receiveData(data)
145-
146-
source.addEventListener 'dashboards', (e) ->
147-
data = JSON.parse(e.data)
148-
if Dashing.debugMode
149-
console.log("Received data for dashboards", data)
150-
if data.dashboard is '*' or window.location.pathname is "/#{data.dashboard}"
151-
Dashing.fire data.event, data
125+
Dashing.on 'run', ->
126+
setTimeout -> # run only when all widgets are created
127+
ids = Object.keys(Dashing.widgets)
128+
source = new EventSource('events?ids=' + encodeURIComponent(ids.join(',')))
129+
source.addEventListener 'open', (e) ->
130+
console.log("Connection opened", e)
131+
132+
source.addEventListener 'error', (e)->
133+
console.log("Connection error", e)
134+
if (e.currentTarget.readyState == EventSource.CLOSED)
135+
console.log("Connection closed")
136+
setTimeout (->
137+
window.location.reload()
138+
), 5*60*1000
139+
140+
source.addEventListener 'message', (e) ->
141+
data = JSON.parse(e.data)
142+
if lastEvents[data.id]?.updatedAt != data.updatedAt
143+
if Dashing.debugMode
144+
console.log("Received data for #{data.id}", data)
145+
lastEvents[data.id] = data
146+
if widgets[data.id]?.length > 0
147+
for widget in widgets[data.id]
148+
widget.receiveData(data)
149+
150+
source.addEventListener 'dashboards', (e) ->
151+
data = JSON.parse(e.data)
152+
if Dashing.debugMode
153+
console.log("Received data for dashboards", data)
154+
if data.dashboard is '*' or window.location.pathname is "/#{data.dashboard}"
155+
Dashing.fire data.event, data
156+
, 0
152157

153158
$(document).ready ->
154159
Dashing.run()

lib/dashing/app.rb

+11-14
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def authenticated?(token)
3636
set :assets_prefix, '/assets'
3737
set :digest_assets, false
3838
set :server, 'thin'
39-
set :connections, []
39+
set :connections, {}
4040
set :history_file, 'history.yml'
4141
set :public_folder, File.join(settings.root, 'public')
4242
set :views, File.join(settings.root, 'dashboards')
@@ -54,7 +54,7 @@ def authenticated?(token)
5454
settings.sprockets.append_path("assets/#{path}")
5555
end
5656

57-
['widgets', File.expand_path('../../../javascripts', __FILE__)]. each do |path|
57+
['widgets', File.expand_path('../../../javascripts', __FILE__)].each do |path|
5858
settings.sprockets.append_path(path)
5959
end
6060

@@ -77,9 +77,12 @@ def authenticated?(token)
7777
get '/events', :provides => 'text/event-stream' do
7878
protected!
7979
response.headers['X-Accel-Buffering'] = 'no' # Disable buffering for nginx
80+
ids = params[:ids].to_s.split(',').to_set
8081
stream :keep_open do |out|
81-
settings.connections << out
82-
out << latest_events
82+
settings.connections[out] = ids
83+
settings.history.each do |id, event|
84+
out << event if ids.include?(id)
85+
end
8386
out.callback { settings.connections.delete(out) }
8487
end
8588
end
@@ -131,7 +134,7 @@ def authenticated?(token)
131134

132135
Thin::Server.class_eval do
133136
def stop_with_connection_closing
134-
Sinatra::Application.settings.connections.dup.each(&:close)
137+
Sinatra::Application.settings.connections.dup.each_key(&:close)
135138
stop_without_connection_closing
136139
end
137140

@@ -141,12 +144,12 @@ def stop_with_connection_closing
141144

142145
def send_event(id, body, target=nil)
143146
body[:id] = id
144-
body[:updatedAt] ||= (Time.now.to_f * 1000.0).to_i
147+
body[:updatedAt] ||= (Time.now.to_f * 1000.0).to_i
145148
event = format_event(body.to_json, target)
146149
Sinatra::Application.settings.history[id] = event unless target == 'dashboards'
147-
Sinatra::Application.settings.connections.each { |out|
150+
Sinatra::Application.settings.connections.each { |out, ids|
148151
begin
149-
out << event
152+
out << event if target == 'dashboards' || ids.include?(id)
150153
rescue IOError => e # if the socket is closed an IOError is thrown
151154
Sinatra::Application.settings.connections.delete(out)
152155
end
@@ -159,12 +162,6 @@ def format_event(body, name=nil)
159162
str << "data: #{body}\n\n"
160163
end
161164

162-
def latest_events
163-
settings.history.inject("") do |str, (_id, body)|
164-
str << body
165-
end
166-
end
167-
168165
def first_dashboard
169166
files = Dir[File.join(settings.views, '*')].collect { |f| File.basename(f, '.*') }
170167
files -= ['layout']

test/app_test.rb

+35-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
class AppTest < Dashing::Test
55
def setup
66
@connection = []
7-
app.settings.connections = [@connection]
7+
# stop sinatra from handling Hash value specially which makes it merge new value into previous one
8+
app.settings.connections = nil
9+
app.settings.connections = {@connection => ['some_widget']}
810
app.settings.auth_token = nil
911
app.settings.default_dashboard = nil
1012
app.settings.history_file = File.join(Dir.tmpdir, 'history.yml')
@@ -77,6 +79,38 @@ def test_get_events
7779
assert_equal 8, parse_data(@connection[0])['value']
7880
end
7981

82+
# rubocop:disable Metrics/AbcSize
83+
# rubocop:disable Metrics/MethodLength
84+
def test_get_events_by_id
85+
with_generated_project do
86+
post '/widgets/synergy', JSON.generate({ value: 3 })
87+
assert_equal 204, last_response.status
88+
89+
post '/widgets/valuation', JSON.generate({ value: 'a' })
90+
assert_equal 204, last_response.status
91+
92+
get '/events?ids=synergy%2Cvaluation'
93+
assert_equal 200, last_response.status
94+
95+
events = last_response.body.split(/\n+/) # two events, printed with blank lines separating them
96+
synergy_event = parse_data(events[0])
97+
valuation_event = parse_data(events[1])
98+
assert_equal ['synergy', 3], [synergy_event['id'], synergy_event['value']]
99+
assert_equal %w[valuation a], [valuation_event['id'], valuation_event['value']]
100+
end
101+
end
102+
103+
def test_get_events_by_unknown_id
104+
with_generated_project do
105+
post '/widgets/synergy', JSON.generate({ value: 3 })
106+
assert_equal 204, last_response.status
107+
108+
get '/events?ids=unknown'
109+
assert_equal 200, last_response.status
110+
assert_equal '', last_response.body
111+
end
112+
end
113+
80114
def test_dashboard_events
81115
post '/dashboards/my_super_sweet_dashboard', JSON.generate({event: 'reload'})
82116
assert_equal 204, last_response.status

0 commit comments

Comments
 (0)