33import org .dataloader .DataLoader ;
44
55import java .util .concurrent .CompletableFuture ;
6+ import java .util .concurrent .Executor ;
7+ import java .util .function .Consumer ;
68import java .util .function .Function ;
9+ import java .util .function .Supplier ;
710
811import static org .dataloader .orchestration .Orchestrator .castAs ;
912
@@ -35,7 +38,11 @@ public Step<K, V> load(K key, Object keyContext) {
3538 }
3639
3740 public Step <V , V > thenLoad (Function <V , K > codeToRun ) {
38- return thenLoadImpl (orchestrator , dl , codeToRun );
41+ return thenLoadImpl (orchestrator , dl , codeToRun , false );
42+ }
43+
44+ public Step <V , V > thenLoadAsync (Function <V , K > codeToRun ) {
45+ return thenLoadImpl (orchestrator , dl , codeToRun , true );
3946 }
4047
4148 static <K , V > Step <K , V > loadImpl (Orchestrator <?, ?> orchestrator , DataLoader <Object , Object > dl , K key , Object keyContext ) {
@@ -49,16 +56,42 @@ static <K, V> Step<K, V> loadImpl(Orchestrator<?, ?> orchestrator, DataLoader<Ob
4956 return step ;
5057 }
5158
52- static <K , V > Step <V , V > thenLoadImpl (Orchestrator <?, ?> orchestrator , DataLoader <Object , Object > dl , Function <V , K > codeToRun ) {
53- Function <V , CompletableFuture <V >> actualCodeToRun = v -> {
59+ static <K , V > Step <V , V > thenLoadImpl (Orchestrator <?, ?> orchestrator , DataLoader <Object , Object > dl , Function <V , K > codeToRun , boolean async ) {
60+ Tracker tracker = orchestrator .getTracker ();
61+ Function <V , CompletableFuture <V >> actualCodeToRun ;
62+ if (async ) {
63+ actualCodeToRun = mkAsyncLoadLambda (orchestrator , dl , codeToRun , tracker );
64+ } else {
65+ actualCodeToRun = mkSyncLoadLambda (dl , codeToRun , tracker );
66+ }
67+ Step <V , V > step = new Step <>(orchestrator , dl , actualCodeToRun );
68+ orchestrator .record (step );
69+ return step ;
70+ }
71+
72+ private static <K , V > Function <V , CompletableFuture <V >> mkSyncLoadLambda (DataLoader <Object , Object > dl , Function <V , K > codeToRun , Tracker tracker ) {
73+ return v -> {
5474 K key = codeToRun .apply (v );
5575 CompletableFuture <V > cf = castAs (dl .load (key ));
56- orchestrator . getTracker () .loadCall (dl );
76+ tracker .loadCall (dl );
5777 return cf ;
5878 };
59- Step <V , V > step = new Step <>(orchestrator , dl , actualCodeToRun );
60- orchestrator .record (step );
61- return step ;
79+ }
80+
81+ private static <K , V > Function <V , CompletableFuture <V >> mkAsyncLoadLambda (Orchestrator <?, ?> orchestrator , DataLoader <Object , Object > dl , Function <V , K > codeToRun , Tracker tracker ) {
82+ return v -> {
83+ Executor executor = orchestrator .getExecutor ();
84+ Consumer <String > callback = atSomePointWeNeedMoreStateButUsingStringForNowToMakeItCompile -> {
85+ tracker .loadCall (dl );
86+ };
87+ ObservingExecutor <String > observingExecutor = new ObservingExecutor <>(executor , "state" , callback );
88+ Supplier <CompletableFuture <V >> dataLoaderCall = () -> {
89+ K key = codeToRun .apply (v );
90+ return castAs (dl .load (key ));
91+ };
92+ return CompletableFuture .supplyAsync (dataLoaderCall , observingExecutor )
93+ .thenCompose (Function .identity ());
94+ };
6295 }
6396
6497 public CompletableFuture <V > toCompletableFuture () {
0 commit comments