OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2016 Eyeo GmbH |
| 4 * |
| 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 |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 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/>. |
| 16 */ |
| 17 |
| 18 /** @module messaging */ |
| 19 |
| 20 "use strict"; |
| 21 |
| 22 /** |
| 23 * Communication port wrapping ext.onMessage to receive messages. |
| 24 * |
| 25 * @constructor |
| 26 */ |
| 27 function Port() |
| 28 { |
| 29 this._callbacks = Object.create(null); |
| 30 ext.onMessage.addListener(this._onMessage.bind(this)); |
| 31 }; |
| 32 |
| 33 Port.prototype = { |
| 34 _onMessage: function(message, sender, sendResponse) |
| 35 { |
| 36 let async = false; |
| 37 let callbacks = this._callbacks[message.type]; |
| 38 |
| 39 if (callbacks) |
| 40 { |
| 41 for (let callback of callbacks) |
| 42 { |
| 43 let response = callback(message, sender); |
| 44 |
| 45 if (response && typeof response.then == "function") |
| 46 { |
| 47 response.then( |
| 48 sendResponse, |
| 49 reason => { |
| 50 console.error(reason); |
| 51 sendResponse(undefined); |
| 52 } |
| 53 ); |
| 54 async = true; |
| 55 } |
| 56 else if (typeof response != "undefined") |
| 57 { |
| 58 sendResponse(response); |
| 59 } |
| 60 } |
| 61 } |
| 62 |
| 63 return async; |
| 64 }, |
| 65 |
| 66 /** |
| 67 * Function to be called when a particular message is received. |
| 68 * |
| 69 * @callback Port~messageCallback |
| 70 * @param {object} message |
| 71 * @param {object} sender |
| 72 * @return The callback can return undefined (no response), |
| 73 * a value (response to be sent to sender immediately) |
| 74 * or a promise (asynchronous response). |
| 75 */ |
| 76 |
| 77 /** |
| 78 * Adds a callback for the specified message. |
| 79 * |
| 80 * The return value of the callback (if not undefined) is sent as response. |
| 81 * @param {string} name |
| 82 * @param {Port~messageCallback} callback |
| 83 */ |
| 84 on: function(name, callback) |
| 85 { |
| 86 if (name in this._callbacks) |
| 87 this._callbacks[name].push(callback); |
| 88 else |
| 89 this._callbacks[name] = [callback]; |
| 90 }, |
| 91 |
| 92 /** |
| 93 * Removes a callback for the specified message. |
| 94 * |
| 95 * @param {string} name |
| 96 * @param {Port~messageCallback} callback |
| 97 */ |
| 98 off: function(name, callback) |
| 99 { |
| 100 let callbacks = this._callbacks[name]; |
| 101 if (callbacks) |
| 102 { |
| 103 let idx = callbacks.indexOf(callback); |
| 104 if (idx != -1) |
| 105 callbacks.splice(idx, 1); |
| 106 } |
| 107 } |
| 108 }; |
| 109 |
| 110 /** |
| 111 * The default port to receive messages. |
| 112 * |
| 113 * @type {Port} |
| 114 */ |
| 115 exports.port = new Port(); |
OLD | NEW |