UNPKG

@dcloudio/uni-debugger

Version:

uni-app debugger

92 lines (81 loc) 2.51 kB
// Copyright 2018 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. Audits2.ProtocolService = class extends Common.Object { constructor() { super(); /** @type {?Protocol.InspectorBackend.Connection} */ this._rawConnection = null; /** @type {?Services.ServiceManager.Service} */ this._backend = null; /** @type {?Promise} */ this._backendPromise = null; /** @type {?function(string)} */ this._status = null; } /** * @return {!Promise<undefined>} */ attach() { return InspectorMain.interceptMainConnection(this._dispatchProtocolMessage.bind(this)).then(rawConnection => { this._rawConnection = rawConnection; }); } /** * @param {string} auditURL * @param {!Array<string>} categoryIDs * @param {!Object} flags * @return {!Promise<!ReportRenderer.RunnerResult>} */ startLighthouse(auditURL, categoryIDs, flags) { return this._send('start', {url: auditURL, categoryIDs, flags}); } /** * @return {!Promise<!Object|undefined>} */ detach() { return Promise.resolve().then(() => this._send('stop')).then(() => this._backend.dispose()).then(() => { delete this._backend; delete this._backendPromise; return this._rawConnection.disconnect(); }); } /** * @param {function (string): undefined} callback */ registerStatusCallback(callback) { this._status = callback; } /** * @param {string} message */ _dispatchProtocolMessage(message) { this._send('dispatchProtocolMessage', {message: message}); } _initWorker() { this._backendPromise = Services.serviceManager.createAppService('audits2_worker', 'Audits2Service').then(backend => { if (this._backend) return; this._backend = backend; this._backend.on('statusUpdate', result => this._status(result.message)); this._backend.on('sendProtocolMessage', result => this._sendProtocolMessage(result.message)); }); } /** * @param {string} message */ _sendProtocolMessage(message) { this._rawConnection.sendMessage(message); } /** * @param {string} method * @param {!Object=} params * @return {!Promise<!ReportRenderer.RunnerResult>} */ _send(method, params) { if (!this._backendPromise) this._initWorker(); return this._backendPromise.then(_ => this._backend.send(method, params)); } };