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,7 +14,8 @@ 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
if !@locked && (entry = $stdin.gets)
entry.gsub! /\n/, '' entry.gsub! /\n/, ''
case entry case entry
when 'stop', 'quit', 'exit', 's', 'q', 'e' when 'stop', 'quit', 'exit', 's', 'q', 'e'
@ -24,22 +28,29 @@ module Guard
::Guard.run_all ::Guard.run_all
end end
end end
rescue LockException
lock
rescue UnlockException
unlock
end end
end end
end end
def stop
@thread.kill
end end
def lock def lock
if @thread == Thread.current
@locked = true @locked = true
stop else
@thread.raise(LockException)
end
end end
def unlock def unlock
if @thread == Thread.current
@locked = false @locked = false
start else
@thread.raise(UnlockException)
end
end end
end end