promise 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/future.rb +18 -14
  3. data/lib/promise.rb +22 -4
  4. metadata +4 -4
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
data/lib/future.rb CHANGED
@@ -8,8 +8,7 @@ require 'promise'
8
8
  # # do stuff...
9
9
  # y = x * 2 # => 6. blocks unless 5 seconds has passed.
10
10
  #
11
- class Future < defined?(BasicObject) ? BasicObject : Object
12
- instance_methods.each { |m| undef_method m unless m =~ /__/ } unless defined?(BasicObject)
11
+ class Future < Promise
13
12
 
14
13
  ##
15
14
  # Creates a new future.
@@ -17,8 +16,14 @@ class Future < defined?(BasicObject) ? BasicObject : Object
17
16
  # @yield [] The block to evaluate optimistically.
18
17
  # @see Kernel#future
19
18
  def initialize(&block)
20
- @promise = ::Promise.new(&block)
21
- @thread = ::Thread.new { @promise.__force__ }
19
+ super
20
+ # Ruby won't assign the value of @thread until after the thread is off and
21
+ # running, meaning we can run into an unset @thread when joining off in
22
+ # __force__, instead of it being Thread.current. We could leave it as nil
23
+ # and check for that, but this is a more explicit way to watch for this
24
+ # easy-to-miss gotcha.
25
+ @thread = NOT_SET
26
+ @thread = ::Thread.new { __force__ }
22
27
  end
23
28
 
24
29
  ##
@@ -26,25 +31,24 @@ class Future < defined?(BasicObject) ? BasicObject : Object
26
31
  #
27
32
  # @return [Object]
28
33
  def __force__
29
- @thread.join if @thread
30
- @promise
34
+ @thread.join unless (@thread == ::Thread.current) || @thread.equal?(NOT_SET)
35
+ super
31
36
  end
32
37
  alias_method :force, :__force__
33
38
 
39
+
34
40
  ##
35
- # Does this future support the given method?
41
+ # Returns true if klass.equal?(Future), if klass.equal?(Promise), or the
42
+ # underlying block returns an instance of the given klass
36
43
  #
37
- # @param [Symbol]
38
- # @return [Boolean]
39
- def respond_to?(method)
40
- :force.equal?(method) || :__force__.equal?(method) || __force__.respond_to?(method)
44
+ # @param [Class]
45
+ # @return [true, false]
46
+ def is_a?(klass)
47
+ klass.equal?(::Future) || super
41
48
  end
42
49
 
43
50
  private
44
51
 
45
- def method_missing(method, *args, &block)
46
- __force__.__send__(method, *args, &block)
47
- end
48
52
  end
49
53
 
50
54
  module Kernel
data/lib/promise.rb CHANGED
@@ -43,14 +43,14 @@ class Promise < defined?(BasicObject) ? BasicObject : ::Object
43
43
  # @return [Object]
44
44
  def __force__
45
45
  @mutex.synchronize do
46
- if @result.equal?(NOT_SET) && @error.equal?(NOT_SET)
46
+ if !__forced?
47
47
  begin
48
48
  @result = @block.call
49
49
  rescue ::Exception => e
50
50
  @error = e
51
51
  end
52
52
  end
53
- end if @result.equal?(NOT_SET) && @error.equal?(NOT_SET)
53
+ end unless __forced?
54
54
  # BasicObject won't send raise to Kernel
55
55
  @error.equal?(NOT_SET) ? @result : ::Kernel.raise(@error)
56
56
  end
@@ -60,9 +60,27 @@ class Promise < defined?(BasicObject) ? BasicObject : ::Object
60
60
  # Does this promise support the given method?
61
61
  #
62
62
  # @param [Symbol]
63
- # @return [Boolean]
63
+ # @return [true, false]
64
64
  def respond_to?(method)
65
- :force.equal?(method) || :__force__.equal?(method) || __force__.respond_to?(method)
65
+ :__forced?.equal?(method) || :force.equal?(method) || :__force__.equal?(method) || __force__.respond_to?(method)
66
+ end
67
+
68
+ ##
69
+ # Returns true if klass.equal?(Promise) or the underlying block returns an
70
+ # instance of the given klass
71
+ #
72
+ # @param [Class]
73
+ # @return [true, false]
74
+ def is_a?(klass)
75
+ klass.equal?(::Promise) || __force__.is_a?(klass)
76
+ end
77
+
78
+ ##
79
+ # Returns true if this promise has finished executing
80
+ #
81
+ # @return [true, false]
82
+ def __forced?
83
+ !(@result.equal?(NOT_SET) && @error.equal?(NOT_SET))
66
84
  end
67
85
 
68
86
  private
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
- - 1
9
- version: 0.2.1
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ben Lavender
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-06-29 00:00:00 -05:00
17
+ date: 2010-07-05 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency