Change Guard::Interactor#lock and #unlock methods so they will lock interactor

in the right thread and free $stdin [closes #137].
This commit is contained in:
Aleksei Gusev 2011-09-20 21:54:21 +03:00
parent 443f57efce
commit 8c6a30795a

View File

@ -1,6 +1,9 @@
module Guard module Guard
class Interactor class Interactor
class LockException < Exception; end
class UnlockException < Exception; end
attr_reader :locked attr_reader :locked
def initialize def initialize
@ -11,35 +14,43 @@ module Guard
return if ENV["GUARD_ENV"] == 'test' return if ENV["GUARD_ENV"] == 'test'
@thread = Thread.new do @thread = Thread.new do
loop do loop do
if (entry = $stdin.gets) && !@locked begin
entry.gsub! /\n/, '' if !@locked && (entry = $stdin.gets)
case entry entry.gsub! /\n/, ''
when 'stop', 'quit', 'exit', 's', 'q', 'e' case entry
::Guard.stop when 'stop', 'quit', 'exit', 's', 'q', 'e'
when 'reload', 'r', 'z' ::Guard.stop
::Guard.reload when 'reload', 'r', 'z'
when 'pause', 'p' ::Guard.reload
::Guard.pause when 'pause', 'p'
else ::Guard.pause
::Guard.run_all else
::Guard.run_all
end
end end
rescue LockException
lock
rescue UnlockException
unlock
end end
end end
end end
end end
def stop
@thread.kill
end
def lock def lock
@locked = true if @thread == Thread.current
stop @locked = true
else
@thread.raise(LockException)
end
end end
def unlock def unlock
@locked = false if @thread == Thread.current
start @locked = false
else
@thread.raise(UnlockException)
end
end end
end end