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

ru.tinkoff.kora.common.util.flow.EmptySubscription Maven / Gradle / Ivy

There is a newer version: 1.1.16
Show newest version
package ru.tinkoff.kora.common.util.flow;

import ru.tinkoff.kora.common.Context;

import java.util.concurrent.Flow;
import java.util.concurrent.atomic.AtomicBoolean;

public final class EmptySubscription extends AtomicBoolean implements Flow.Subscription {
    private final Context context;
    private final Flow.Subscriber subscriber;

    public EmptySubscription(Context context, Flow.Subscriber subscriber) {
        this.context = context;
        this.subscriber = subscriber;
    }

    @Override
    public void request(long n) {
        assert n > 0;
        if (this.compareAndSet(false, true)) {
            var subscriber = this.subscriber;
            var ctx = Context.current();
            this.context.inject();
            try {
                subscriber.onComplete();
            } finally {
                ctx.inject();
            }
        }
    }

    @Override
    public void cancel() {
        this.set(true);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy