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

io.micronaut.http.client.netty.MicronautFlux Maven / Gradle / Ivy

There is a newer version: 4.7.6
Show newest version
/*
 * Copyright 2017-2021 original authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.micronaut.http.client.netty;

import io.micronaut.core.annotation.Internal;
import org.reactivestreams.Subscription;
import reactor.core.CoreSubscriber;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxOperator;
import reactor.util.context.Context;

import java.util.function.Consumer;

/**
 * Custom extensions to project reactor.
 *
 * @author James Kleeh
 * @since 3.0.0
 * @param  The stream type
 */
@Internal
class MicronautFlux extends Flux {

    private final Flux flux;

    /**
     *
     * @param publisher Reactive Sequence
     */
    MicronautFlux(Flux publisher) {
        super();
        this.flux = publisher;
    }

    @Override
    public void subscribe(CoreSubscriber actual) {
        flux.subscribe(actual);
    }

    /**
     * Calls the specified Consumer with the current item after this item has been emitted to the downstream.
     * @param afterNext current item
     * @return Reactive sequence
     */
    public Flux doAfterNext(Consumer afterNext) {
        return onAssembly(new AfterNextOperator<>(flux, afterNext));
    }

    static class AfterNextOperator extends FluxOperator {

        private final Consumer afterNext;

        /**
         * Build a {@link FluxOperator} wrapper around the passed parent {@link org.reactivestreams.Publisher}.
         *
         * @param source the {@link org.reactivestreams.Publisher} to decorate
         * @param afterNext Consumer with the current item after this item has been emitted to the downstream.
         */
        protected AfterNextOperator(Flux source, Consumer afterNext) {
            super(source);
            this.afterNext = afterNext;
        }

        @Override
        public void subscribe(CoreSubscriber actual) {
            source.subscribe(new CoreSubscriber() {
                @Override
                public Context currentContext() {
                    return actual.currentContext();
                }

                @Override
                public void onSubscribe(Subscription s) {
                    actual.onSubscribe(s);
                }

                @Override
                public void onNext(T t) {
                    actual.onNext(t);
                    afterNext.accept(t);
                }

                @Override
                public void onError(Throwable t) {
                    actual.onError(t);
                }

                @Override
                public void onComplete() {
                    actual.onComplete();
                }
            });
        }
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy