-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (79 loc) · 2.98 KB
/
index.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
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
const {EventEmitter} = require('events');
const {Session} = require('inspector');
module.exports = new class extends EventEmitter {
run() {
this._session = new Session();
this._session.connect();
this._count = 0;
this._getPromises();
}
_getPromises() {
this._session.post('Runtime.evaluate', {expression: 'Promise.prototype'}, this._gotPromisePrototype.bind(this));
}
_gotPromisePrototype(err, params) {
if (err) return this._done(err);
this._objectId = params.result.objectId;
this._session.post('Runtime.queryObjects', {prototypeObjectId: this._objectId}, this._gotPromiseObjects.bind(this));
}
_releaseObjectAndContinue(nextCallback) {
let self = this;
this._session.post('Runtime.releaseObject', {objectId: this._objectId}, (err)=>{
if (err) return self._done(err);
nextCallback.call(self);
});
}
_gotPromiseObjects(err, params) {
this._releaseObjectAndContinue(()=>{
if (err) return this._done(err);
this._objectId = params.objects.objectId;
this._session.post('Runtime.getProperties', {objectId: this._objectId, ownProperties: true}, this._gotPromises.bind(this));
})
}
_gotPromises(err, params) {
this._releaseObjectAndContinue(()=>{
if (err) return this._done(err);
this._promises = params.result.filter(prop=>Number.isInteger(parseInt(prop.name))).map(prop=>prop.value.objectId);
this._anyPending = false;
this._getNextPromise();
})
}
_getNextPromise() {
if (this._promises.length == 0) {
if (this._anyPending) {
return process.nextTick(this._getPromises.bind(this));
} else {
return this._done();
}
}
this._objectId = this._promises.shift();
this._session.post('Runtime.getProperties', {objectId: this._objectId, ownProperties: true}, this._waitForPromise.bind(this));
}
_waitForPromise(err, params) {
if (err) return this._done(err);
let status = params.internalProperties.find(prop=>prop.name=='[[PromiseState]]').value;
if (status.value != 'pending') {
this._releaseObjectAndContinue(()=>{
this._getNextPromise();
});
return;
}
this._count ++;
this._anyPending = true;
this.emit('await', this._count);
this._session.post('Runtime.awaitPromise', {promiseObjectId: this._objectId}, this._awaitResult.bind(this));
}
_awaitResult(err) {
this._releaseObjectAndContinue(()=>{
if (err) return this._done(err);
this._getNextPromise();
});
}
_done(err) {
this._session.disconnect();
if (err) {
this.emit('error', err);
} else {
this.emit('done', this._count);
}
}
}