"use strict";
/* This is from the ISC licensed async-await-utils project, from here:
   https://github.com/masotime/async-await-utils/blob/master/src/hof/reuseInFlight.js
   Including that quite heavy (due to babel dep) project just for this one function
   is a bit much, so I copy/pasted it here.
*/
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var DEFAULT_CONFIG = {
    createKey: function (args) {
        return JSON.stringify(args);
    },
    ignoreSingleUndefined: false,
};
// for a given Promise-generating function, track each execution by the stringified
// arguments. if the function is called again with the same arguments, then instead
// of generating a new promise, an existing in-flight promise is used instead. This
// prevents unnecessary repetition of async function calls while the same function
// is still in flight.
function reuseInFlight(asyncFn, config) {
    config = __assign(__assign({}, DEFAULT_CONFIG), (config || {}));
    var inflight = {};
    return function debounced() {
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i] = arguments[_i];
        }
        if (config.ignoreSingleUndefined &&
            args.length === 1 &&
            args[0] === undefined) {
            console.warn("Ignoring single undefined arg (reuseInFlight)");
            args = [];
        }
        var key = config.createKey(args);
        if (!inflight.hasOwnProperty(key)) {
            // WE DO NOT AWAIT, we are storing the promise itself
            inflight[key] = asyncFn.apply(this, args).then(function (results) {
                // self invalidate
                delete inflight[key];
                return results;
            }, function (err) {
                // still self-invalidate, then rethrow
                delete inflight[key];
                throw err;
            });
        }
        return inflight[key];
    };
}
exports.default = reuseInFlight;
//# sourceMappingURL=reuseInFlight.js.map