Class: Redis::Synchrony

Inherits:
Object
  • Object
show all
Defined in:
lib/redis/synchrony.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis) ⇒ Synchrony

Returns a new instance of Synchrony.



28
29
30
# File 'lib/redis/synchrony.rb', line 28

def initialize redis
  @redis = redis
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/redis/synchrony.rb', line 31

def method_missing method, *args, &block
  raise 'synchrony not allowed in multi' if @redis.in_multi? and method != :exec
  result = @redis.send method, *args, &block
  if result.respond_to? :callback and result.respond_to? :errback
    result = Synchrony.sync result
    raise result if Exception === result
  end
  result
end

Class Method Details

.sync(df) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/redis/synchrony.rb', line 15

def self.sync(df)
  f = Fiber.current
  xback = proc {|r|
    if f == Fiber.current
      return r
    else
      f.resume r
    end
  }
  df.callback &xback
  df.errback &xback
  Fiber.yield
end