audiobookshelf/server/libs/watcher/watcher_locks_resolver.js

45 lines
3.1 KiB
JavaScript
Raw Normal View History

"use strict";
/* WATCHER LOCKS RESOLVER */
Object.defineProperty(exports, "__esModule", { value: true });
// Registering a single interval scales much better than registering N timeouts
// Timeouts are respected within the interval margin
const WatcherLocksResolver = {
/* VARIABLES */
interval: 100,
intervalId: undefined,
fns: new Map(),
/* LIFECYCLE */
init: () => {
if (WatcherLocksResolver.intervalId)
return;
WatcherLocksResolver.intervalId = setInterval(WatcherLocksResolver.resolve, WatcherLocksResolver.interval);
},
reset: () => {
if (!WatcherLocksResolver.intervalId)
return;
clearInterval(WatcherLocksResolver.intervalId);
delete WatcherLocksResolver.intervalId;
},
/* API */
add: (fn, timeout) => {
WatcherLocksResolver.fns.set(fn, Date.now() + timeout);
WatcherLocksResolver.init();
},
remove: (fn) => {
WatcherLocksResolver.fns.delete(fn);
},
resolve: () => {
if (!WatcherLocksResolver.fns.size)
return WatcherLocksResolver.reset();
const now = Date.now();
for (const [fn, timestamp] of WatcherLocksResolver.fns) {
if (timestamp >= now)
continue; // We should still wait some more for this
WatcherLocksResolver.remove(fn);
fn();
}
}
};
/* EXPORT */
exports.default = WatcherLocksResolver;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoid2F0Y2hlcl9sb2Nrc19yZXNvbHZlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy93YXRjaGVyX2xvY2tzX3Jlc29sdmVyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFDQSw0QkFBNEI7O0FBRTVCLCtFQUErRTtBQUMvRSxvREFBb0Q7QUFFcEQsTUFBTSxvQkFBb0IsR0FBRztJQUUzQixlQUFlO0lBRWYsUUFBUSxFQUFFLEdBQUc7SUFDYixVQUFVLEVBQUUsU0FBdUM7SUFDbkQsR0FBRyxFQUFFLElBQUksR0FBRyxFQUFxQjtJQUVqQyxlQUFlO0lBR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