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

org.jboss.hal.flow.FlowRunner Maven / Gradle / Ivy

The newest version!
/*
 *  Copyright 2022 Red Hat
 *
 *  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 org.jboss.hal.flow;

import elemental2.promise.IThenable;
import elemental2.promise.Promise;

abstract class FlowRunner implements Promisable, Subscription {

    static final String TIMEOUT_ERROR = "flow.timeout";

    final C context;

    protected FlowRunner(final C context, final int tasks) {
        this.context = context;
        if (tasks > 1) {
            this.context.progress.reset(tasks);
        } else {
            this.context.progress.reset();
        }
    }

    // ------------------------------------------------------ promisable API

    @Override
    public  Promise then(final IThenable.ThenOnFulfilledCallbackFn onFulfilled) {
        return runAndCatch(true).then(onFulfilled);
    }

    @Override
    public  Promise then(final IThenable.ThenOnFulfilledCallbackFn onFulfilled,
            final IThenable.ThenOnRejectedCallbackFn onRejected) {
        return runAndCatch(true).then(onFulfilled, onRejected);
    }

    @Override
    public  Promise catch_(final Promise.CatchOnRejectedCallbackFn onRejected) {
        return runAndCatch(true).catch_(onRejected);
    }

    @Override
    public Promise finally_(final Promise.FinallyOnFinallyCallbackFn onFinally) {
        return runAndCatch(true).finally_(onFinally);
    }

    @Override
    public Promise promise() {
        return runAndCatch(true);
    }

    // ------------------------------------------------------ subscription API

    @Override
    public void subscribe(final FlowCallback callback) {
        runAndCatch(false).then(c -> {
            callback.finish(c);
            return null;
        });
    }

    // ------------------------------------------------------ run

    Promise runAndCatch(final boolean rejectOnError) {
        context.status = FlowStatus.IN_PROGRESS;
        return run()
                .then(c -> {
                    c.status = FlowStatus.SUCCESS;
                    return Promise.resolve(c);
                })
                .catch_(error -> {
                    if (TIMEOUT_ERROR.equals(error)) {
                        context.status = FlowStatus.TIMEOUT;
                    } else {
                        context.status = FlowStatus.FAILURE;
                        context.failure = String.valueOf(error);
                    }
                    if (rejectOnError) {
                        return Promise.reject(error);
                    } else {
                        return Promise.resolve(context);
                    }
                });
    }

    abstract Promise run();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy