Changeset View
Changeset View
Standalone View
Standalone View
extension/extension-utils.js
Show All 14 Lines | 1 | /* | |||
---|---|---|---|---|---|
15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
16 | */ | 16 | */ | ||
17 | 17 | | |||
18 | var port; | 18 | var port; | ||
19 | 19 | | |||
20 | var callbacks = {}; // TODO rename to "portCallbacks"? | 20 | var callbacks = {}; // TODO rename to "portCallbacks"? | ||
21 | var runtimeCallbacks = {}; | 21 | var runtimeCallbacks = {}; | ||
22 | 22 | | |||
23 | let currentMessageSerial = 0; | ||||
24 | let pendingMessageReplyResolvers = {}; | ||||
25 | | ||||
23 | var storage = (IS_FIREFOX ? chrome.storage.local : chrome.storage.sync); | 26 | var storage = (IS_FIREFOX ? chrome.storage.local : chrome.storage.sync); | ||
24 | 27 | | |||
25 | let firefoxVersionMatch = navigator.userAgent.match(/Firefox\/(\d+)/) | 28 | let firefoxVersionMatch = navigator.userAgent.match(/Firefox\/(\d+)/) | ||
26 | let firefoxVersion = firefoxVersionMatch ? Number(firefoxVersionMatch[1]) : NaN | 29 | let firefoxVersion = firefoxVersionMatch ? Number(firefoxVersionMatch[1]) : NaN | ||
27 | 30 | | |||
28 | // Callback is called with following arguments (in that order); | 31 | // Callback is called with following arguments (in that order); | ||
29 | // - The actual message data/payload | 32 | // - The actual message data/payload | ||
30 | // - The name of the action triggered | 33 | // - The name of the action triggered | ||
Show All 17 Lines | 50 | { | |||
48 | // why do we put stuff on root level here but otherwise have a "payload"? :( | 51 | // why do we put stuff on root level here but otherwise have a "payload"? :( | ||
49 | var message = payload || {} | 52 | var message = payload || {} | ||
50 | message.subsystem = subsystem; | 53 | message.subsystem = subsystem; | ||
51 | message.event = event; | 54 | message.event = event; | ||
52 | 55 | | |||
53 | port.postMessage(message); | 56 | port.postMessage(message); | ||
54 | } | 57 | } | ||
55 | 58 | | |||
59 | function sendPortMessageWithReply(subsystem, event, payload) | ||||
60 | { | ||||
61 | return new Promise((resolve, reject) => { | ||||
62 | let message = payload || {}; | ||||
63 | message.subsystem = subsystem; | ||||
64 | message.event = event; | ||||
65 | message.serial = ++currentMessageSerial; | ||||
fvogt: Add a manual wrap before INT32_MAX, just to be safe | |||||
66 | pendingMessageReplyResolvers[message.serial] = resolve; | ||||
67 | | ||||
68 | port.postMessage(message); | ||||
69 | }); | ||||
70 | } | ||||
71 | | ||||
56 | // Callback is called with following arguments (in that order); | 72 | // Callback is called with following arguments (in that order); | ||
57 | // - The actual message data/payload | 73 | // - The actual message data/payload | ||
58 | // - Information about the sender of the message (including tab and frameId) | 74 | // - Information about the sender of the message (including tab and frameId) | ||
59 | // - The name of the action triggered | 75 | // - The name of the action triggered | ||
60 | // Return a Promise from the callback if you wish to send a reply to the sender | 76 | // Return a Promise from the callback if you wish to send a reply to the sender | ||
61 | function addRuntimeCallback(subsystem, action, callback) | 77 | function addRuntimeCallback(subsystem, action, callback) | ||
62 | { | 78 | { | ||
63 | if (action.constructor === Array) { | 79 | if (action.constructor === Array) { | ||
▲ Show 20 Lines • Show All 66 Lines • Show Last 20 Lines |
Add a manual wrap before INT32_MAX, just to be safe