Source: groups.js

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConcurrentGroup = exports.SerialGroup = void 0;
/**
 * The base type for Runnables composed of other Runnables.
 * Do not construct the base Group type; use Job.sequential (or SerialGroup)
 * or Job.concurrent (or ConcurrentGroup) instead.
 *
 * @abstract
 */
class Group {
    constructor(...runnables) {
        this.runnables = runnables || [];
    }
    add(...runnables) {
        for (const runnable of runnables) {
            this.runnables.push(runnable);
        }
    }
    length() {
        return this.runnables.length;
    }
}
/**
 * A Runnable composed of sub-Runnables (such as jobs
 * or concurrent groups) running one after another.
 * A new Runnable is started only when the previous one completes.
 * The sequence completes when the last Runnable has completed (or when any
 * Runnable fails).
 *
 * @param runnables The work items to be run in sequence
 */
class SerialGroup extends Group {
    constructor(...runnables) {
        super(...runnables);
    }
    /**
     * Runs the serial group.
     *
     * @returns A Promise which completes when the last item in the group completes (or
     * any item fails). If all items ran successfully, the Promise resolves; if any
     * item failed (that is, its Runnable#run Promise rejected), the Promise
     * rejects.
     */
    run() {
        return __awaiter(this, void 0, void 0, function* () {
            for (const runnable of this.runnables) {
                yield runnable.run();
            }
        });
    }
}
exports.SerialGroup = SerialGroup;
/**
 * A Runnable composed of sub-Runnables (such as jobs
 * or sequential groups) running concurrently.
 * When run, all Runnables are started simultaneously (subject to
 * scheduling constraints).
 * The concurrent group completes when all Runnables have completed.
 *
 * @param runnables The work items to be run in parallel
 */
class ConcurrentGroup extends Group {
    constructor(...runnables) {
        super(...runnables);
    }
    /**
     * Runs the concurrent group.
     *
     * @returns A Promise which completes when all items in the group complete.
     * If all items ran successfully, the Promise resolves; if any
     * item failed (that is, its Runnable#run Promise rejected), the Promise
     * rejects.
     */
    run() {
        return __awaiter(this, void 0, void 0, function* () {
            const promises = [];
            for (const runnable of this.runnables) {
                promises.push(runnable.run());
            }
            try {
                yield Promise.all(promises);
                return Promise.resolve();
            }
            catch (e) {
                return Promise.reject(e);
            }
        });
    }
}
exports.ConcurrentGroup = ConcurrentGroup;
//# sourceMappingURL=groups.js.map