node-hue-api
Version:
Philips Hue API Library for Node.js
15 lines (14 loc) • 579 B
JavaScript
import Bottleneck from 'bottleneck';
import { HueApiRateLimitLogger } from './HueApiRateLimitLogger';
export class HueRateLimiter {
constructor(bridgeName, name, rateLimit) {
this.id = 0;
this.limiterName = `${bridgeName.trim().length > 0 ? bridgeName : 'node-hue-api'}:${name}`;
this.bottleneck = new Bottleneck(rateLimit || undefined);
HueApiRateLimitLogger.install(name, this.bottleneck);
}
schedule(fn) {
const jobId = this.id++;
return this.bottleneck.schedule({ id: `${this.limiterName}:${jobId}` }, fn);
}
}