@@ -50,7 +50,11 @@ non-sealed interface AsyncResultInternal<T> extends AsyncResult<T>, UnresolvedFu
5050
5151 void tryCancel ();
5252
53- void tryComplete (NotificationReader reader );
53+ /**
54+ * Attempts to complete this result from available notifications; returns whether it is now
55+ * done.
56+ */
57+ boolean tryComplete (NotificationReader reader );
5458
5559 CompletableFuture <T > publicFuture ();
5660
@@ -121,19 +125,21 @@ public void tryCancel() {
121125 }
122126
123127 @ Override
124- public void tryComplete (NotificationReader reader ) {
125- if (this .isDone ()) return ;
126- reader
127- .take (handle )
128- .ifPresent (
129- value -> {
130- try {
131- completer .complete (value , publicFuture );
132- } catch (Throwable e ) {
133- contextInternal .fail (e );
134- publicFuture .completeExceptionally (AbortedExecutionException .INSTANCE );
135- }
136- });
128+ public boolean tryComplete (NotificationReader reader ) {
129+ if (this .isDone ()) {
130+ return true ;
131+ }
132+ Optional <NotificationValue > value = reader .take (handle );
133+ if (value .isEmpty ()) {
134+ return false ;
135+ }
136+ try {
137+ completer .complete (value .get (), publicFuture );
138+ } catch (Throwable e ) {
139+ contextInternal .fail (e );
140+ publicFuture .completeExceptionally (AbortedExecutionException .INSTANCE );
141+ }
142+ return true ;
137143 }
138144
139145 @ Override
@@ -174,9 +180,12 @@ public void tryCancel() {
174180 }
175181
176182 @ Override
177- public void tryComplete (NotificationReader reader ) {
178- if (this .isDone ()) return ;
183+ public boolean tryComplete (NotificationReader reader ) {
184+ if (this .isDone ()) return true ;
179185 asyncResult .tryComplete (reader );
186+ // This is defensive, because the async result might not compelte immediately if there's a
187+ // mapper?!
188+ return this .isDone ();
180189 }
181190
182191 @ Override
@@ -293,15 +302,16 @@ public void tryCancel() {
293302 }
294303
295304 @ Override
296- public void tryComplete (NotificationReader reader ) {
297- if (this .isDone ()) return ;
305+ public boolean tryComplete (NotificationReader reader ) {
306+ if (this .isDone ()) return true ;
298307 asyncResults .forEach (ar -> ar .tryComplete (reader ));
299308 for (int i = 0 ; i < asyncResults .size (); i ++) {
300309 if (asyncResults .get (i ).isDone ()) {
301310 publicFuture .complete (i );
302- return ;
311+ return true ;
303312 }
304313 }
314+ return this .isDone ();
305315 }
306316
307317 @ Override
@@ -343,8 +353,8 @@ public void tryCancel() {
343353 }
344354
345355 @ Override
346- public void tryComplete (NotificationReader reader ) {
347- if (this .isDone ()) return ;
356+ public boolean tryComplete (NotificationReader reader ) {
357+ if (this .isDone ()) return true ;
348358 asyncResults .forEach (ar -> ar .tryComplete (reader ));
349359 asyncResults .stream ()
350360 .filter (ar -> ar .publicFuture ().isCompletedExceptionally ())
@@ -357,6 +367,7 @@ public void tryComplete(NotificationReader reader) {
357367 this .publicFuture .completeExceptionally (e .getCause ());
358368 }
359369 });
370+ return this .isDone ();
360371 }
361372
362373 @ Override
0 commit comments