From e75912279c4fb275dae1c979f4ecdb02b3f97349 Mon Sep 17 00:00:00 2001 From: Joshua Muheim Date: Thu, 20 Mar 2014 11:33:53 +0100 Subject: [PATCH] Display "Browser connected." only once. Never display "Browser disconnected." --- lib/guard/livereload/reactor.rb | 14 ++++++++------ spec/lib/guard/livereload/reactor_spec.rb | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/guard/livereload/reactor.rb b/lib/guard/livereload/reactor.rb index 33daee7..dba9d62 100644 --- a/lib/guard/livereload/reactor.rb +++ b/lib/guard/livereload/reactor.rb @@ -3,12 +3,13 @@ module Guard class LiveReload class Reactor - attr_reader :web_sockets, :thread, :options + attr_reader :web_sockets, :thread, :options, :connections_count def initialize(options) - @web_sockets = [] - @options = options - @thread = Thread.new { _start_reactor } + @web_sockets = [] + @options = options + @thread = Thread.new { _start_reactor } + @connections_count = 0 end def stop @@ -51,7 +52,9 @@ def _start_reactor end def _connect(ws) - UI.info "Browser connected." + @connections_count += 1 + UI.info "Browser connected." if connections_count == 1 + ws.send MultiJson.encode( command: 'hello', protocols: ['http://livereload.com/protocols/official-7'], @@ -64,7 +67,6 @@ def _connect(ws) end def _disconnect(ws) - UI.info "Browser disconnected." @web_sockets.delete(ws) end diff --git a/spec/lib/guard/livereload/reactor_spec.rb b/spec/lib/guard/livereload/reactor_spec.rb index 2695575..6acab9e 100644 --- a/spec/lib/guard/livereload/reactor_spec.rb +++ b/spec/lib/guard/livereload/reactor_spec.rb @@ -31,6 +31,23 @@ end end + describe "#_connect(ws)" do + let(:ws) { double.as_null_object } + let(:reactor) { new_live_reactor } + + it "displays a message once" do + expect(Guard::UI).to receive(:info).with("Browser connected.").once + reactor.send(:_connect, ws) + reactor.send(:_connect, ws) + end + + it "increments the connection count" do + expect { + reactor.send(:_connect, ws) + }.to change { reactor.connections_count }.from(0).to 1 + end + end + end def new_live_reactor(options = {})