Skip to content

Commit 2a4d5d4

Browse files
committed
Add Scheduled a Promise implementation replacing ScheduledTask.
1 parent e086eba commit 2a4d5d4

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

lib/concurrent/next.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ def promise(executor = :fast)
150150
ConcurrentNext::OuterPromise.new([], executor)
151151
end
152152

153+
# Schedules the block to be executed on executor in given intended_time.
154+
# @return [Future]
155+
def schedule(intended_time, executor = :fast, &task)
156+
Scheduled.new(intended_time, executor, &task).future
157+
end
158+
153159
# fails on first error
154160
# does not block a thread
155161
# @return [Future]
@@ -609,6 +615,24 @@ def initialize(executor = :fast, &task)
609615
end
610616
end
611617

618+
class Scheduled < Promise
619+
def initialize(intended_time, executor = :fast, &task)
620+
super(executor)
621+
schedule_time = synchronize do
622+
@schedule_time = Concurrent::TimerSet.calculate_schedule_time(intended_time)
623+
end
624+
625+
# TODO review
626+
Concurrent::timer(schedule_time.to_f - Time.now.to_f) do
627+
ConcurrentNext.executor(executor).post { evaluate_to &task }
628+
end
629+
end
630+
631+
def schedule_time
632+
synchronize { @schedule_time }
633+
end
634+
end
635+
612636
# will be evaluated to task when first requested
613637
class Delay < Promise
614638
def initialize(blocked_by_future, executor = :fast, &task)
@@ -755,6 +779,12 @@ def execute_once
755779
# or just
756780
p ConcurrentNext.promise.connect_to(source).value
757781

782+
puts '-- scheduled'
783+
784+
start = Time.now.to_f
785+
ConcurrentNext.schedule(0.1) { 1 + 1 }.then { |v| p v, Time.now.to_f - start}
786+
sleep 0.2
787+
758788
puts '-- using shortcuts'
759789

760790
include ConcurrentNext # includes Future::Shortcuts

0 commit comments

Comments
 (0)