Class: Session

Inherits:
Object
  • Object
show all
Defined in:
lib/jiji/util/block_to_session.rb

Overview

ブロックを渡して処理を行なうAPIを セッション風に使えるようにする

Defined Under Namespace

Classes: Request

Instance Method Summary collapse

Constructor Details

#initializeSession

Returns a new instance of Session.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/jiji/util/block_to_session.rb', line 7

def initialize
  @alive = true
  @alive_mutex = Mutex.new
  @q = Queue.new
  @t = Thread.fork {
    yield proc {|*args|
      while( @alive_mutex.synchronize { @alive } )
        req = @q.pop
        req.call( *args ) if req
      end
    }
  }
end

Instance Method Details

#closeObject

セッションを破棄する



28
29
30
31
32
33
34
# File 'lib/jiji/util/block_to_session.rb', line 28

def close
  @alive_mutex.synchronize { 
    @alive = false
  }
  @q.push nil
  @t.join
end

#request(&block) ⇒ Object

リクエストを送る



21
22
23
24
25
26
# File 'lib/jiji/util/block_to_session.rb', line 21

def request( &block )
  return unless block_given?
  req = Request.new(block)
  @q.push req
  req.wait
end