@@ -10,36 +10,39 @@ module Edge
10
10
module FutureShortcuts
11
11
# TODO to construct event to be set later to trigger rest of the tree
12
12
13
- def event ( default_executor = :fast )
14
- CompletableEvent . new ( default_executor )
15
- end
16
-
17
- # Constructs new Future which will be completed after block is evaluated on executor. Evaluation begins immediately.
18
- # @return [Future]
19
- # @note TODO allow to pass in variables as Thread.new(args) {|args| _ } does
20
- def future ( default_executor = :fast , &task )
21
- ImmediatePromise . new ( default_executor ) . event . chain ( &task )
13
+ # User is responsible for completing the event once.
14
+ # @return [CompletableEvent]
15
+ def event ( default_executor = :io )
16
+ CompletableEventPromise . new ( default_executor ) . future
17
+ end
18
+
19
+ # @overload future(default_executor = :io, &task)
20
+ # Constructs new Future which will be completed after block is evaluated on executor. Evaluation begins immediately.
21
+ # @return [Future]
22
+ # @note FIXME allow to pass in variables as Thread.new(args) {|args| _ } does
23
+ # @overload future(default_executor = :io)
24
+ # User is responsible for completing the future once.
25
+ # @return [CompletableFuture]
26
+ def future ( default_executor = :io , &task )
27
+ if task
28
+ ImmediatePromise . new ( default_executor ) . event . chain ( &task )
29
+ else
30
+ CompletableFuturePromise . new ( default_executor ) . future
31
+ end
22
32
end
23
33
24
34
alias_method :async , :future
25
35
26
36
# Constructs new Future which will be completed after block is evaluated on executor. Evaluation is delays until
27
37
# requested by {Future#wait} method, {Future#value} and {Future#value!} methods are calling {Future#wait} internally.
28
38
# @return [Delay]
29
- def delay ( default_executor = :fast , &task )
39
+ def delay ( default_executor = :io , &task )
30
40
Delay . new ( default_executor ) . event . chain ( &task )
31
41
end
32
42
33
- # Constructs {Promise} which helds its {Future} in {AbstractPromise#future} method. Intended for completion by user.
34
- # User is responsible not to complete the Promise twice.
35
- # @return [AbstractPromise] in this case instance of {OuterPromise}
36
- def promise ( default_executor = :fast )
37
- CompletablePromise . new ( default_executor )
38
- end
39
-
40
43
# Schedules the block to be executed on executor in given intended_time.
41
44
# @return [Future]
42
- def schedule ( intended_time , default_executor = :fast , &task )
45
+ def schedule ( intended_time , default_executor = :io , &task )
43
46
ScheduledPromise . new ( intended_time , default_executor ) . future . chain ( &task )
44
47
end
45
48
@@ -82,7 +85,7 @@ class Event < Synchronization::Object
82
85
extend FutureShortcuts
83
86
84
87
# @api private
85
- def initialize ( promise , default_executor = :fast )
88
+ def initialize ( promise , default_executor = :io )
86
89
super ( )
87
90
synchronize { ns_initialize ( promise , default_executor ) }
88
91
end
@@ -196,7 +199,7 @@ def with_default_executor(executor = default_executor)
196
199
197
200
private
198
201
199
- def ns_initialize ( promise , default_executor = :fast )
202
+ def ns_initialize ( promise , default_executor = :io )
200
203
@promise = promise
201
204
@state = :pending
202
205
@callbacks = [ ]
@@ -375,13 +378,14 @@ def wait!(timeout = nil)
375
378
# @raise [Exception] when #failed? it raises #reason
376
379
# @return [Object] see Dereferenceable#deref
377
380
def value! ( timeout = nil )
381
+ touch
378
382
synchronize { ns_value! timeout }
379
383
end
380
384
381
- # @example allows Obligation to be risen
382
- # failed_ivar = Ivar.new.fail
383
- # raise failed_ivar
385
+ # @example allows failed Future to be risen
386
+ # raise Concurrent.future.fail
384
387
def exception ( *args )
388
+ touch
385
389
synchronize { ns_exception ( *args ) }
386
390
end
387
391
@@ -447,7 +451,7 @@ def ns_add_callback(method, *args)
447
451
448
452
private
449
453
450
- def ns_initialize ( promise , default_executor = :fast )
454
+ def ns_initialize ( promise , default_executor = :io )
451
455
super ( promise , default_executor )
452
456
@value = nil
453
457
@reason = nil
@@ -528,8 +532,6 @@ def ns_complete(success, value, reason, raise = true)
528
532
callbacks
529
533
end
530
534
531
- private
532
-
533
535
def ns_complete_state ( success , value , reason )
534
536
if success
535
537
@value = value
@@ -573,6 +575,46 @@ def pr_async_callback_on_completion(success, value, reason, executor, callback)
573
575
end
574
576
end
575
577
578
+ class CompletableEvent < Event
579
+ # Complete the event
580
+ # @api public
581
+ def complete ( raise = true )
582
+ super raise
583
+ end
584
+ end
585
+
586
+ class CompletableFuture < Future
587
+ # Complete the future
588
+ # @api public
589
+ def complete ( success , value , reason , raise = true )
590
+ super success , value , reason , raise
591
+ end
592
+
593
+ def success ( value )
594
+ promise . success ( value )
595
+ end
596
+
597
+ def try_success ( value )
598
+ promise . try_success ( value )
599
+ end
600
+
601
+ def fail ( reason = StandardError . new )
602
+ promise . fail ( reason )
603
+ end
604
+
605
+ def try_fail ( reason = StandardError . new )
606
+ promise . try_fail ( reason )
607
+ end
608
+
609
+ def evaluate_to ( *args , &block )
610
+ promise . evaluate_to ( *args , &block )
611
+ end
612
+
613
+ def evaluate_to! ( *args , &block )
614
+ promise . evaluate_to! ( *args , &block )
615
+ end
616
+ end
617
+
576
618
# TODO modularize blocked_by and notify blocked
577
619
578
620
# @abstract
@@ -638,21 +680,21 @@ def pr_evaluate_to(future, *args, &block)
638
680
end
639
681
end
640
682
641
- class CompletableEvent < AbstractPromise
683
+ class CompletableEventPromise < AbstractPromise
642
684
public :complete
643
685
644
686
private
645
687
646
- def ns_initialize ( default_executor = :fast )
647
- super Event . new ( self , default_executor )
688
+ def ns_initialize ( default_executor = :io )
689
+ super CompletableEvent . new ( self , default_executor )
648
690
end
649
691
end
650
692
651
693
# @note Be careful not to fullfill the promise twice
652
694
# @example initialization
653
695
# Concurrent.promise
654
696
# @note TODO consider to allow being blocked_by
655
- class CompletablePromise < CompletableEvent
697
+ class CompletableFuturePromise < AbstractPromise
656
698
# Set the `IVar` to a value and wake or notify all threads waiting on it.
657
699
#
658
700
# @param [Object] value the value to store in the `IVar`
@@ -679,6 +721,7 @@ def try_fail(reason = StandardError.new)
679
721
!!complete ( false , nil , reason , false )
680
722
end
681
723
724
+ public :complete
682
725
public :evaluate_to
683
726
684
727
# @return [Future]
@@ -688,8 +731,8 @@ def evaluate_to!(*args, &block)
688
731
689
732
private
690
733
691
- def ns_initialize ( default_executor = :fast )
692
- super Future . new ( self , default_executor )
734
+ def ns_initialize ( default_executor = :io )
735
+ super CompletableFuture . new ( self , default_executor )
693
736
end
694
737
end
695
738
@@ -765,7 +808,7 @@ def executor
765
808
766
809
private
767
810
768
- def ns_initialize ( blocked_by_future , default_executor = :fast , executor = default_executor , &task )
811
+ def ns_initialize ( blocked_by_future , default_executor = :io , executor = default_executor , &task )
769
812
raise ArgumentError , 'no block given' unless block_given?
770
813
super Future . new ( self , default_executor ) , [ blocked_by_future ]
771
814
@task = task
@@ -796,7 +839,7 @@ def pr_completable(_, _, _, _, _)
796
839
class ThenPromise < BlockedTaskPromise
797
840
private
798
841
799
- def ns_initialize ( blocked_by_future , default_executor = :fast , executor = default_executor , &task )
842
+ def ns_initialize ( blocked_by_future , default_executor = :io , executor = default_executor , &task )
800
843
blocked_by_future . is_a? Future or
801
844
raise ArgumentError , 'only Future can be appended with then'
802
845
super ( blocked_by_future , default_executor , executor , &task )
@@ -814,7 +857,7 @@ def pr_completable(done_future, _, future, executor, task)
814
857
class RescuePromise < BlockedTaskPromise
815
858
private
816
859
817
- def ns_initialize ( blocked_by_future , default_executor = :fast , executor = default_executor , &task )
860
+ def ns_initialize ( blocked_by_future , default_executor = :io , executor = default_executor , &task )
818
861
blocked_by_future . is_a? Future or
819
862
raise ArgumentError , 'only Future can be rescued'
820
863
super ( blocked_by_future , default_executor , executor , &task )
@@ -851,7 +894,7 @@ def self.new(*args)
851
894
852
895
private
853
896
854
- def ns_initialize ( default_executor = :fast )
897
+ def ns_initialize ( default_executor = :io )
855
898
super Event . new ( self , default_executor )
856
899
end
857
900
end
@@ -877,7 +920,7 @@ def ns_done(future)
877
920
super future
878
921
end
879
922
880
- def ns_initialize ( blocked_by_future , levels = 1 , default_executor = :fast )
923
+ def ns_initialize ( blocked_by_future , levels = 1 , default_executor = :io )
881
924
blocked_by_future . is_a? Future or
882
925
raise ArgumentError , 'only Future can be flatten'
883
926
super ( Future . new ( self , default_executor ) , [ blocked_by_future ] )
@@ -893,8 +936,9 @@ def pr_completable(_, blocked_by, future)
893
936
class AllPromise < BlockedPromise
894
937
private
895
938
896
- def ns_initialize ( blocked_by_futures , default_executor = :fast )
939
+ def ns_initialize ( blocked_by_futures , default_executor = :io )
897
940
klass = blocked_by_futures . any? { |f | f . is_a? ( Future ) } ? Future : Event
941
+ # noinspection RubyArgCount
898
942
super ( klass . new ( self , default_executor ) , blocked_by_futures )
899
943
end
900
944
@@ -918,7 +962,7 @@ class AnyPromise < BlockedPromise
918
962
919
963
private
920
964
921
- def ns_initialize ( blocked_by_futures , default_executor = :fast )
965
+ def ns_initialize ( blocked_by_futures , default_executor = :io )
922
966
blocked_by_futures . all? { |f | f . is_a? Future } or
923
967
raise ArgumentError , 'accepts only Futures not Events'
924
968
super ( Future . new ( self , default_executor ) , blocked_by_futures )
@@ -940,7 +984,7 @@ def touch
940
984
941
985
private
942
986
943
- def ns_initialize ( default_executor = :fast )
987
+ def ns_initialize ( default_executor = :io )
944
988
super Event . new ( self , default_executor )
945
989
end
946
990
end
@@ -957,7 +1001,7 @@ def inspect
957
1001
958
1002
private
959
1003
960
- def ns_initialize ( intended_time , default_executor = :fast )
1004
+ def ns_initialize ( intended_time , default_executor = :io )
961
1005
super Event . new ( self , default_executor )
962
1006
in_seconds = begin
963
1007
@intended_time = intended_time
0 commit comments