helene
Version:
Real-time Web Apps for Node.js
32 lines • 1.23 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.onAllThrottled = exports.waitForAll = exports.onceAll = void 0;
const throttle_1 = __importDefault(require("lodash/throttle"));
const onceAll = async (emitter, events) => {
const promises = events.map(event => new Promise(resolve => {
emitter.once(event, () => {
resolve();
});
}));
await Promise.all(promises);
};
exports.onceAll = onceAll;
const waitForAll = async (emitter, events, timeout = 30000) => {
await Promise.all(events.map(event => emitter.waitFor(event, timeout)));
};
exports.waitForAll = waitForAll;
const onAllThrottled = (emitter, events, callback, throttleMs = 1000, throttleOptions) => {
const throttled = (0, throttle_1.default)(callback, throttleMs, throttleOptions);
events.forEach(event => emitter.on(event, throttled));
/**
* Cleanup function
*/
return () => {
events.forEach(event => emitter.off(event, throttled));
};
};
exports.onAllThrottled = onAllThrottled;
//# sourceMappingURL=events.js.map