All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vlingo.reactivestreams.StreamSubscriberDelegate Maven / Gradle / Ivy

There is a newer version: 1.7.5
Show newest version
// Copyright © 2012-2020 VLINGO LABS. All rights reserved.
//
// This Source Code Form is subject to the terms of the
// Mozilla Public License, v. 2.0. If a copy of the MPL
// was not distributed with this file, You can obtain
// one at https://mozilla.org/MPL/2.0/.

package io.vlingo.reactivestreams;

import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

import io.vlingo.actors.Logger;

public class StreamSubscriberDelegate implements Subscriber {
  private boolean completed;
  private long count;
  private boolean errored;
  private final Logger logger;
  private final long requestThreshold;
  private final Sink sink;
  private Subscription subscription;

  public StreamSubscriberDelegate(final Sink sink, final long requestThreshold, final Logger logger) {
    this.sink = sink;
    this.requestThreshold = requestThreshold;
    this.logger = logger;
    this.count = 0;
    this.completed = false;
    this.errored = false;
  }

  @Override
  public void onSubscribe(final Subscription subscription) {
    if (this.subscription == null) {
      this.subscription = subscription;
      this.sink.ready();
      this.subscription.request(requestThreshold);
    } else {
      subscription.cancel();
    }
  }

  @Override
  public void onNext(final T value) {
    if (!isFinalized()) {
      sink.whenValue(value);

      if (++count >= requestThreshold) {
        subscription.request(requestThreshold);
      }
    }
  }

  @Override
  public void onComplete() {
    this.completed = true;

    terminate();

    logger.info("Subscriber with " + sink + " is completed.");
  }

  @Override
  public void onError(final Throwable cause) {
    this.errored = true;

    terminate();

    logger.error("Subscriber with " + sink + " is terminating because: " + cause.getMessage(), cause);
  }

  public boolean isFinalized() {
    return completed || errored;
  }

  public void cancelSubscription() {
    subscription.cancel();
  }

  private void terminate() {
    sink.terminate();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy