@openhps/sphero
Version:
Open Hybrid Positioning System - Sphero component
54 lines • 1.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Queue = void 0;
class Queue {
constructor(queueListener) {
this.commandQueue = [];
this.waitingForResponseQueue = [];
this.queueListener = queueListener;
}
onCommandProcessed(payloadReceived) {
const lastCommand = this.waitingForResponseQueue.find(command => this.queueListener.match(command.payload, payloadReceived));
if (lastCommand) {
this.removeFromWaiting(lastCommand);
lastCommand.success(payloadReceived);
}
else {
console.log('PACKET RECEIVED BUT NOT EXECUTING', payloadReceived);
}
}
queue(payload) {
return new Promise((success, reject) => {
this.commandQueue.push({
payload,
reject,
success
});
this.processCommand();
});
}
processCommand() {
const command = this.commandQueue.shift();
if (command) {
this.queueListener.onExecute(command.payload);
this.waitingForResponseQueue.push(command);
command.timeout = setTimeout(() => this.onCommandTimedout(command), 5000);
}
}
removeFromWaiting(command) {
const index = this.waitingForResponseQueue.indexOf(command);
if (index >= 0) {
this.waitingForResponseQueue.splice(index, 1);
clearTimeout(command.timeout);
}
}
onCommandTimedout(command) {
this.handleQueueError('Command Timedout', command);
this.removeFromWaiting(command);
}
handleQueueError(error, command) {
command.reject(error);
}
}
exports.Queue = Queue;
//# sourceMappingURL=queue.js.map