Project

General

Profile

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

haketilo / background / broadcast_broker.js @ 4c6a2323

1
/**
2
 * This file is part of Haketilo.
3
 *
4
 * Function: Facilitate broadcasting messages between different execution
5
 *           contexts of the extension
6
 *
7
 * Copyright (C) 2021 Wojtek Kosior
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * As additional permission under GNU GPL version 3 section 7, you
20
 * may distribute forms of that code without the copy of the GNU
21
 * GPL normally required by section 4, provided you include this
22
 * license notice and, in case of non-source distribution, a URL
23
 * through which recipients can access the Corresponding Source.
24
 * If you modify file(s) with this exception, you may extend this
25
 * exception to your version of the file(s), but you are not
26
 * obligated to do so. If you do not wish to do so, delete this
27
 * exception statement from your version.
28
 *
29
 * As a special exception to the GPL, any HTML file which merely
30
 * makes function calls to this code, and for that purpose
31
 * includes it by reference shall be deemed a separate work for
32
 * copyright law purposes. If you modify this code, you may extend
33
 * this exception to your version of the code, but you are not
34
 * obligated to do so. If you do not wish to do so, delete this
35
 * exception statement from your version.
36
 *
37
 * You should have received a copy of the GNU General Public License
38
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
39
 *
40
 * I, Wojtek Kosior, thereby promise not to sue for violation of this file's
41
 * license. Although I request that you do not make use of this code in a
42
 * proprietary program, I am not going to enforce this in court.
43
 */
44

    
45
#FROM common/message_server.js IMPORT listen_for_connection
46

    
47
let next_id = 1;
48

    
49
const listeners_by_channel = new Map();
50

    
51
function new_broadcast_listener(port)
52
{
53
    const listener_ctx = {port, id: ++next_id, channels: new Set()};
54
    port.onMessage.addListener(msg => listener_command(msg, listener_ctx));
55
    const on_disconnect = () => remove_broadcast_listener(listener_ctx);
56
    port.onDisconnect.addListener(on_disconnect);
57
}
58

    
59
function listener_command(msg, listener_ctx)
60
{
61
    const [disposition, name] = msg;
62

    
63
    if (disposition === "subscribe")
64
	subscribe_channel(name, listener_ctx);
65
    else if (disposition === "unsubscribe")
66
	unsubscribe_channel(name, listener_ctx);
67
    else
68
	throw `bad broadcast listener disposition '${disposition}'`;
69
}
70

    
71
function subscribe_channel(channel_name, listener_ctx)
72
{
73
    if (!listeners_by_channel.has(channel_name))
74
	listeners_by_channel.set(channel_name, new Map());
75

    
76
    listeners_by_channel.get(channel_name).set(listener_ctx.id, listener_ctx);
77

    
78
    listener_ctx.channels.add(channel_name);
79
}
80

    
81
function unsubscribe_channel(channel_name, listener_ctx)
82
{
83
    const channel_listeners =
84
	  listeners_by_channel.get(channel_name) || new Map();
85
    channel_listeners.delete(listener_ctx.id);
86

    
87
    if (channel_listeners.size == 0)
88
	listeners_by_channel.delete(channel_name);
89

    
90
    listener_ctx.channels.delete(channel_name);
91
}
92

    
93
function remove_broadcast_listener(listener_ctx)
94
{
95
    for (const channel_name of [...listener_ctx.channels.keys()])
96
	unsubscribe_channel(channel_name, listener_ctx);
97
}
98

    
99
function new_broadcast_sender(port)
100
{
101
    const sender_ctx = {prepared_broadcasts: new Set()};
102
    port.onMessage.addListener(msg => sender_command(msg, sender_ctx));
103
    port.onDisconnect.addListener(msg => flush(sender_ctx));
104
}
105

    
106
function sender_command(msg, sender_ctx)
107
{
108
    const [disposition, name, value, timeout] = msg;
109

    
110
    if (disposition === "prepare")
111
	prepare(sender_ctx, name, value, timeout)
112
    else if (disposition === "discard")
113
	sender_ctx.prepared_broadcasts = new Set();
114
    else if (disposition === "flush")
115
	flush(sender_ctx);
116
    else if (disposition === "broadcast")
117
	broadcast(name, value);
118
    else
119
	throw `bad broadcast sender disposition '${disposition}'`;
120
}
121

    
122
function prepare(sender_ctx, channel_name, value, timeout)
123
{
124
    const broadcast_data = [channel_name, value];
125
    sender_ctx.prepared_broadcasts.add(broadcast_data);
126

    
127
    if (timeout === 0)
128
	return;
129

    
130
    setTimeout(() => prepare_timeout_cb(sender_ctx, broadcast_data), timeout);
131
}
132

    
133
function prepare_timeout_cb(sender_ctx, broadcast_data)
134
{
135
    if (sender_ctx.prepared_broadcasts.has(broadcast_data)) {
136
	sender_ctx.prepared_broadcasts.delete(broadcast_data);
137
	broadcast(...broadcast_data);
138
    }
139
}
140

    
141
function flush(sender_ctx)
142
{
143
    sender_ctx.prepared_broadcasts.forEach(nv => broadcast(...nv));
144
    sender_ctx.prepared_broadcasts = new Set();
145
}
146

    
147
function broadcast(channel_name, value)
148
{
149
    const listeners = listeners_by_channel.get(channel_name);
150
    if (listeners == undefined)
151
	return;
152

    
153
    for (const listener_ctx of [...listeners.values()]) {
154
	try {
155
	    listener_ctx.port.postMessage([channel_name, value]);
156
	} catch (e) {
157
	    console.error(e);
158
	    remove_broadcast_listener(listener_ctx);
159
	}
160
    }
161
}
162

    
163
function remove_broadcast_sender(sender_ctx)
164
{
165
    sender_ctx.prepared_broadcasts.forEach(nv => broadcast(...nv));
166
}
167

    
168
function start()
169
{
170
    listen_for_connection("broadcast_send", new_broadcast_sender);
171
    listen_for_connection("broadcast_listen", new_broadcast_listener);
172
}
173
#EXPORT start
(3-3/7)