void scheduleTimeout(long idx) {
      Disposable d = timer;
      if (d != null) {
        d.dispose();
      }

      if (TIMER.compareAndSet(this, d, NEW_TIMER)) {
        d =
            worker.schedule(
                () -> {
                  if (idx == index) {
                    done = true;
                    s.dispose();
                    dispose();

                    actual.onError(new TimeoutException());
                  }
                },
                timeout,
                unit);

        if (!TIMER.compareAndSet(this, NEW_TIMER, d)) {
          d.dispose();
        }
      }
    }
    @Override
    public void onComplete() {
      if (done) {
        return;
      }
      done = true;
      dispose();

      actual.onComplete();
    }
    @Override
    public void onSubscribe(Disposable s) {
      if (SubscriptionHelper.validateDisposable(this.s, s)) {
        return;
      }

      this.s = s;
      actual.onSubscribe(s);
      scheduleTimeout(0L);
    }
    @Override
    public void onError(Throwable t) {
      if (done) {
        RxJavaPlugins.onError(t);
        return;
      }
      done = true;
      dispose();

      actual.onError(t);
    }
    @Override
    public void onNext(T t) {
      if (done) {
        return;
      }
      long idx = index + 1;
      index = idx;

      actual.onNext(t);

      scheduleTimeout(idx);
    }