This repository was archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
194 lines (173 loc) · 5.33 KB
/
script.js
File metadata and controls
194 lines (173 loc) · 5.33 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const CODE = `
function preparation(itemName, callback) {
let preparationTime;
switch (itemName) {
case "Coffee":
preparationTime = 4000;
break;
case "Chips":
preparationTime = 10000;
break;
case "Burger":
preparationTime = 10000;
break;
case "Juice":
preparationTime = 500;
break;
default:
console.log("We don't have that");
return;
}
setTimeout(() => {
console.log("Food prepped - " + itemName);
callback(itemName);
}, preparationTime);
}
function takeOrder(itemName) {
console.log(itemName + ", is that correct?");
submitOrder(itemName);
}
function submitOrder(itemName) {
console.log("taking " + itemName + " to Kitchen");
preparation(itemName, serveOrder);
}
function serveOrder(itemName) {
console.log(itemName);
}
takeOrder("Coffee")
`
document.getElementById("code").innerHTML = CODE.trim().split("\n").reduce((str, line, index) => {
str += `<span class="line${index + 1}"><span class="line-num">   ${String(index + 1).padStart(2, " ")}   </span>` + line + "</span>\n"
return str
}, "")
let currentHighlightedLine;
function removeHighlight() {
if (currentHighlightedLine) {
currentHighlightedLine.classList.remove("highlight")
currentHighlightedLine = null
}
}
function highlightLine(num) {
removeHighlight()
const line = document.getElementsByClassName(`line${num}`)[0]
line.classList.add("highlight")
currentHighlightedLine = line
}
const consoleEl = document.getElementById("console")
async function log(text) {
await addToCallstack(`console.log(...)`)
console.log(text);
let node = document.createElement("div")
node.classList.add("console-item")
let codeElement = document.createElement("code")
codeElement.append("> " + text)
node.append(codeElement)
consoleEl.appendChild(node)
await popCallstack()
}
const callstack = document.getElementById("callstack")
async function addToCallstack(code) {
let node = document.createElement("div")
node.classList.add("callstack-item")
let codeElement = document.createElement("code")
let preElement = document.createElement("pre")
preElement.classList.add("prettyprint")
preElement.append(code)
codeElement.append(preElement)
node.append(codeElement)
callstack.appendChild(node)
PR.prettyPrint()
await wait(2)
}
async function popCallstack() {
callstack.removeChild(callstack.lastChild)
await wait(1)
}
const eventQueue = document.getElementById("event-queue")
async function addToEventQueue(code) {
let node = document.createElement("div")
node.classList.add("event-queue-item")
let codeElement = document.createElement("code")
let preElement = document.createElement("pre")
preElement.append(code)
codeElement.append(preElement)
node.append(codeElement)
eventQueue.appendChild(node)
PR.prettyPrint()
await wait(0.5)
}
async function popEventQueue() {
eventQueue.removeChild(eventQueue.firstChild)
await wait(0.5)
}
async function wait(time) {
await new Promise(resolve => setTimeout(resolve, time * 1000))
}
async function runCode() {
async function preparation(itemName, callback) {
let preparationTime;
switch (itemName) {
case "Coffee":
preparationTime = 4000;
break;
case "Chips":
preparationTime = 10000;
break;
case "Burger":
preparationTime = 10000;
break;
case "Juice":
preparationTime = 500;
break;
default:
await log("We don't have that");
return;
}
highlightLine(21)
await addToCallstack(`setTimeout(...)`)
setTimeout(async () => {
await popEventQueue()
highlightLine(22)
await log("Food prepped - " + itemName);
highlightLine(23)
await addToCallstack(`callback(itemName)`)
await callback(itemName);
await popCallstack()
}, preparationTime);
await addToEventQueue(`${preparationTime}ms timeout`)
removeHighlight()
await popCallstack()
}
async function takeOrder(itemName) {
highlightLine(28)
await log(itemName + ", is that correct?");
highlightLine(29)
await addToCallstack(`submitOrder(itemName)`)
await submitOrder(itemName);
await popCallstack()
}
async function submitOrder(itemName) {
highlightLine(33)
await log("taking " + itemName + " to Kitchen");
highlightLine(34)
await addToCallstack(`preparation(itemName, serveOrder)`)
await preparation(itemName, serveOrder);
await popCallstack()
}
async function serveOrder(itemName) {
highlightLine(38)
await log(itemName);
removeHighlight()
}
highlightLine(41)
await addToCallstack(`takeOrder("Coffee")`)
await takeOrder("Coffee")
await popCallstack()
}
let debounce = false
document.getElementById("run").addEventListener("click", async () => {
if (debounce) return;
debounce = true
await runCode()
debounce = false
})