@openevstack/ocpp-rpc
Version:
⚡ A lightweight, production-ready RPC server built with Express and WebSocket for handling OCPP-based EV charger communication. Part of the OpenEVStack ecosystem.
40 lines (39 loc) • 986 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
class Queue {
constructor() {
this._pending = 0;
this._concurrency = Infinity;
this._queue = [];
}
setConcurrency(concurrency) {
this._concurrency = concurrency;
this._next();
}
push(fn) {
return new Promise((resolve, reject) => {
this._queue.push({ fn, resolve, reject });
this._next();
});
}
async _next() {
if (this._pending >= this._concurrency)
return;
const job = this._queue.shift();
if (!job)
return;
this._pending++;
try {
const result = await job.fn();
job.resolve(result);
}
catch (error) {
job.reject(error);
}
finally {
this._pending--;
this._next(); // continue processing remaining jobs
}
}
}
exports.default = Queue;