CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
withTimeout.js129 linesDownload Raw Back to lib
1"use strict";2Object.defineProperty(exports, "__esModule", { value: true });3exports.withTimeout = void 0;4var tslib_1 = require("tslib");5/* eslint-disable @typescript-eslint/no-explicit-any */6var errors_1 = require("./errors");7function withTimeout(sync, timeout, timeoutError) {8    var _this = this;9    if (timeoutError === void 0) { timeoutError = errors_1.E_TIMEOUT; }10    return {11        acquire: function (weightOrPriority, priority) {12            var weight;13            if (isSemaphore(sync)) {14                weight = weightOrPriority;15            }16            else {17                weight = undefined;18                priority = weightOrPriority;19            }20            if (weight !== undefined && weight <= 0) {21                throw new Error("invalid weight ".concat(weight, ": must be positive"));22            }23            return new Promise(function (resolve, reject) { return tslib_1.__awaiter(_this, void 0, void 0, function () {24                var isTimeout, handle, ticket, release, e_1;25                return tslib_1.__generator(this, function (_a) {26                    switch (_a.label) {27                        case 0:28                            isTimeout = false;29                            handle = setTimeout(function () {30                                isTimeout = true;31                                reject(timeoutError);32                            }, timeout);33                            _a.label = 1;34                        case 1:35                            _a.trys.push([1, 3, , 4]);36                            return [4 /*yield*/, (isSemaphore(sync)37                                    ? sync.acquire(weight, priority)38                                    : sync.acquire(priority))];39                        case 2:40                            ticket = _a.sent();41                            if (isTimeout) {42                                release = Array.isArray(ticket) ? ticket[1] : ticket;43                                release();44                            }45                            else {46                                clearTimeout(handle);47                                resolve(ticket);48                            }49                            return [3 /*break*/, 4];50                        case 3:51                            e_1 = _a.sent();52                            if (!isTimeout) {53                                clearTimeout(handle);54                                reject(e_1);55                            }56                            return [3 /*break*/, 4];57                        case 4: return [2 /*return*/];58                    }59                });60            }); });61        },62        runExclusive: function (callback, weight, priority) {63            return tslib_1.__awaiter(this, void 0, void 0, function () {64                var release, ticket;65                return tslib_1.__generator(this, function (_a) {66                    switch (_a.label) {67                        case 0:68                            release = function () { return undefined; };69                            _a.label = 1;70                        case 1:71                            _a.trys.push([1, , 7, 8]);72                            return [4 /*yield*/, this.acquire(weight, priority)];73                        case 2:74                            ticket = _a.sent();75                            if (!Array.isArray(ticket)) return [3 /*break*/, 4];76                            release = ticket[1];77                            return [4 /*yield*/, callback(ticket[0])];78                        case 3: return [2 /*return*/, _a.sent()];79                        case 4:80                            release = ticket;81                            return [4 /*yield*/, callback()];82                        case 5: return [2 /*return*/, _a.sent()];83                        case 6: return [3 /*break*/, 8];84                        case 7:85                            release();86                            return [7 /*endfinally*/];87                        case 8: return [2 /*return*/];88                    }89                });90            });91        },92        release: function (weight) {93            sync.release(weight);94        },95        cancel: function () {96            return sync.cancel();97        },98        waitForUnlock: function (weightOrPriority, priority) {99            var weight;100            if (isSemaphore(sync)) {101                weight = weightOrPriority;102            }103            else {104                weight = undefined;105                priority = weightOrPriority;106            }107            if (weight !== undefined && weight <= 0) {108                throw new Error("invalid weight ".concat(weight, ": must be positive"));109            }110            return new Promise(function (resolve, reject) {111                var handle = setTimeout(function () { return reject(timeoutError); }, timeout);112                (isSemaphore(sync)113                    ? sync.waitForUnlock(weight, priority)114                    : sync.waitForUnlock(priority)).then(function () {115                    clearTimeout(handle);116                    resolve();117                });118            });119        },120        isLocked: function () { return sync.isLocked(); },121        getValue: function () { return sync.getValue(); },122        setValue: function (value) { return sync.setValue(value); },123    };124}125exports.withTimeout = withTimeout;126function isSemaphore(sync) {127    return sync.getValue !== undefined;128}129 
basant307/AI_Governance_Project · CoolFace