Class: Tribe::Future

Inherits:
Object
  • Object
show all
Defined in:
lib/tribe/future.rb

Instance Method Summary collapse

Constructor Details

#initialize(actor = nil) ⇒ Future

Returns a new instance of Future.



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/tribe/future.rb', line 3

def initialize(actor = nil)
  @state = :initialized
  @lock = Mutex.new
  @condition = ConditionVariable.new
  @result = nil
  @success_callback = nil
  @failure_callback = nil
  @actor = actor

  return nil
end

Instance Method Details

#failure(&block) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/tribe/future.rb', line 116

def failure(&block)
  @lock.synchronize do
    case @state
    when :initialized
      @failure_callback = block
    when :finished
      if @result.is_a?(Exception)
        if @actor
          @actor.perform! do
            block.call(@result)
          end
        else
          block.call(@result)
        end
      end
    else
      raise Tribe::FutureError.new('Invalid state.')
    end

    return nil
  end
end

#failure?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/tribe/future.rb', line 89

def failure?
  return !success?
end

#finished?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
# File 'lib/tribe/future.rb', line 15

def finished?
  @lock.synchronize do
    return @state == :finished
  end
end

#resultObject



63
64
65
66
67
68
69
# File 'lib/tribe/future.rb', line 63

def result
  @lock.synchronize do
    raise Tribe::FutureNoResult.new('Result must be set first.') unless @state == :finished

    return @result
  end
end

#result=(val) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tribe/future.rb', line 27

def result=(val)
  @lock.synchronize do
    return unless @state == :initialized

    @timer.cancel if @timer

    @result = val
    @state = :finished
    @condition.signal

    if val.is_a?(Exception)
      if @failure_callback
        if @actor
          @actor.perform! do
            @failure_callback.call(val)
          end
        else
          @failure_callback.call(val)
        end
      end
    else
      if @success_callback
        if @actor
          @actor.perform! do
            @success_callback.call(val)
          end
        else
          @success_callback.call(val)
        end
      end
    end

    return nil
  end
end

#success(&block) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/tribe/future.rb', line 93

def success(&block)
  @lock.synchronize do
    case @state
    when :initialized
      @success_callback = block
    when :finished
      unless @result.is_a?(Exception)
        if @actor
          @actor.perform! do
            block.call(@result)
          end
        else
          block.call(@result)
        end
      end
    else
      raise Tribe::FutureError.new('Invalid state.')
    end

    return nil
  end
end

#success?Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
87
# File 'lib/tribe/future.rb', line 81

def success?
  @lock.synchronize do
    raise Tribe::FutureNoResult.new('Result must be set first.') unless @state == :finished

    return !@result.is_a?(Exception)
  end
end

#timeoutObject



139
140
141
142
143
# File 'lib/tribe/future.rb', line 139

def timeout
  @lock.synchronize do
    @timeout
  end
end

#timeout=(val) ⇒ Object

Raises:



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/tribe/future.rb', line 145

def timeout=(val)
  raise Tribe::FutureError.new('Timeout may only be set once.') if @timeout

  @timeout = val

  @timer = Workers::Timer.new(val) do
    begin
      raise Tribe::FutureTimeout.new("Timeout after #{@timeout} seconds.")
    rescue Tribe::FutureTimeout => e
      self.result = e
    end
  end
end

#timeout?Boolean

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/tribe/future.rb', line 21

def timeout?
  @lock.synchronnize do
    return @state == :finished && @result.is_a?(Tribe::FutureTimeout)
  end
end

#waitObject



71
72
73
74
75
76
77
78
79
# File 'lib/tribe/future.rb', line 71

def wait
  @lock.synchronize do
    return if @state == :finished

    @condition.wait(@lock)

    return nil
  end
end