Skip to content

Commit 505b9f4

Browse files
committed
Add chain_completable to Event and Future to connect different trees
1 parent 4cd5b64 commit 505b9f4

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/concurrent/edge/future.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@ def chain(executor = nil, &callback)
223223

224224
alias_method :then, :chain
225225

226+
def chain_completable(completable_event)
227+
on_completion! { completable_event.complete_with COMPLETED }
228+
end
229+
230+
alias_method :tangle, :chain_completable
231+
226232
# Zip with future producing new Future
227233
# @return [Event]
228234
def zip(other)
@@ -606,6 +612,12 @@ def then_ask(actor)
606612
self.then { |v| actor.ask(v) }.flat
607613
end
608614

615+
def chain_completable(completable_future)
616+
on_completion! { completable_future.complete_with internal_state }
617+
end
618+
619+
alias_method :tangle, :chain_completable
620+
609621
# @yield [reason] executed only on parent failure
610622
# @return [Future]
611623
def rescue(executor = nil, &callback)

spec/concurrent/edge/future_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@
33

44
describe 'Concurrent::Edge futures' do
55

6+
describe 'chain_completable' do
7+
it 'event' do
8+
b = Concurrent.event
9+
a = Concurrent.event.chain_completable(b)
10+
a.complete
11+
expect(b).to be_completed
12+
end
13+
14+
it 'future' do
15+
b = Concurrent.future
16+
a = Concurrent.future.chain_completable(b)
17+
a.success :val
18+
expect(b).to be_completed
19+
expect(b.value).to eq :val
20+
end
21+
end
22+
623
describe '.post' do
724
it 'executes tasks asynchronously' do
825
queue = Queue.new

0 commit comments

Comments
 (0)