Project

General

Profile

Download (1.56 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / background / page_info_server.js @ 9e26b71e

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 new_connection(port)
44
{
45
    console.log("new page info connection!");
46

    
47
    const connection_data = {
48
	subscribed : new Set(),
49
	port
50
    };
51

    
52
    let _handle_change = change => handle_change(connection_data, change);
53

    
54
    storage.add_change_listener(_handle_change);
55

    
56
    port.onMessage.addListener(m => handle_subscription(connection_data, m));
57
    port.onDisconnect.addListener(
58
	() => storage.remove_change_listener(_handle_change)
59
    );
60
}
61

    
62
async function start_page_info_server()
63
{
64
    storage = await get_storage();
65
    query_all = await get_query_all();
66

    
67
    listen_for_connection(CONNECTION_TYPE.PAGE_INFO, new_connection);
68
}
69

    
70
/*
71
 * EXPORTS_START
72
 * EXPORT start_page_info_server
73
 * EXPORTS_END
74
 */
(3-3/7)