Skip to content

Instantly share code, notes, and snippets.

@sachinsmc
Last active October 28, 2021 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sachinsmc/06f6c60d81db51975bbcdc59a2f71c3f to your computer and use it in GitHub Desktop.
Save sachinsmc/06f6c60d81db51975bbcdc59a2f71c3f to your computer and use it in GitHub Desktop.
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()
@Amzul
Copy link

Amzul commented Oct 28, 2021

Umm
This is much better then setInterval.
We still missing here the number of topping chefs and the rest of the crew... and it will be best to have a single point class that handle the flow (instead having each chef passing to the next chef, each chef should only mention that he is done and ready for new task).

@sachinsmc
Copy link
Author

I will add rest of crew, create better report and update you.

@sachinsmc
Copy link
Author

========= Report =========

Started 1 orders processing at : Thu Oct 28 2021 23:50:13 GMT+0400 (Gulf Standard Time)
Served all 1 orders at : Thu Oct 28 2021 23:50:39 GMT+0400 (Gulf Standard Time)
Order processing took : 26.026s




========= Report for each order =========

{
  '1': [
    'Started At : Thu Oct 28 2021 23:50:13 GMT+0400 (Gulf Standard Time)',
    'Completed At : Thu Oct 28 2021 23:50:39 GMT+0400 (Gulf Standard Time)',
    'Took more than 26s'
  ]
}

@Amzul Your thoughts on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment