using event emitter

node v14.20.1
version: 2.0.1
endpointsharetweet
const { EventEmitter } = require('events'); var input = { "orders": [ [ "Pepperoni", "Mushrooms", "Sausage" ] ] }; /** * Restaurant class is reponsible for processing orders, it extends eventEmitter */ class Restaurant extends EventEmitter { constructor(doughChefs, toppingChefs, isOvenFree, waiters, orders, report) { super(); this.doughChefs = doughChefs; this.toppingChefs = toppingChefs; this.isOvenFree = isOvenFree; this.waiters = waiters; this.orders = orders; this.servedOrders = 0; this.report = "\n========= Report ========= \n"; this.orderReport = "\n========= Report for each order ========= \n"; this.orderTimings = {}; this.currentOrder = 0; this.completed = 0; } /** * This methods handles order dough * @param {Order} order */ dough(order) { console.log('dough: ' + order + new Date()); this.currentOrder++ this.orderTimings[`${this.currentOrder}`] = ["Started At : " + new Date()] setTimeout(() => { this.noOfToppings = order.length this.doughChefs++ this.emit('topping', order); }, 7000) } /** * This methods handles order topping * @param {Order} order */ topping(order) { console.log('topping: ' + order + new Date()); setTimeout(() => { if (this.noOfToppings <= 0 || this.noOfToppings < 6) { this.emit('oven', order); } else { this.noOfToppings -= 6 this.emit('topping', order); } }, 4000); } /** * This methods handles order oven * @param {Order} order */ oven(order) { if (this.isOvenFree) { this.isOvenFree = false; console.log('oven: ' + order + new Date()); setTimeout(() => { this.emit('serve', order); }, 10000); } else { setTimeout(() => { this.emit('oven', order); }, 10000); } } /** * This methods handles order serving * @param {Order} order */ serve(order) { this.isOvenFree = true; if (this.Waiters > 0) { this.Waiters-- setTimeout(() => { this.Waiters++ this.servedOrders++ console.log('served: ' + order + new Date()); }, 5000); } else { this.Waiters-- setTimeout(() => { console.log('served: ' + order + new Date()) this.servedOrders++ this.areThereMoreOrders(this.currentOrder) }, 5000); } } areThereMoreOrders() { this.completed++ this.orderTimings[`${this.completed}`].push("Completed At : " + new Date()) this.orderTimings[`${this.completed}`].push('Took more than 26s') this.Waiters++ if (this.orders.length === this.servedOrders) { this.report += `\nServed all ${this.servedOrders} orders at : ${new Date()}` console.log(this.report) console.timeEnd("Order processing took ") console.log("\n \n") console.log(this.orderReport) console.log(this.orderTimings) } return } /** * This is starting point, it will process order */ processOrder() { console.time("Order processing took ") console.log(`\nStarting Processing ${this.orders.length} orders\n`) this.report += `\nStarted ${this.orders.length} orders processing at : ${new Date()}` for (let i = 0; i < this.orders.length; i++) { if (this.doughChefs > 0) { this.emit('dough', input.orders[i]); this.doughChefs-- } else { setTimeout(() => { this.emit('dough', input.orders[i]); this.doughChefs-- }, 7000) } } } } const restaurant = new Restaurant(2, 3, true, 2, input.orders); restaurant.on('dough', restaurant.dough); restaurant.on('topping', restaurant.topping); restaurant.on('oven', restaurant.oven); restaurant.on('serve', restaurant.serve); // Process order restaurant.processOrder()
Loading…

no comments

    sign in to comment