add generator for futur-lazy

This commit is contained in:
WhatCats
2025-01-06 23:08:59 +01:00
parent 4236adbd9e
commit 6174193145
11 changed files with 595 additions and 188 deletions

View File

@@ -48,8 +48,9 @@ public abstract class AbstractPromise<T, FS, FA> implements CompletablePromise<T
@NotNull ExceptionalFunction<T, V> completer
) {
return () -> {
if (promise.isCompleted()) return;
runCompleter(promise, () -> promise.complete(completer.apply(result)));
if (!promise.isCompleted()) {
runCompleter(promise, () -> promise.complete(completer.apply(result)));
}
};
}
@@ -479,14 +480,14 @@ public abstract class AbstractPromise<T, FS, FA> implements CompletablePromise<T
while (iter.hasNext()) {
PromiseListener<T> listener = iter.next();
if (listener instanceof AsyncPromiseListener) {
callListenerAsync(listener, ctx);
} else {
try {
try {
if (listener instanceof AsyncPromiseListener) {
callListenerAsync(listener, ctx);
} else {
callListenerNow(listener, ctx);
} finally {
iter.forEachRemaining(v -> callListenerAsyncLastResort(v, ctx));
}
} finally {
iter.forEachRemaining(v -> callListenerAsyncLastResort(v, ctx));
}
}
}
@@ -531,16 +532,18 @@ public abstract class AbstractPromise<T, FS, FA> implements CompletablePromise<T
@Override
public @NotNull CompletableFuture<T> toFuture() {
CompletableFuture<T> future = new CompletableFuture<>();
this.addDirectListener(future::complete, future::completeExceptionally);
addDirectListener(future::complete, future::completeExceptionally);
future.whenComplete((_, e) -> {
if (e instanceof CancellationException) {
this.cancel();
}
});
return future;
}
private static class DeferredExecutionException extends ExecutionException {
}
}