"use strict";
exports.id = 4181;
exports.ids = [4181];
exports.modules = {

/***/ 644181:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {


/*
 *  This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
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());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.async_as_callback = exports.cancel_scheduled = exports.reuse_in_flight_methods = exports.callback2 = exports.once = exports.retry_until_success = exports.callback_opts = void 0;
/*
Some async utils.

(Obviously should be moved somewhere else when the dust settles!)

The two helpful async/await libraries I found are:

   - https://github.com/hunterloftis/awaiting
   - https://github.com/masotime/async-await-utils

*/
var awaiting = __importStar(__webpack_require__(372007));
var hof_1 = __webpack_require__(752185);
// turns a function of opts, which has a cb input into
// an async function that takes an opts with no cb as input; this is just like
// awaiting.callback, but for our functions that take opts.
// WARNING: this is different than callback from awaiting, which
// on which you do:   callback(f, args...)
// With callback_opts, you do:   callback_opts(f)(opts)
// TODO: maybe change this everwhere to callback_opts(f, opts) for consistency!
function callback_opts(f) {
    return function (opts) {
        return __awaiter(this, void 0, void 0, function () {
            function g(cb) {
                opts.cb = cb;
                f(opts);
            }
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (opts === undefined) {
                            opts = {};
                        }
                        return [4 /*yield*/, awaiting.callback(g)];
                    case 1: return [2 /*return*/, _a.sent()];
                }
            });
        });
    };
}
exports.callback_opts = callback_opts;
function retry_until_success(opts) {
    return __awaiter(this, void 0, void 0, function () {
        // Return nonempty string if time or tries exceeded.
        function check_done() {
            if (opts.max_time &&
                next_delay + new Date().valueOf() - start_time > opts.max_time) {
                return "maximum time exceeded";
            }
            if (opts.max_tries && tries >= opts.max_tries) {
                return "maximum tries exceeded";
            }
            return "";
        }
        var next_delay, tries, start_time, last_exc, exc_1, err, e;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    if (!opts.start_delay)
                        opts.start_delay = 100;
                    if (!opts.max_delay)
                        opts.max_delay = 20000;
                    if (!opts.factor)
                        opts.factor = 1.4;
                    next_delay = opts.start_delay;
                    tries = 0;
                    start_time = new Date().valueOf();
                    _a.label = 1;
                case 1:
                    if (false) {}
                    _a.label = 2;
                case 2:
                    _a.trys.push([2, 4, , 6]);
                    return [4 /*yield*/, opts.f()];
                case 3: return [2 /*return*/, _a.sent()];
                case 4:
                    exc_1 = _a.sent();
                    //console.warn('retry_until_success', exc);
                    if (opts.log !== undefined) {
                        opts.log("failed ", exc_1);
                    }
                    // might try again -- update state...
                    tries += 1;
                    next_delay = Math.min(opts.max_delay, opts.factor * next_delay);
                    err = check_done();
                    if (err) {
                        e = void 0;
                        if (last_exc) {
                            e = Error("".concat(err, " -- last error was ").concat(last_exc, " -- ").concat(opts.desc));
                        }
                        else {
                            e = Error("".concat(err, " -- ").concat(opts.desc));
                        }
                        //console.warn(e);
                        throw e;
                    }
                    // record exception so can use it later.
                    last_exc = exc_1;
                    // wait before trying again
                    return [4 /*yield*/, awaiting.delay(next_delay)];
                case 5:
                    // wait before trying again
                    _a.sent();
                    return [3 /*break*/, 6];
                case 6: return [3 /*break*/, 1];
                case 7: return [2 /*return*/];
            }
        });
    });
}
exports.retry_until_success = retry_until_success;
var events_1 = __webpack_require__(582361);
/* Wait for an event emitter to emit any event at all once.
   Returns array of args emitted by that event.
   If timeout_ms is 0 (the default) this can wait an unbounded
   amount of time.  That's intentional and does make sense
   in our applications. */
function once(obj, event, timeout_ms) {
    if (timeout_ms === void 0) { timeout_ms = 0; }
    return __awaiter(this, void 0, void 0, function () {
        function wait(cb) {
            obj.once(event, function () {
                var args = [];
                for (var _i = 0; _i < arguments.length; _i++) {
                    args[_i] = arguments[_i];
                }
                val = args;
                cb();
            });
        }
        var val;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    if (!(obj instanceof events_1.EventEmitter)) {
                        // just in case typescript doesn't catch something:
                        throw Error("obj must be an EventEmitter");
                    }
                    if (timeout_ms > 0) {
                        // just to keep both versions more readable...
                        return [2 /*return*/, once_with_timeout(obj, event, timeout_ms)];
                    }
                    val = [];
                    return [4 /*yield*/, awaiting.callback(wait)];
                case 1:
                    _a.sent();
                    return [2 /*return*/, val];
            }
        });
    });
}
exports.once = once;
function once_with_timeout(obj, event, timeout_ms) {
    return __awaiter(this, void 0, void 0, function () {
        function wait(cb) {
            function fail() {
                obj.removeListener(event, handler);
                cb("timeout");
            }
            var timer = setTimeout(fail, timeout_ms);
            function handler() {
                var args = [];
                for (var _i = 0; _i < arguments.length; _i++) {
                    args[_i] = arguments[_i];
                }
                clearTimeout(timer);
                val = args;
                cb();
            }
            obj.once(event, handler);
        }
        var val;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    val = [];
                    return [4 /*yield*/, awaiting.callback(wait)];
                case 1:
                    _a.sent();
                    return [2 /*return*/, val];
            }
        });
    });
}
// Alternative to callback_opts that behaves like the
// callback defined in awaiting.
function callback2(f, opts) {
    if (opts === void 0) { opts = {}; }
    return __awaiter(this, void 0, void 0, function () {
        function g(cb) {
            opts.cb = cb;
            f(opts);
        }
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, awaiting.callback(g)];
                case 1: return [2 /*return*/, _a.sent()];
            }
        });
    });
}
exports.callback2 = callback2;
function reuse_in_flight_methods(obj, method_names) {
    var e_1, _a;
    try {
        for (var method_names_1 = __values(method_names), method_names_1_1 = method_names_1.next(); !method_names_1_1.done; method_names_1_1 = method_names_1.next()) {
            var method_name = method_names_1_1.value;
            obj[method_name] = (0, hof_1.reuseInFlight)(obj[method_name].bind(obj));
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (method_names_1_1 && !method_names_1_1.done && (_a = method_names_1.return)) _a.call(method_names_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
}
exports.reuse_in_flight_methods = reuse_in_flight_methods;
// Cancel pending throttle or debounce, where f is the
// output of underscore.throttle (or debounce).  Safe to call
// with f null or a normal function.
function cancel_scheduled(f) {
    if (f != null && f.cancel != null) {
        f.cancel();
    }
}
exports.cancel_scheduled = cancel_scheduled;
// WARNING -- not tested
function async_as_callback(f, cb) {
    var args = [];
    for (var _i = 2; _i < arguments.length; _i++) {
        args[_i - 2] = arguments[_i];
    }
    return __awaiter(this, void 0, void 0, function () {
        var err_1;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    _a.trys.push([0, 2, , 3]);
                    return [4 /*yield*/, f.apply(void 0, __spreadArray([], __read(args), false))];
                case 1:
                    _a.sent();
                    cb();
                    return [3 /*break*/, 3];
                case 2:
                    err_1 = _a.sent();
                    cb(err_1);
                    return [3 /*break*/, 3];
                case 3: return [2 /*return*/];
            }
        });
    });
}
exports.async_as_callback = async_as_callback;
//# sourceMappingURL=async-utils.js.map

/***/ })

};
;