Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/messaging.js

Issue 29723558: Issue 6482 - Use ES6 classes (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Created March 15, 2018, 7:46 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« lib/filterValidation.js ('K') | « lib/filterValidation.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 /** @module messaging */ 18 /** @module messaging */
19 19
20 "use strict"; 20 "use strict";
21 21
22 const {EventEmitter} = require("events"); 22 const {EventEmitter} = require("events");
23 23
24 /** 24 /**
25 * Communication port wrapping ext.onMessage to receive messages. 25 * Communication port wrapping ext.onMessage to receive messages.
26 *
27 * @constructor
28 */ 26 */
29 function Port() 27 class Port
30 { 28 {
31 this._eventEmitter = new EventEmitter(); 29 constructor()
32 this._onMessage = this._onMessage.bind(this); 30 {
33 ext.onMessage.addListener(this._onMessage); 31 this._eventEmitter = new EventEmitter();
34 } 32 this._onMessage = this._onMessage.bind(this);
33 ext.onMessage.addListener(this._onMessage);
34 }
35 35
36 Port.prototype = {
37 _onMessage(message, sender, sendResponse) 36 _onMessage(message, sender, sendResponse)
38 { 37 {
39 let async = false; 38 let async = false;
40 let callbacks = this._eventEmitter.listeners(message.type); 39 let callbacks = this._eventEmitter.listeners(message.type);
41 40
42 for (let callback of callbacks) 41 for (let callback of callbacks)
43 { 42 {
44 let response = callback(message, sender); 43 let response = callback(message, sender);
45 44
46 if (response && typeof response.then == "function") 45 if (response && typeof response.then == "function")
47 { 46 {
48 response.then( 47 response.then(
49 sendResponse, 48 sendResponse,
50 reason => 49 reason =>
51 { 50 {
52 console.error(reason); 51 console.error(reason);
53 sendResponse(undefined); 52 sendResponse(undefined);
54 } 53 }
55 ); 54 );
56 async = true; 55 async = true;
57 } 56 }
58 else if (typeof response != "undefined") 57 else if (typeof response != "undefined")
59 { 58 {
60 sendResponse(response); 59 sendResponse(response);
61 } 60 }
62 } 61 }
63 62
64 return async; 63 return async;
65 }, 64 }
66 65
67 /** 66 /**
68 * Function to be called when a particular message is received. 67 * Function to be called when a particular message is received.
69 * 68 *
70 * @callback Port~messageCallback 69 * @callback Port~messageCallback
71 * @param {object} message 70 * @param {object} message
72 * @param {object} sender 71 * @param {object} sender
73 * @return The callback can return undefined (no response), 72 * @return The callback can return undefined (no response),
74 * a value (response to be sent to sender immediately) 73 * a value (response to be sent to sender immediately)
75 * or a promise (asynchronous response). 74 * or a promise (asynchronous response).
76 */ 75 */
77 76
78 /** 77 /**
79 * Adds a callback for the specified message. 78 * Adds a callback for the specified message.
80 * 79 *
81 * The return value of the callback (if not undefined) is sent as response. 80 * The return value of the callback (if not undefined) is sent as response.
82 * @param {string} name 81 * @param {string} name
83 * @param {Port~messageCallback} callback 82 * @param {Port~messageCallback} callback
84 */ 83 */
85 on(name, callback) 84 on(name, callback)
86 { 85 {
87 this._eventEmitter.on(name, callback); 86 this._eventEmitter.on(name, callback);
88 }, 87 }
89 88
90 /** 89 /**
91 * Removes a callback for the specified message. 90 * Removes a callback for the specified message.
92 * 91 *
93 * @param {string} name 92 * @param {string} name
94 * @param {Port~messageCallback} callback 93 * @param {Port~messageCallback} callback
95 */ 94 */
96 off(name, callback) 95 off(name, callback)
97 { 96 {
98 this._eventEmitter.off(name, callback); 97 this._eventEmitter.off(name, callback);
99 }, 98 }
100 99
101 /** 100 /**
102 * Disables the port and makes it stop listening to incoming messages. 101 * Disables the port and makes it stop listening to incoming messages.
103 */ 102 */
104 disconnect() 103 disconnect()
105 { 104 {
106 ext.onMessage.removeListener(this._onMessage); 105 ext.onMessage.removeListener(this._onMessage);
107 } 106 }
108 }; 107 }
109 108
110 /** 109 /**
111 * The default port to receive messages. 110 * The default port to receive messages.
112 * 111 *
113 * @type {Port} 112 * @type {Port}
114 */ 113 */
115 exports.port = new Port(); 114 exports.port = new Port();
116 115
117 /** 116 /**
118 * Creates a new port that is disconnected when the given window is unloaded. 117 * Creates a new port that is disconnected when the given window is unloaded.
119 * 118 *
120 * @param {Window} window 119 * @param {Window} window
121 * @return {Port} 120 * @return {Port}
122 */ 121 */
123 exports.getPort = function(window) 122 exports.getPort = function(window)
124 { 123 {
125 let port = new Port(); 124 let port = new Port();
126 window.addEventListener("unload", () => 125 window.addEventListener("unload", () =>
127 { 126 {
128 port.disconnect(); 127 port.disconnect();
129 }); 128 });
130 return port; 129 return port;
131 }; 130 };
132 131
OLDNEW
« lib/filterValidation.js ('K') | « lib/filterValidation.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld