Index: lib/messaging.js |
=================================================================== |
--- a/lib/messaging.js |
+++ b/lib/messaging.js |
@@ -18,27 +18,26 @@ |
/** @module messaging */ |
"use strict"; |
const {EventEmitter} = require("events"); |
/** |
* Communication port wrapping ext.onMessage to receive messages. |
- * |
- * @constructor |
*/ |
-function Port() |
+class Port |
{ |
- this._eventEmitter = new EventEmitter(); |
- this._onMessage = this._onMessage.bind(this); |
- ext.onMessage.addListener(this._onMessage); |
-} |
+ constructor() |
+ { |
+ this._eventEmitter = new EventEmitter(); |
+ this._onMessage = this._onMessage.bind(this); |
+ ext.onMessage.addListener(this._onMessage); |
+ } |
-Port.prototype = { |
_onMessage(message, sender, sendResponse) |
{ |
let async = false; |
let callbacks = this._eventEmitter.listeners(message.type); |
for (let callback of callbacks) |
{ |
let response = callback(message, sender); |
@@ -57,17 +56,17 @@ |
} |
else if (typeof response != "undefined") |
{ |
sendResponse(response); |
} |
} |
return async; |
- }, |
+ } |
/** |
* Function to be called when a particular message is received. |
* |
* @callback Port~messageCallback |
* @param {object} message |
* @param {object} sender |
* @return The callback can return undefined (no response), |
@@ -80,37 +79,37 @@ |
* |
* The return value of the callback (if not undefined) is sent as response. |
* @param {string} name |
* @param {Port~messageCallback} callback |
*/ |
on(name, callback) |
{ |
this._eventEmitter.on(name, callback); |
- }, |
+ } |
/** |
* Removes a callback for the specified message. |
* |
* @param {string} name |
* @param {Port~messageCallback} callback |
*/ |
off(name, callback) |
{ |
this._eventEmitter.off(name, callback); |
- }, |
+ } |
/** |
* Disables the port and makes it stop listening to incoming messages. |
*/ |
disconnect() |
{ |
ext.onMessage.removeListener(this._onMessage); |
} |
-}; |
+} |
/** |
* The default port to receive messages. |
* |
* @type {Port} |
*/ |
exports.port = new Port(); |