38
38
# An example loader which is blocking and synchronous as a whole, but executes all of its operations concurrently.
39
39
module Loaders
40
40
class HTTPLoader < GraphQL ::Batch ::Loader
41
+ include GraphQL ::Batch ::Async
42
+
41
43
def initialize ( host :, size : 4 , timeout : 4 )
42
44
super ( )
43
45
@host = host
@@ -46,7 +48,7 @@ def initialize(host:, size: 4, timeout: 4)
46
48
@futures = { }
47
49
end
48
50
49
- def perform_on_wait ( operations )
51
+ def perform_early ( operations )
50
52
# This fans out and starts off all the concurrent work, which starts and
51
53
# immediately returns Concurrent::Promises::Future` objects for each operation.
52
54
operations . each do |operation |
@@ -55,17 +57,14 @@ def perform_on_wait(operations)
55
57
end
56
58
57
59
def perform ( operations )
58
- # Defer to let other non-async loaders run to completion first.
59
- defer
60
-
61
60
# Collect the futures (and possibly trigger any newly added ones)
62
61
futures = operations . map do |operation |
63
62
future ( operation )
64
63
end
65
64
66
65
# At this point, all of the concurrent work has been started.
67
66
68
- # This converges back in, waiting on each concurrent future to finish, and fulfilling each
67
+ # Now it converges back in, waiting on each concurrent future to finish, and fulfilling each
69
68
# (non-concurrent) Promise.rb promise.
70
69
operations . each_with_index . each do |operation , index |
71
70
fulfill ( operation , futures [ index ] . value! ) # .value is a blocking call
0 commit comments