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
class Interactor
class LockException < Exception; end
class UnlockException < Exception; end
attr_reader :locked
def initialize
@ -11,7 +14,8 @@ module Guard
return if ENV["GUARD_ENV"] == 'test'
@thread = Thread.new do
loop do
if (entry = $stdin.gets) && !@locked
begin
if !@locked && (entry = $stdin.gets)
entry.gsub! /\n/, ''
case entry
when 'stop', 'quit', 'exit', 's', 'q', 'e'
@ -24,22 +28,29 @@ module Guard
::Guard.run_all
end
end
rescue LockException
lock
rescue UnlockException
unlock
end
end
end
def stop
@thread.kill
end
def lock
if @thread == Thread.current
@locked = true
stop
else
@thread.raise(LockException)
end
end
def unlock
if @thread == Thread.current
@locked = false
start
else
@thread.raise(UnlockException)
end
end
end