use of ratpack.stream.internal.BufferingPublisher in project ratpack by ratpack.
the class DefaultParallelBatch method publisher.
@Override
public TransformablePublisher<T> publisher() {
Iterator<? extends Promise<T>> iterator = promises.iterator();
return new BufferingPublisher<>(Action.noop(), write -> {
return new Subscription() {
volatile boolean cancelled;
volatile boolean complete;
final AtomicLong finished = new AtomicLong();
volatile long started;
@Override
public void request(long n) {
while (n-- > 0 && !cancelled) {
if (iterator.hasNext()) {
++started;
Promise<T> promise = iterator.next();
if (!iterator.hasNext()) {
complete = true;
}
Execution.fork().onStart(execInit).onComplete(e -> {
long finished = this.finished.incrementAndGet();
if (finished == started && complete && !cancelled) {
write.complete();
}
}).start(e -> promise.onError(write::error).then(write::item));
} else {
return;
}
}
}
@Override
public void cancel() {
cancelled = true;
}
};
});
}
Aggregations