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,GAAQ,YAC3C,IAAI,EAAQ,KAAK,aACjB,YAAc,IAAV,EACO,EAEJ,IAAI,SAAQ,CAAC,EAAS,KACrB,KAAK,UAAY,KAAK,SACtB,KAAK,QAAS,EACd,KAAK,WAET,KAAK,QAAQ,KAAK,CAAE,UAAS,UAAS,GAE9C,GACJ,CACA,OAAA,CAAQ,GACJ,KAAK,eAAe,KAAK,UAAW,KAAK,SAAW,IAAM,EAC9D,CACA,KAAA,GACI,MAAM,EAAI,IAAI,MAAM,KAAK,UACzB,IAAK,IAAI,EAAI,EAAG,EAAI,KAAK,SAAU,IAC/B,EAAE,GAAK,KAAK,UAEhB,OAAO,QAAQ,IAAI,EACvB,CACA,SAAA,GACI,OAAO,KAAK,QAAQ,MACxB,EAGJ,SAAS,UAAU,GAAK,SAAE,EAAW,IAAI,oBAAE,GAAsB,GAAW,CAAC,GACzE,MAAM,EAAO,IAAI,KAAK,EAAsB,EAAI,GAC1C,EAAQ,EAAsB,EAAW,EAAM,EACrD,OAAO,WACH,OAAO,QAAQ,UAAU,UAAM,OAAQ,GAAQ,kBACrC,EAAK,UACX,YAAW,IAAM,EAAK,WAAW,EACrC,GACJ,CACJ,CAVA,QAAQ,KAAO,KAWf,QAAQ,UAAY","file":"/npm/@nixjs23n6/async-sema@0.0.1/lib/cjs/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/@nixjs23n6/async-sema@0.0.1/lib/cjs/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.RateLimit = exports.Sema = void 0;\nconst tslib_1 = require(\"tslib\");\nconst events_1 = tslib_1.__importDefault(require(\"events\"));\nfunction arrayMove(src, srcIndex, dst, dstIndex, len) {\n    for (let j = 0; j < len; ++j) {\n        dst[j + dstIndex] = src[j + srcIndex];\n        src[j + srcIndex] = void 0;\n    }\n}\nfunction pow2AtLeast(n) {\n    n = n >>> 0;\n    n = n - 1;\n    n = n | (n >> 1);\n    n = n | (n >> 2);\n    n = n | (n >> 4);\n    n = n | (n >> 8);\n    n = n | (n >> 16);\n    return n + 1;\n}\nfunction getCapacity(capacity) {\n    return pow2AtLeast(Math.min(Math.max(16, capacity), 1073741824));\n}\n// Deque is based on https://github.com/petkaantonov/deque/blob/master/js/deque.js\n// Released under the MIT License: https://github.com/petkaantonov/deque/blob/6ef4b6400ad3ba82853fdcc6531a38eb4f78c18c/LICENSE\nclass Deque {\n    constructor(capacity) {\n        this._capacity = getCapacity(capacity);\n        this._length = 0;\n        this._front = 0;\n        this.arr = [];\n    }\n    push(item) {\n        const length = this._length;\n        this.checkCapacity(length + 1);\n        const i = (this._front + length) & (this._capacity - 1);\n        this.arr[i] = item;\n        this._length = length + 1;\n        return length + 1;\n    }\n    pop() {\n        const length = this._length;\n        if (length === 0) {\n            return void 0;\n        }\n        const i = (this._front + length - 1) & (this._capacity - 1);\n        const ret = this.arr[i];\n        this.arr[i] = void 0;\n        this._length = length - 1;\n        return ret;\n    }\n    shift() {\n        const length = this._length;\n        if (length === 0) {\n            return void 0;\n        }\n        const front = this._front;\n        const ret = this.arr[front];\n        this.arr[front] = void 0;\n        this._front = (front + 1) & (this._capacity - 1);\n        this._length = length - 1;\n        return ret;\n    }\n    get length() {\n        return this._length;\n    }\n    checkCapacity(size) {\n        if (this._capacity < size) {\n            this.resizeTo(getCapacity(this._capacity * 1.5 + 16));\n        }\n    }\n    resizeTo(capacity) {\n        const oldCapacity = this._capacity;\n        this._capacity = capacity;\n        const front = this._front;\n        const length = this._length;\n        if (front + length > oldCapacity) {\n            const moveItemsCount = (front + length) & (oldCapacity - 1);\n            arrayMove(this.arr, 0, this.arr, oldCapacity, moveItemsCount);\n        }\n    }\n}\nclass ReleaseEmitter extends events_1.default {\n}\nfunction isFn(x) {\n    return typeof x === 'function';\n}\nfunction defaultInit() {\n    return '1';\n}\nclass Sema {\n    constructor(nr, { initFn = defaultInit, pauseFn, resumeFn, capacity = 10, } = {}) {\n        if (isFn(pauseFn) !== isFn(resumeFn)) {\n            throw new Error('pauseFn and resumeFn must be both set for pausing');\n        }\n        this.nrTokens = nr;\n        this.free = new Deque(nr);\n        this.waiting = new Deque(capacity);\n        this.releaseEmitter = new ReleaseEmitter();\n        this.noTokens = initFn === defaultInit;\n        this.pauseFn = pauseFn;\n        this.resumeFn = resumeFn;\n        this.paused = false;\n        this.releaseEmitter.on('release', (token) => {\n            const p = this.waiting.shift();\n            if (p) {\n                p.resolve(token);\n            }\n            else {\n                if (this.resumeFn && this.paused) {\n                    this.paused = false;\n                    this.resumeFn();\n                }\n                this.free.push(token);\n            }\n        });\n        for (let i = 0; i < nr; i++) {\n            this.free.push(initFn());\n        }\n    }\n    tryAcquire() {\n        return this.free.pop();\n    }\n    acquire() {\n        return tslib_1.__awaiter(this, void 0, void 0, function* () {\n            let token = this.tryAcquire();\n            if (token !== void 0) {\n                return token;\n            }\n            return new Promise((resolve, reject) => {\n                if (this.pauseFn && !this.paused) {\n                    this.paused = true;\n                    this.pauseFn();\n                }\n                this.waiting.push({ resolve, reject });\n            });\n        });\n    }\n    release(token) {\n        this.releaseEmitter.emit('release', this.noTokens ? '1' : token);\n    }\n    drain() {\n        const a = new Array(this.nrTokens);\n        for (let i = 0; i < this.nrTokens; i++) {\n            a[i] = this.acquire();\n        }\n        return Promise.all(a);\n    }\n    nrWaiting() {\n        return this.waiting.length;\n    }\n}\nexports.Sema = Sema;\nfunction RateLimit(rps, { timeUnit = 1000, uniformDistribution = false, } = {}) {\n    const sema = new Sema(uniformDistribution ? 1 : rps);\n    const delay = uniformDistribution ? timeUnit / rps : timeUnit;\n    return function rl() {\n        return tslib_1.__awaiter(this, void 0, void 0, function* () {\n            yield sema.acquire();\n            setTimeout(() => sema.release(), delay);\n        });\n    };\n}\nexports.RateLimit = RateLimit;\n"]}