rxpoweredup
Version:
A Typescript RxJS-based library for controlling LEGO Powered UP hubs & peripherals.
50 lines (49 loc) • 1.56 kB
JavaScript
import { Subscription } from 'rxjs';
export class TaskVisitor {
feedbackStream;
logger;
commands = new Map();
subscription = new Subscription();
constructor(feedbackStream, logger, feedbackHandler) {
this.feedbackStream = feedbackStream;
this.logger = logger;
this.subscription.add(this.feedbackStream.subscribe((message) => feedbackHandler.handlePortOutputCommandFeedback(this.commands.get(message.portId) ?? [], message.feedback)));
}
visitTaskPortOutputCommand(task) {
const portCommands = this.commands.get(task.portId);
if (!portCommands) {
this.commands.set(task.portId, [task]);
}
else {
portCommands.push(task);
}
task.result.subscribe({
complete: () => this.removePortOutputCommand(task),
error: () => this.removePortOutputCommand(task),
});
}
visitTaskWithResponse() {
return void 0;
}
visitTaskWithoutResponse() {
return void 0;
}
dispose() {
this.subscription.unsubscribe();
this.logger.debug('Task visitor disposed');
}
removePortOutputCommand(task) {
const portCommands = this.commands.get(task.portId);
if (!portCommands) {
return;
}
if (portCommands.length === 1) {
this.commands.delete(task.portId);
return;
}
const index = portCommands.indexOf(task);
if (index >= 0) {
portCommands.splice(index, 1);
}
}
}