-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcooking.js
More file actions
163 lines (134 loc) Β· 5.13 KB
/
cooking.js
File metadata and controls
163 lines (134 loc) Β· 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
async function highlightStep(stepId, duration, description, color) {
let step = document.getElementById(stepId);
let currentTask = document.getElementById("currentTask");
step.classList.add("active");
step.style.backgroundColor = color;
step.style.color = "black";
currentTask.innerHTML = description;
await new Promise((resolve) => setTimeout(resolve, duration));
step.classList.remove("active");
}
function addMicrotask(taskDescription) {
let microtasksQueue = document.getElementById("microtasksQueue");
let taskElement = document.createElement("div");
taskElement.textContent = taskDescription;
taskElement.classList.add("microtask");
taskElement.style.backgroundColor = "#16C47F";
microtasksQueue.appendChild(taskElement);
setTimeout(() => {
microtasksQueue.removeChild(taskElement);
}, 5000);
}
async function addMacrotask(taskDescription, color, duration = 5000) {
return new Promise((resolve) => {
let macroTasksQueue = document.getElementById("macroTasksQueue");
let taskElement = document.createElement("div");
taskElement.textContent = taskDescription;
taskElement.classList.add("macrotask");
taskElement.style.backgroundColor = color;
macroTasksQueue.appendChild(taskElement);
setTimeout(() => {
macroTasksQueue.removeChild(taskElement);
resolve();
}, duration);
});
}
function addPendingPromise(taskDescription) {
let pendingPromises = document.getElementById("pendingPromises");
let taskElement = document.createElement("div");
taskElement.textContent = taskDescription;
taskElement.classList.add("pending-promise");
pendingPromises.appendChild(taskElement);
}
function addCallStack(taskDescription) {
let callStack = document.getElementById("callStack");
let taskElement = document.createElement("div");
taskElement.textContent = taskDescription;
taskElement.classList.add("callstack");
callStack.appendChild(taskElement);
setTimeout(() => {
callStack.removeChild(taskElement);
}, 5000);
}
async function startCooking() {
resetCookingProcess();
console.log("Starting cooking process - synchronous code executed first");
addCallStack("πͺ Initial Execution: Preparing ingredients.");
await highlightStep(
"initialExecution",
5000,
`πͺ **Preparing ingredients and preheating the oven**.<br>
<span>π
Chopping vegetables </span><br>
<span> Gathering pasta sheets </span>`,
"gold"
);
let boilingStep = document.getElementById("promiseExecutor");
boilingStep.classList.add("active");
boilingStep.style.backgroundColor = "#EB5353";
document.getElementById("currentTask").innerHTML = `
<span>π° Filling a pot and placing it on the stove.</span>
<br>
<span>π₯ Boiling water for pasta.</span> <br>
`;
console.log("Promise executor started - Water is boiling");
addPendingPromise(
"π₯ Waiting for water to boil (Promise Pending)",
"#36AE7C"
);
setTimeout(async () => {
console.log("Call stack execution - Cooking sauce while waiting for water");
addCallStack("π³ Cooking sauce");
await highlightStep(
"callStackExecution",
5000,
"π³ **Cooking sauce while waiting for water.** Example: SautΓ©ing onions, browning meat, adding tomato sauce.",
"#3DB2FF"
);
}, 3000); // Sauce starts after 3s
await new Promise((resolve) => setTimeout(resolve, 5000)); // Ensure delay before checking water
document.getElementById("pendingPromises").innerHTML = "";
let checkStep = document.getElementById("microtasksExecution");
checkStep.classList.add("active");
checkStep.style.backgroundColor = " #16C47F";
addMicrotask("β
Checking if water is boiling...");
await new Promise((resolve) => setTimeout(resolve, 3000));
addMicrotask("π Adding pasta to boiling water...");
await highlightStep(
"microtasksExecution",
5000,
"π **Microtasks Execution:** Checking boiling water & adding pasta.",
"#16C47F"
);
await highlightStep(
"taskExecution",
0,
"π½ **Task Execution:** Assembling and baking the lasagna.",
"#8B5DFF"
);
console.log("Starting macrotasks for baking...");
highlightStep(
"taskExecution",
10000,
"π½ **Task Execution Continues:** Baking and checking lasagna.",
"#8B5DFF"
);
await addMacrotask("β³ Timer set for baking...", "#8B5DFF");
await addMacrotask("πͺ Opening oven to check lasagna...", "#8B5DFF");
console.log("Lasagna is ready to serve!");
document.getElementById("currentTask").innerHTML =
"π **Lasagna is Ready!** Time to eat!";
alert("Lasagna is ready to serve!");
}
function resetCookingProcess() {
console.log("Resetting the cooking process...");
document.querySelectorAll(".step").forEach((step) => {
step.classList.remove("active");
step.style.backgroundColor = "";
});
document.getElementById("currentTask").innerHTML = "Waiting to start...";
document.getElementById("microtasksQueue").innerHTML = "";
document.getElementById("macroTasksQueue").innerHTML = "";
document.getElementById("pendingPromises").innerHTML = "";
document.getElementById("callStack").innerHTML = "";
console.log("Cooking process reset. Ready to start again!");
}