diff options
Diffstat (limited to 'background/storage_server.js')
-rw-r--r-- | background/storage_server.js | 95 |
1 files changed, 50 insertions, 45 deletions
diff --git a/background/storage_server.js b/background/storage_server.js index acdca27..d39898d 100644 --- a/background/storage_server.js +++ b/background/storage_server.js @@ -5,58 +5,63 @@ * Redistribution terms are gathered in the `copyright' file. */ -"use strict"; - -(() => { - const listen_for_connection = window.listen_for_connection; - const get_storage = window.get_storage; - const TYPE_PREFIX = window.TYPE_PREFIX; - const CONNECTION_TYPE = window.CONNECTION_TYPE; - - var storage; - - async function handle_remote_call(port, message) - { - let [call_id, func, args] = message; - - try { - let result = await Promise.resolve(storage[func](...args)); - port.postMessage({call_id, result}); - } catch (error) { - error = error + ''; - port.postMessage({call_id, error}); - } - } +/* + * IMPORTS_START + * IMPORT listen_for_connection + * IMPORT get_storage + * IMPORT TYPE_PREFIX + * IMPORT CONNECTION_TYPE + * IMPORTS_END + */ + +var storage; + +async function handle_remote_call(port, message) +{ + let [call_id, func, args] = message; - function remove_storage_listener(cb) { - storage.remove_change_listener(cb); + try { + let result = await Promise.resolve(storage[func](...args)); + port.postMessage({call_id, result}); + } catch (error) { + error = error + ''; + port.postMessage({call_id, error}); } +} - function new_connection(port) - { - console.log("new remote storage connection!"); +function remove_storage_listener(cb) +{ + storage.remove_change_listener(cb); +} - port.postMessage({ - [TYPE_PREFIX.SCRIPT] : storage.get_all(TYPE_PREFIX.SCRIPT), - [TYPE_PREFIX.BAG] : storage.get_all(TYPE_PREFIX.BAG), - [TYPE_PREFIX.PAGE] : storage.get_all(TYPE_PREFIX.PAGE) - }); +function new_connection(port) +{ + console.log("new remote storage connection!"); - let handle_change = change => port.postMessage(change); + port.postMessage({ + [TYPE_PREFIX.SCRIPT] : storage.get_all(TYPE_PREFIX.SCRIPT), + [TYPE_PREFIX.BAG] : storage.get_all(TYPE_PREFIX.BAG), + [TYPE_PREFIX.PAGE] : storage.get_all(TYPE_PREFIX.PAGE) + }); - storage.add_change_listener(handle_change); + let handle_change = change => port.postMessage(change); - port.onMessage.addListener(m => handle_remote_call(port, m)); - port.onDisconnect.addListener(() => - remove_storage_listener(handle_change)); - } + storage.add_change_listener(handle_change); - async function start() - { - storage = await get_storage(); + port.onMessage.addListener(m => handle_remote_call(port, m)); + port.onDisconnect.addListener(() => + remove_storage_listener(handle_change)); +} - listen_for_connection(CONNECTION_TYPE.REMOTE_STORAGE, new_connection); - } +async function start_storage_server() +{ + storage = await get_storage(); - window.start_storage_server = start; -})(); + listen_for_connection(CONNECTION_TYPE.REMOTE_STORAGE, new_connection); +} + +/* + * EXPORTS_START + * EXPORT start_storage_server + * EXPORTS_END + */ |