-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallStack.js
60 lines (56 loc) · 1.58 KB
/
callStack.js
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
import { Stack } from './dataStructure.js';
class CallStack extends Stack {
WebAPIThreadPool;
eventLoop;
constructor() {
super();
}
push(context) {
console.log(`Push context! => ${context.name}`);
super.push(context);
this.run();
}
pop() {
console.log(`Pop context! => ${this.top.data.name}`);
super.pop();
if (this.isEmpty() && !this.eventLoop.isLooping) {
console.log('callstack is empty. run event loop');
this.eventLoop.runLoop();
}
}
run() {
const { data } = this.top;
console.log(`Run context! => ${data.name}`);
data.childFunc?.forEach(context => this.push(context));
if (data.callback)
switch (data.type) {
case 'micro':
console.log(
`Push micro task to micro task queue! => ${data.callback.name}`,
);
this.eventLoop.microtaskQueue.enqueue(data);
break;
case 'macro':
console.log(`Push setTimeout to web API Thread! => ${data.name}`);
this.WebAPIThreadPool.webAPITaskEnQueue(data);
break;
case 'raf':
console.log(
`Push raf task to raf task queue! => ${data.callback.name}`,
);
this.eventLoop.rafQueue.enqueue(data.callback);
break;
default:
break;
}
if (data.resolve)
this.eventLoop.microtaskQueue.forEach(node => {
if (node.data.name === data.resolve) {
node.data.state = 'fullfilled';
console.log(`${node.data.name} has fullfilled!`);
}
});
this.pop();
}
}
export default CallStack;