1
|
/**
|
2
|
* part of Hachette
|
3
|
* Serving of storage data corresponding to requested urls (server side).
|
4
|
*
|
5
|
* Copyright (C) 2021 Wojtek Kosior
|
6
|
* Redistribution terms are gathered in the `copyright' file.
|
7
|
*/
|
8
|
|
9
|
/*
|
10
|
* IMPORTS_START
|
11
|
* IMPORT listen_for_connection
|
12
|
* IMPORT get_storage
|
13
|
* IMPORT get_query_all
|
14
|
* IMPORT TYPE_PREFIX
|
15
|
* IMPORT CONNECTION_TYPE
|
16
|
* IMPORT url_matches
|
17
|
* IMPORTS_END
|
18
|
*/
|
19
|
|
20
|
var storage;
|
21
|
var query_all;
|
22
|
|
23
|
function handle_change(connection_data, change)
|
24
|
{
|
25
|
if (change.prefix !== TYPE_PREFIX.PAGE)
|
26
|
return;
|
27
|
|
28
|
connection_data.port.postMessage(["change", change]);
|
29
|
}
|
30
|
|
31
|
async function handle_subscription(connection_data, message)
|
32
|
{
|
33
|
const [action, url] = message;
|
34
|
if (action === "unsubscribe") {
|
35
|
connection_data.subscribed.delete(url);
|
36
|
return;
|
37
|
}
|
38
|
|
39
|
connection_data.subscribed.add(url);
|
40
|
connection_data.port.postMessage(["new_url", query_all(url)]);
|
41
|
}
|
42
|
|
43
|
function remove_storage_listener(cb)
|
44
|
{
|
45
|
storage.remove_change_listener(cb);
|
46
|
}
|
47
|
|
48
|
function new_connection(port)
|
49
|
{
|
50
|
console.log("new page info connection!");
|
51
|
|
52
|
const connection_data = {
|
53
|
subscribed : new Set(),
|
54
|
port
|
55
|
};
|
56
|
|
57
|
let _handle_change = change => handle_change(connection_data, change);
|
58
|
|
59
|
storage.add_change_listener(_handle_change);
|
60
|
|
61
|
port.onMessage.addListener(m => handle_subscription(connection_data, m));
|
62
|
port.onDisconnect.addListener(() => remove_storage_listener(handle_change));
|
63
|
}
|
64
|
|
65
|
async function start_page_info_server()
|
66
|
{
|
67
|
storage = await get_storage();
|
68
|
query_all = await get_query_all();
|
69
|
|
70
|
listen_for_connection(CONNECTION_TYPE.PAGE_INFO, new_connection);
|
71
|
}
|
72
|
|
73
|
/*
|
74
|
* EXPORTS_START
|
75
|
* EXPORT start_page_info_server
|
76
|
* EXPORTS_END
|
77
|
*/
|