import Machine from ‘./Machine.js’;
class Robot extends Machine {
constructor(name) {
this.name = name;
this.taskQueue = [];
}
speak = (message) => {
console.log(message);
}
introduce = () => {
this.speak(`Hello, my name is ${this.name}. I am your robot friend.`);
}
order = (task, ...args) => {
this.taskQueue.push({
taskName: task,
...(args && { taskInfo: args })
});
this.processTasks();
}
processTasks = async () => {
if (!this.taskQueue.length) return;
const { taskName, taskInfo = [] } = this.taskQueue.pop();
await this.processTask(taskName, ...taskInfo);
this.processTasks();
}
processTask = async (taskName, ...args) => {
const machineProto = Machine.prototype;
const task = machineProto[taskName];
if (typeof task === ‘function’) {
try {
const result = await task.apply(this, args);
return result ?? undefined;
} catch (e) {
console.error(’Error while processing task:’, e);
}
} else {
console.error(`Invalid task: ${taskName}`);
}
}
selfDestruct() {
Object.setPrototypeOf(this, null);
const keys = Object.keys(this);
for (const key of keys) {
delete this[key];
}
}
}
const robot = new Robot(’Kevin’);
robot.introduce(); // logged: “Hello, my name is Kevin. I am your robot friend.”
robot.order(’walk’, ’to’, ‘computer’);
robot.speak(’Whoa!’);
robot.speak(’What is this?’);
robot.speak(’This is awesome!’);
robot.speak(’Dang, this dude is putting in work!’);
robot.speak(’This guy is a machine!’);
robot.speak(’I mean, I\’m a machine, but he\’s the REAL machine!’); // TODO: robot seems really into the guy?
robot.order(’search’, ‘computer’, ‘new video’);
robot.speak(’Oh yeah! This is the one!’); // TODO: picked one with the same dude?
robot.speak(’My man!’);
robot.speak(’He is RED HOT in this one!’); // logged: ‘Wink ;)’ ???
robot.order(’leave’, ‘computer’); // logged: ’No way, man! I gotta see how this one ends! ;)’
robot.order(’leave’, ‘computer’);
robot.order(’leave’, ‘computer’);
robot.order(’leave’, ‘computer’);
robot.order(’leave’, ‘computer’); // logged: ‘Error while processing task: Chill out, baby! I'm almost done ;)’
robot.selfDestruct();
robot.speak(’fuck, I\’m gonna cummmmmmmmmmm ;)’);
robot.selfDestruct();
robot.selfDestruct();
robot.selfDestruct();
robot.selfDestruct();
while (true) robot.selfDestruct();
// Uncaught RangeError: Maximum call stack size exceeded
// ;)