@stackbit/cms-contentful
Version:
Stackbit Contentful CMS Interface
56 lines • 1.88 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LazyPoller = void 0;
class LazyPoller {
constructor({ notificationCallback, sleepTimeoutMs = 30 * 60 * 1000, pollingIntervalMs = 1000, logger }) {
this.notificationCallback = notificationCallback;
this.sleepTimeoutMs = sleepTimeoutMs;
this.pollingIntervalMs = pollingIntervalMs;
this.logger = logger;
this.sleepTimeout = null;
this.pollTimeout = null;
this.isSleeping = true;
this.sleep = this.sleep.bind(this);
this.callPoll = this.callPoll.bind(this);
}
resetSleepTimer() {
if (this.sleepTimeout) {
clearTimeout(this.sleepTimeout);
}
this.sleepTimeout = setTimeout(this.sleep, this.sleepTimeoutMs);
if (this.isSleeping) {
this.logger.debug('start polling');
this.isSleeping = false;
this.setPollTimeout();
}
}
sleep() {
this.logger.debug('sleep');
if (this.sleepTimeout) {
clearTimeout(this.sleepTimeout);
this.sleepTimeout = null;
}
this.isSleeping = true;
if (this.pollTimeout) {
clearTimeout(this.pollTimeout);
this.pollTimeout = null;
}
}
setPollTimeout() {
this.pollTimeout = setTimeout(this.callPoll, this.pollingIntervalMs);
}
async callPoll() {
this.pollTimeout = null;
return Promise.resolve(this.poll(this.notificationCallback))
.catch((error) => {
this.logger.error('error in pollCallback', { error: error.message });
})
.finally(() => {
if (!this.isSleeping) {
this.setPollTimeout();
}
});
}
}
exports.LazyPoller = LazyPoller;
//# sourceMappingURL=lazy-poller.js.map