Comparing version 85.0.0 to 86.0.0
@@ -5,6 +5,2 @@ import { AsyncFunction } from "./typing.js"; | ||
export declare const rateLimit: <Function_1 extends AsyncFunction>(maxCalls: number, maxWeight: number, timeWindowMs: number, weight: (...args: Parameters<Function_1>) => number, f: Function_1) => Function_1; | ||
export declare const semaphore: (max: number) => { | ||
acquire: () => Promise<void>; | ||
release: () => void; | ||
}; | ||
export declare const throttle: <Function_1 extends AsyncFunction>(max: number) => (f: Function_1) => (...args: Parameters<Function_1>) => Promise<any>; | ||
export declare const throttle: (max: number) => <F extends AsyncFunction>(f: F) => F; |
@@ -56,3 +56,3 @@ import { sleep } from "./time.js"; | ||
}; | ||
export const semaphore = (max) => { | ||
const semaphore = (max) => { | ||
let counter = 0; | ||
@@ -83,13 +83,12 @@ const waiting = []; | ||
const { acquire, release } = semaphore(max); | ||
return (f) => { | ||
return async (...args) => { | ||
await acquire(); | ||
try { | ||
return await f(...args); | ||
} | ||
finally { | ||
release(); | ||
} | ||
}; | ||
// @ts-expect-error too complex | ||
return (f) => async (...args) => { | ||
await acquire(); | ||
try { | ||
return await f(...args); | ||
} | ||
finally { | ||
release(); | ||
} | ||
}; | ||
}; |
{ | ||
"name": "gamla", | ||
"version": "85.0.0", | ||
"version": "86.0.0", | ||
"description": "Functional programming with async and type safety", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -5,6 +5,2 @@ import { AsyncFunction } from "./typing.js"; | ||
export declare const rateLimit: <Function_1 extends AsyncFunction>(maxCalls: number, maxWeight: number, timeWindowMs: number, weight: (...args: Parameters<Function_1>) => number, f: Function_1) => Function_1; | ||
export declare const semaphore: (max: number) => { | ||
acquire: () => Promise<void>; | ||
release: () => void; | ||
}; | ||
export declare const throttle: <Function_1 extends AsyncFunction>(max: number) => (f: Function_1) => (...args: Parameters<Function_1>) => Promise<any>; | ||
export declare const throttle: (max: number) => <F extends AsyncFunction>(f: F) => F; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.throttle = exports.semaphore = exports.rateLimit = exports.sequentialized = exports.keepTryingEvery50ms = void 0; | ||
exports.throttle = exports.rateLimit = exports.sequentialized = exports.keepTryingEvery50ms = void 0; | ||
const time_js_1 = require("./time.js"); | ||
@@ -86,17 +86,15 @@ const withLock = (lock, unlock, f) => (async (...args) => { | ||
}; | ||
exports.semaphore = semaphore; | ||
const throttle = (max) => { | ||
const { acquire, release } = (0, exports.semaphore)(max); | ||
return (f) => { | ||
return async (...args) => { | ||
await acquire(); | ||
try { | ||
return await f(...args); | ||
} | ||
finally { | ||
release(); | ||
} | ||
}; | ||
const { acquire, release } = semaphore(max); | ||
// @ts-expect-error too complex | ||
return (f) => async (...args) => { | ||
await acquire(); | ||
try { | ||
return await f(...args); | ||
} | ||
finally { | ||
release(); | ||
} | ||
}; | ||
}; | ||
exports.throttle = throttle; |
231586
2447