Skip to content

Commit

Permalink
Implemented an event
Browse files Browse the repository at this point in the history
  • Loading branch information
motin committed Apr 5, 2018
1 parent c90397b commit ac0f4f9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
3 changes: 3 additions & 0 deletions extension/background.js
@@ -1,3 +1,6 @@
browser.hello.onSomething.addListener(param1 => {
console.log(`Something happened: ${param1}`);
});

browser.hello.hello().then(
message => console.log(`hello sez: "${message}"`)
Expand Down
26 changes: 25 additions & 1 deletion extension/hello.js
@@ -1,12 +1,36 @@
ChromeUtils.import("resource://gre/modules/ExtensionCommon.jsm");
ChromeUtils.import("resource://gre/modules/ExtensionUtils.jsm");

const { EventManager } = ExtensionCommon;
const { EventEmitter } = ExtensionUtils;

class HelloEventEmitter extends EventEmitter {
emitHelloEvent() {
console.log("Emitting hello-event from HelloEventEmitter");
this.emit("hello-event");
}
}

this.hello = class extends ExtensionAPI {
getAPI(context) {
const helloEventEmitter = new HelloEventEmitter();
return {
hello: {
async hello() {
helloEventEmitter.emitHelloEvent();
return "Hello, world!";
}
}
},
onSomething: new EventManager(context, "hello.onSomething", fire => {
const callback = value => {
console.log("Firing hello-event from hello experiments API");
fire.async(value);
};
helloEventEmitter.on("hello-event", callback);
return () => {
helloEventEmitter.off("hello-event", callback);
};
}).api()
};
}
}
13 changes: 13 additions & 0 deletions extension/schema.json
Expand Up @@ -10,6 +10,19 @@
"async": true,
"parameters": []
}
],
"events": [
{
"name": "onSomething",

This comment has been minimized.

Copy link
@rpl

rpl Apr 5, 2018

The events need to have a "type" property set to "function" (e.g. as the browserAction.onClicked API schema is doing here: https://searchfox.org/mozilla-central/rev/f860c2bf00576959b07e2d9619c7b5088b0005be/browser/components/extensions/schemas/browser_action.json#451)

This comment has been minimized.

Copy link
@motin

motin Apr 5, 2018

Author Owner

It comes further, thanks! I had already tried with type set to "event" but did not try "function".
New error:

WebExtensions: Emitting hello-event from HelloEventEmitter hello.js:9
TypeError: handler is undefined[Learn More] ExtensionParent.jsm:890:1
hello sez: "Hello, world!" background.js:6:14 

This comment has been minimized.

Copy link
@rpl

rpl Apr 5, 2018

The onSomething property defined at line 24 of hello.js is supposed to be inside the returned hello namespace (same level of the hello methods), right now it is at the same level of the hello namespace and so the webextensions internals are getting undefined when they search for the browser.hello.onSomething implementation (which happens lazily when the extension call browser.hello.onSomething.addListener for the first time)

This comment has been minimized.

Copy link
@motin

motin Apr 5, 2018

Author Owner

Thanks, worked like a charm after b07ab4d :)

"description": "Description of the event",
"parameters": [
{
"name": "param1",
"description": "Description of the first callback parameter",
"type": "number"
}
]
}
]
}
]

0 comments on commit ac0f4f9

Please sign in to comment.