File tree Expand file tree Collapse file tree 2 files changed +41
-10
lines changed Expand file tree Collapse file tree 2 files changed +41
-10
lines changed Original file line number Diff line number Diff line change 1
1
require 'thread'
2
+ require 'concurrent/obligation'
2
3
3
4
module Concurrent
4
5
@@ -48,7 +49,7 @@ def initialize(opts = {}, &block)
48
49
49
50
init_obligation
50
51
@state = :pending
51
- @task = block
52
+ @task = block
52
53
set_deref_options ( opts )
53
54
end
54
55
@@ -78,18 +79,34 @@ def value
78
79
mutex . unlock
79
80
end
80
81
82
+ # reconfigures the block returning the value if still #incomplete?
83
+ # @yield the delayed operation to perform
84
+ # @returns [true, false] if success
85
+ def reconfigure ( &block )
86
+ mutex . lock
87
+ raise ArgumentError . new ( 'no block given' ) unless block_given?
88
+ if @state == :pending
89
+ @task = block
90
+ true
91
+ else
92
+ false
93
+ end
94
+ ensure
95
+ mutex . unlock
96
+ end
97
+
81
98
private
82
99
83
- def execute_task_once
84
- if @state == :pending
85
- begin
86
- @value = @task . call
87
- @state = :fulfilled
88
- rescue => ex
89
- @reason = ex
90
- @state = :rejected
91
- end
100
+ def execute_task_once
101
+ if @state == :pending
102
+ begin
103
+ @value = @task . call
104
+ @state = :fulfilled
105
+ rescue => ex
106
+ @reason = ex
107
+ @state = :rejected
92
108
end
93
109
end
110
+ end
94
111
end
95
112
end
Original file line number Diff line number Diff line change @@ -53,6 +53,20 @@ def dereferenceable_subject(value, opts = {})
53
53
end
54
54
end
55
55
56
+
57
+ context '#reconfigure' do
58
+ it 'returns value of block used in reconfiguration' do
59
+ Delay . new { nil } . tap { |d | d . reconfigure { true } } . value . should be_true
60
+ end
61
+
62
+ it 'returns false when process completed?' do
63
+ d = Delay . new { 1 }
64
+ d . reconfigure { 2 } . should be_true
65
+ d . value . should be 2
66
+ d . reconfigure { 3 } . should be_false
67
+ end
68
+ end
69
+
56
70
context '#value' do
57
71
58
72
let ( :task ) { proc { nil } }
You can’t perform that action at this time.
0 commit comments