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
195
196
197
198
199
200
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const protocol = require("devtools/shared/protocol");
const { promisesSpec } = require("devtools/shared/specs/promises");
const { expectState, ActorPool } = require("devtools/server/actors/common");
const { ObjectActor, createValueGrip } = require("devtools/server/actors/object");
const DevToolsUtils = require("devtools/shared/DevToolsUtils");
loader.lazyRequireGetter(this, "events", "sdk/event/core");
/**
* The Promises Actor provides support for getting the list of live promises and
* observing changes to their settlement state.
*/
var PromisesActor = protocol.ActorClassWithSpec(promisesSpec, {
/**
* @param conn DebuggerServerConnection.
* @param parent TabActor|RootActor
*/
initialize: function (conn, parent) {
protocol.Actor.prototype.initialize.call(this, conn);
this.conn = conn;
this.parent = parent;
this.state = "detached";
this._dbg = null;
this._gripDepth = 0;
this._navigationLifetimePool = null;
this._newPromises = null;
this._promisesSettled = null;
this.objectGrip = this.objectGrip.bind(this);
this._makePromiseEventHandler = this._makePromiseEventHandler.bind(this);
this._onWindowReady = this._onWindowReady.bind(this);
},
destroy: function () {
protocol.Actor.prototype.destroy.call(this, this.conn);
if (this.state === "attached") {
this.detach();
}
},
get dbg() {
if (!this._dbg) {
this._dbg = this.parent.makeDebugger();
}
return this._dbg;
},
/**
* Attach to the PromisesActor.
*/
attach: expectState("detached", function () {
this.dbg.addDebuggees();
this._navigationLifetimePool = this._createActorPool();
this.conn.addActorPool(this._navigationLifetimePool);
this._newPromises = [];
this._promisesSettled = [];
this.dbg.findScripts().forEach(s => {
this.parent.sources.createSourceActors(s.source);
});
this.dbg.onNewScript = s => {
this.parent.sources.createSourceActors(s.source);
};
events.on(this.parent, "window-ready", this._onWindowReady);
this.state = "attached";
}, "attaching to the PromisesActor"),
/**
* Detach from the PromisesActor upon Debugger closing.
*/
detach: expectState("attached", function () {
this.dbg.removeAllDebuggees();
this.dbg.enabled = false;
this._dbg = null;
this._newPromises = null;
this._promisesSettled = null;
if (this._navigationLifetimePool) {
this.conn.removeActorPool(this._navigationLifetimePool);
this._navigationLifetimePool = null;
}
events.off(this.parent, "window-ready", this._onWindowReady);
this.state = "detached";
}),
_createActorPool: function () {
let pool = new ActorPool(this.conn);
pool.objectActors = new WeakMap();
return pool;
},
/**
* Create an ObjectActor for the given Promise object.
*
* @param object promise
* The promise object
* @return object
* An ObjectActor object that wraps the given Promise object
*/
_createObjectActorForPromise: function (promise) {
if (this._navigationLifetimePool.objectActors.has(promise)) {
return this._navigationLifetimePool.objectActors.get(promise);
}
let actor = new ObjectActor(promise, {
getGripDepth: () => this._gripDepth,
incrementGripDepth: () => this._gripDepth++,
decrementGripDepth: () => this._gripDepth--,
createValueGrip: v =>
createValueGrip(v, this._navigationLifetimePool, this.objectGrip),
sources: () => this.parent.sources,
createEnvironmentActor: () => DevToolsUtils.reportException(
"PromisesActor", Error("createEnvironmentActor not yet implemented")),
getGlobalDebugObject: () => DevToolsUtils.reportException(
"PromisesActor", Error("getGlobalDebugObject not yet implemented")),
});
this._navigationLifetimePool.addActor(actor);
this._navigationLifetimePool.objectActors.set(promise, actor);
return actor;
},
/**
* Get a grip for the given Promise object.
*
* @param object value
* The Promise object
* @return object
* The grip for the given Promise object
*/
objectGrip: function (value) {
return this._createObjectActorForPromise(value).grip();
},
/**
* Get a list of ObjectActors for all live Promise Objects.
*/
listPromises: function () {
let promises = this.dbg.findObjects({ class: "Promise" });
this.dbg.onNewPromise = this._makePromiseEventHandler(this._newPromises,
"new-promises");
this.dbg.onPromiseSettled = this._makePromiseEventHandler(
this._promisesSettled, "promises-settled");
return promises.map(p => this._createObjectActorForPromise(p));
},
/**
* Creates an event handler for onNewPromise that will add the new
* Promise ObjectActor to the array and schedule it to be emitted as a
* batch for the provided event.
*
* @param array array
* The list of Promise ObjectActors to emit
* @param string eventName
* The event name
*/
_makePromiseEventHandler: function (array, eventName) {
return promise => {
let actor = this._createObjectActorForPromise(promise);
let needsScheduling = array.length == 0;
array.push(actor);
if (needsScheduling) {
DevToolsUtils.executeSoon(() => {
events.emit(this, eventName, array.splice(0, array.length));
});
}
};
},
_onWindowReady: expectState("attached", function ({ isTopLevel }) {
if (!isTopLevel) {
return;
}
this._navigationLifetimePool.cleanup();
this.dbg.removeAllDebuggees();
this.dbg.addDebuggees();
})
});
exports.PromisesActor = PromisesActor;
|