function* newConnectedDebuggerClient(opts) { var transport = DebuggerServer.connectPipe(); var client = new DebuggerClient(transport); yield promiseInvoke(client, client.connect); var root = yield promiseInvoke(client, client.listTabs); return { client: client, root: root, transport: transport }; }
/** * Registers new backend tab actor. * * @param {DebuggerClient} client RDP client object (toolbox.target.client) * @param {Object} options Configuration object with the following options: * * - moduleUrl {String}: URL of the module that contains actor implementation. * - prefix {String}: prefix of the actor. * - actorClass {ActorClass}: Constructor object for the actor. * - frontClass {FrontClass}: Constructor object for the front part * of the registered actor. * * @returns {Promise} A promise that is resolved when the actor is registered. * The resolved value has two properties: * * - registrar {ActorActor}: A handle to the registered actor that allows * unregistration. * - form {Object}: The JSON actor form provided by the server. */ function registerTabActor(client, options) { let moduleUrl = options.moduleUrl; // Since client.listTabs doesn't use promises we need to // 'promisify' it using 'promiseInvoke' helper method. // This helps us to chain all promises and catch errors. return promiseInvoke(client, client.listTabs).then(response => { let config = { prefix: options.prefix, constructor: options.actorClass, type: { tab: true }, }; // Register the custom actor on the backend. let registry = ActorRegistryFront(client, response); return registry.registerActor(moduleUrl, config).then(registrar => { return client.getTab().then(response => { return { registrar: registrar, form: response.tab }; }); }); }); }
/** * Create a promise that is resolved with the server's response to the client's * Remote Debugger Protocol request. If a response with the `error` property is * received, the promise is rejected. Any extra arguments passed in are * forwarded to the method invocation. * * See `setBreakpoint` below, for example usage. * * @param DebuggerClient/ThreadClient/SourceClient/etc client * @param Function method * @param any args * @returns Promise */ function rdpRequest(client, method, ...args) { return promiseInvoke(client, method, ...args) .then(response => { const { error, message } = response; if (error) { throw new Error(error + ": " + message); } return response; }); }
function rdpInvoke(aClient, aMethod, ...args) { return promiseInvoke(aClient, aMethod, ...args) .then((packet) => { let { error, message } = packet; if (error) { throw new Error(error + ": " + message); } return packet; }); }
return Task.spawn(function* () { function func(a, b, expectedThis, callback) { "use strict"; do_check_eq(a, "foo"); do_check_eq(b, "bar"); do_check_eq(this, expectedThis); callback(a + b); } // Test call. let callResult = yield promiseCall(func, "foo", "bar", undefined); do_check_eq(callResult, "foobar"); // Test invoke. let obj = { method: func }; let invokeResult = yield promiseInvoke(obj, obj.method, "foo", "bar", obj); do_check_eq(invokeResult, "foobar"); // Test passing multiple values to the callback. function multipleResults(callback) { callback("foo", "bar"); } let results = yield promiseCall(multipleResults); do_check_eq(results.length, 2); do_check_eq(results[0], "foo"); do_check_eq(results[1], "bar"); // Test throwing from the function. function thrower() { throw "boom"; } yield promiseCall(thrower).then(null, error => { do_check_eq(error, "boom"); }); });