fix(Connection): do not assert that methods always have callbacks. (#2330)

In certain cases, all callbacks could be rejected before we get a
response from transport. This is easily reproducible with `slowMo`
option.

Fixes #563.
This commit is contained in:
Andrey Lushnikov 2018-04-07 17:59:22 -07:00 committed by GitHub
parent 294f33b75c
commit 57fa690724
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -102,15 +102,17 @@ class Connection extends EventEmitter {
await new Promise(f => setTimeout(f, this._delay)); await new Promise(f => setTimeout(f, this._delay));
debugProtocol('◀ RECV ' + message); debugProtocol('◀ RECV ' + message);
const object = JSON.parse(message); const object = JSON.parse(message);
if (object.id && this._callbacks.has(object.id)) { if (object.id) {
const callback = this._callbacks.get(object.id); const callback = this._callbacks.get(object.id);
this._callbacks.delete(object.id); // Callbacks could be all rejected if someone has called `.dispose()`.
if (object.error) if (callback) {
callback.reject(rewriteError(callback.error, `Protocol error (${callback.method}): ${object.error.message} ${object.error.data}`)); this._callbacks.delete(object.id);
else if (object.error)
callback.resolve(object.result); callback.reject(rewriteError(callback.error, `Protocol error (${callback.method}): ${object.error.message} ${object.error.data}`));
else
callback.resolve(object.result);
}
} else { } else {
console.assert(!object.id);
if (object.method === 'Target.receivedMessageFromTarget') { if (object.method === 'Target.receivedMessageFromTarget') {
const session = this._sessions.get(object.params.sessionId); const session = this._sessions.get(object.params.sessionId);
if (session) if (session)