Created
May 23, 2018 13:06
-
-
Save pyldin601/db1968f3db2e2b183ae038faf0928cef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as PromiseThrottle from 'promise-throttle'; | |
function makeThrottle(requestsPerSecond: number) { | |
const promiseThrottle = new PromiseThrottle({ requestsPerSecond }); | |
return (weight: number = 1) => (target: any, key: string, descriptor: PropertyDescriptor) => { | |
if (descriptor === undefined) { | |
descriptor = Object.getOwnPropertyDescriptor(target, key); | |
} | |
const original = descriptor.value; | |
descriptor.value = function(...args) { | |
return promiseThrottle.add(() => original.apply(this, args), weight); | |
}; | |
}; | |
} | |
export default makeThrottle; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment