aboutsummaryrefslogtreecommitdiff
path: root/node_modules/app-builder/lib/compose.js
blob: e615d57e6bb144a581d7c96304c18d6d3564e8a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const flatten = (values) => [].concat(...values), noop = function noop() {
    return Promise.resolve();
};
function throwIfHasBeenCalled(fn) {
    if (fn.__appbuildercalled) {
        throw new Error('Cannot call next more than once');
    }
    return (fn.__appbuildercalled = true);
}
function tryInvokeMiddleware(context, middleware, next = noop) {
    try {
        return Promise.resolve(middleware ? middleware(context, next) : context);
    }
    catch (error) {
        return Promise.reject(error);
    }
}
function functionList(list, ...args) {
    const arrayList = Symbol.iterator in list ? Array.from(list) : [list];
    return arrayList.map((x) => {
        return (_, next) => Promise.resolve(x(...args)).then(next);
    });
}
exports.functionList = functionList;
/**
 * Create a function to invoke all passed middleware functions
 * with a single argument <T>context
 * @param middleware
 */
function compose(...middleware) {
    return flatten(middleware)
        .filter((x) => {
        if ('function' !== typeof x) {
            throw new TypeError(`${x}, must be a middleware function accpeting (context, next) arguments`);
        }
        return x;
    })
        .reduceRight((composed, mw) => {
        return function (context, nextFn) {
            const next = () => throwIfHasBeenCalled(next) && composed(context, nextFn);
            return tryInvokeMiddleware(context, mw, next);
        };
    }, tryInvokeMiddleware);
}
exports.compose = compose;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29tcG9zZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9jb21wb3NlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBT0EsTUFBTSxPQUFPLEdBQUcsQ0FBSSxNQUEyQixFQUFFLEVBQUUsQ0FBRSxFQUFVLENBQUMsTUFBTSxDQUFDLEdBQUcsTUFBTSxDQ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