Revision 261548ff
Added by koszko about 2 years ago
| background/storage_server.js | ||
|---|---|---|
| 5 | 5 |
* Redistribution terms are gathered in the `copyright' file. |
| 6 | 6 |
*/ |
| 7 | 7 |
|
| 8 |
"use strict"; |
|
| 9 |
|
|
| 10 |
(() => {
|
|
| 11 |
const listen_for_connection = window.listen_for_connection; |
|
| 12 |
const get_storage = window.get_storage; |
|
| 13 |
const TYPE_PREFIX = window.TYPE_PREFIX; |
|
| 14 |
const CONNECTION_TYPE = window.CONNECTION_TYPE; |
|
| 15 |
|
|
| 16 |
var storage; |
|
| 17 |
|
|
| 18 |
async function handle_remote_call(port, message) |
|
| 19 |
{
|
|
| 20 |
let [call_id, func, args] = message; |
|
| 21 |
|
|
| 22 |
try {
|
|
| 23 |
let result = await Promise.resolve(storage[func](...args)); |
|
| 24 |
port.postMessage({call_id, result});
|
|
| 25 |
} catch (error) {
|
|
| 26 |
error = error + ''; |
|
| 27 |
port.postMessage({call_id, error});
|
|
| 28 |
} |
|
| 29 |
} |
|
| 8 |
/* |
|
| 9 |
* IMPORTS_START |
|
| 10 |
* IMPORT listen_for_connection |
|
| 11 |
* IMPORT get_storage |
|
| 12 |
* IMPORT TYPE_PREFIX |
|
| 13 |
* IMPORT CONNECTION_TYPE |
|
| 14 |
* IMPORTS_END |
|
| 15 |
*/ |
|
| 16 |
|
|
| 17 |
var storage; |
|
| 18 |
|
|
| 19 |
async function handle_remote_call(port, message) |
|
| 20 |
{
|
|
| 21 |
let [call_id, func, args] = message; |
|
| 30 | 22 |
|
| 31 |
function remove_storage_listener(cb) {
|
|
| 32 |
storage.remove_change_listener(cb); |
|
| 23 |
try {
|
|
| 24 |
let result = await Promise.resolve(storage[func](...args)); |
|
| 25 |
port.postMessage({call_id, result});
|
|
| 26 |
} catch (error) {
|
|
| 27 |
error = error + ''; |
|
| 28 |
port.postMessage({call_id, error});
|
|
| 33 | 29 |
} |
| 30 |
} |
|
| 34 | 31 |
|
| 35 |
function new_connection(port) |
|
| 36 |
{
|
|
| 37 |
console.log("new remote storage connection!");
|
|
| 32 |
function remove_storage_listener(cb) |
|
| 33 |
{
|
|
| 34 |
storage.remove_change_listener(cb); |
|
| 35 |
} |
|
| 38 | 36 |
|
| 39 |
port.postMessage({
|
|
| 40 |
[TYPE_PREFIX.SCRIPT] : storage.get_all(TYPE_PREFIX.SCRIPT), |
|
| 41 |
[TYPE_PREFIX.BAG] : storage.get_all(TYPE_PREFIX.BAG), |
|
| 42 |
[TYPE_PREFIX.PAGE] : storage.get_all(TYPE_PREFIX.PAGE) |
|
| 43 |
}); |
|
| 37 |
function new_connection(port) |
|
| 38 |
{
|
|
| 39 |
console.log("new remote storage connection!");
|
|
| 44 | 40 |
|
| 45 |
let handle_change = change => port.postMessage(change); |
|
| 41 |
port.postMessage({
|
|
| 42 |
[TYPE_PREFIX.SCRIPT] : storage.get_all(TYPE_PREFIX.SCRIPT), |
|
| 43 |
[TYPE_PREFIX.BAG] : storage.get_all(TYPE_PREFIX.BAG), |
|
| 44 |
[TYPE_PREFIX.PAGE] : storage.get_all(TYPE_PREFIX.PAGE) |
|
| 45 |
}); |
|
| 46 | 46 |
|
| 47 |
storage.add_change_listener(handle_change);
|
|
| 47 |
let handle_change = change => port.postMessage(change);
|
|
| 48 | 48 |
|
| 49 |
port.onMessage.addListener(m => handle_remote_call(port, m)); |
|
| 50 |
port.onDisconnect.addListener(() => |
|
| 51 |
remove_storage_listener(handle_change)); |
|
| 52 |
} |
|
| 49 |
storage.add_change_listener(handle_change); |
|
| 53 | 50 |
|
| 54 |
async function start() |
|
| 55 |
{
|
|
| 56 |
storage = await get_storage(); |
|
| 51 |
port.onMessage.addListener(m => handle_remote_call(port, m)); |
|
| 52 |
port.onDisconnect.addListener(() => |
|
| 53 |
remove_storage_listener(handle_change)); |
|
| 54 |
} |
|
| 57 | 55 |
|
| 58 |
listen_for_connection(CONNECTION_TYPE.REMOTE_STORAGE, new_connection); |
|
| 59 |
} |
|
| 56 |
async function start_storage_server() |
|
| 57 |
{
|
|
| 58 |
storage = await get_storage(); |
|
| 60 | 59 |
|
| 61 |
window.start_storage_server = start; |
|
| 62 |
})(); |
|
| 60 |
listen_for_connection(CONNECTION_TYPE.REMOTE_STORAGE, new_connection); |
|
| 61 |
} |
|
| 62 |
|
|
| 63 |
/* |
|
| 64 |
* EXPORTS_START |
|
| 65 |
* EXPORT start_storage_server |
|
| 66 |
* EXPORTS_END |
|
| 67 |
*/ |
|
Also available in: Unified diff
emply an sh-based build system; make some changes to blocking