diff options
author | hackademix | 2018-08-21 23:54:04 +0200 |
---|---|---|
committer | hackademix | 2018-08-21 23:54:04 +0200 |
commit | 1de1db3c29d11f49e130d44db359d1187f6c24a0 (patch) | |
tree | a59d10d32eeb0dcf9a6dfbac893b68af6fcc16d3 /src/lib | |
parent | 91334fe94477fdbcb85fcf93d87e3c73294da106 (diff) | |
download | noscript-1de1db3c29d11f49e130d44db359d1187f6c24a0.tar.gz noscript-1de1db3c29d11f49e130d44db359d1187f6c24a0.tar.xz noscript-1de1db3c29d11f49e130d44db359d1187f6c24a0.zip |
Fixed possible surprises in background script message handling.
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/Messages.js | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/lib/Messages.js b/src/lib/Messages.js new file mode 100644 index 0000000..c87a8a8 --- /dev/null +++ b/src/lib/Messages.js @@ -0,0 +1,31 @@ +"use strict"; +{ + let handlers = new Set(); + + let dispatch = async (msg, sender) => { + let {action} = msg; + for (let h of handlers) { + let f = h[action]; + if (typeof f === "function") { + return await f(msg, sender); + } + } + }; + + var Messages = { + addHandler(handler) { + let originalSize = handlers.size; + handlers.add(handler); + if (originalSize === 0 && handlers.size === 1) { + browser.runtime.onMessage.addListener(dispatch); + } + }, + removeHandler(handler) { + let originalSize = handlers.size; + handlers.delete(handler); + if (originalSize === 1 && handlers.size === 0) { + browser.runtime.onMessage.remveListener(dispatch); + } + } + } +} |