Skip to content

Commit

Permalink
Change Guard::Interactor#lock and #unlock methods so they will lock i…
Browse files Browse the repository at this point in the history
…nteractor

in the right thread and free $stdin [closes guard#137].
  • Loading branch information
hron committed Sep 20, 2011
1 parent 443f57e commit 8c6a307
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions lib/guard/interactor.rb
@@ -1,6 +1,9 @@
module Guard
class Interactor

class LockException < Exception; end
class UnlockException < Exception; end

attr_reader :locked

def initialize
Expand All @@ -11,35 +14,43 @@ def start
return if ENV["GUARD_ENV"] == 'test'
@thread = Thread.new do
loop do
if (entry = $stdin.gets) && !@locked
entry.gsub! /\n/, ''
case entry
when 'stop', 'quit', 'exit', 's', 'q', 'e'
::Guard.stop
when 'reload', 'r', 'z'
::Guard.reload
when 'pause', 'p'
::Guard.pause
else
::Guard.run_all
begin
if !@locked && (entry = $stdin.gets)
entry.gsub! /\n/, ''
case entry
when 'stop', 'quit', 'exit', 's', 'q', 'e'
::Guard.stop
when 'reload', 'r', 'z'
::Guard.reload
when 'pause', 'p'
::Guard.pause
else
::Guard.run_all
end
end
rescue LockException
lock
rescue UnlockException
unlock
end
end
end
end

def stop
@thread.kill
end

def lock
@locked = true
stop
if @thread == Thread.current
@locked = true
else
@thread.raise(LockException)
end
end

def unlock
@locked = false
start
if @thread == Thread.current
@locked = false
else
@thread.raise(UnlockException)
end
end

end
Expand Down

0 comments on commit 8c6a307

Please sign in to comment.