1 |
58fe4c7d
|
Wojtek Kosior
|
/**
|
2 |
|
|
* This file is part of Haketilo.
|
3 |
|
|
*
|
4 |
|
|
* Function: Broadcast messages to different execution contexts of the extension
|
5 |
|
|
*
|
6 |
|
|
* Copyright (C) 2021 Wojtek Kosior
|
7 |
|
|
*
|
8 |
|
|
* This program is free software: you can redistribute it and/or modify
|
9 |
|
|
* it under the terms of the GNU General Public License as published by
|
10 |
|
|
* the Free Software Foundation, either version 3 of the License, or
|
11 |
|
|
* (at your option) any later version.
|
12 |
|
|
*
|
13 |
|
|
* This program is distributed in the hope that it will be useful,
|
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
|
|
* GNU General Public License for more details.
|
17 |
|
|
*
|
18 |
|
|
* As additional permission under GNU GPL version 3 section 7, you
|
19 |
|
|
* may distribute forms of that code without the copy of the GNU
|
20 |
|
|
* GPL normally required by section 4, provided you include this
|
21 |
|
|
* license notice and, in case of non-source distribution, a URL
|
22 |
|
|
* through which recipients can access the Corresponding Source.
|
23 |
|
|
* If you modify file(s) with this exception, you may extend this
|
24 |
|
|
* exception to your version of the file(s), but you are not
|
25 |
|
|
* obligated to do so. If you do not wish to do so, delete this
|
26 |
|
|
* exception statement from your version.
|
27 |
|
|
*
|
28 |
|
|
* As a special exception to the GPL, any HTML file which merely
|
29 |
|
|
* makes function calls to this code, and for that purpose
|
30 |
|
|
* includes it by reference shall be deemed a separate work for
|
31 |
|
|
* copyright law purposes. If you modify this code, you may extend
|
32 |
|
|
* this exception to your version of the code, but you are not
|
33 |
|
|
* obligated to do so. If you do not wish to do so, delete this
|
34 |
|
|
* exception statement from your version.
|
35 |
|
|
*
|
36 |
|
|
* You should have received a copy of the GNU General Public License
|
37 |
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
38 |
|
|
*
|
39 |
|
|
* I, Wojtek Kosior, thereby promise not to sue for violation of this file's
|
40 |
372d24ea
|
Wojtek Kosior
|
* license. Although I request that you do not make use of this code in a
|
41 |
58fe4c7d
|
Wojtek Kosior
|
* proprietary program, I am not going to enforce this in court.
|
42 |
|
|
*/
|
43 |
|
|
|
44 |
01e977f9
|
Wojtek Kosior
|
#FROM common/message_server.js IMPORT connect_to_background
|
45 |
58fe4c7d
|
Wojtek Kosior
|
|
46 |
|
|
function sender_connection()
|
47 |
|
|
{
|
48 |
|
|
return {
|
49 |
4c6a2323
|
Wojtek Kosior
|
port: connect_to_background("broadcast_send")
|
50 |
58fe4c7d
|
Wojtek Kosior
|
};
|
51 |
|
|
}
|
52 |
b590eaa2
|
Wojtek Kosior
|
#EXPORT sender_connection
|
53 |
58fe4c7d
|
Wojtek Kosior
|
|
54 |
|
|
function out(sender_conn, channel_name, value)
|
55 |
|
|
{
|
56 |
|
|
sender_conn.port.postMessage(["broadcast", channel_name, value]);
|
57 |
|
|
}
|
58 |
b590eaa2
|
Wojtek Kosior
|
#EXPORT out
|
59 |
58fe4c7d
|
Wojtek Kosior
|
|
60 |
b7378a99
|
Wojtek Kosior
|
/*
|
61 |
|
|
* prepare()'d message will be broadcasted if the connection is closed or when
|
62 |
|
|
* flush() is called. All messages prepared by given sender will be discarded
|
63 |
|
|
* when discard() is called.
|
64 |
|
|
*
|
65 |
|
|
* Timeout will cause the prepared message to be broadcasted after given number
|
66 |
|
|
* of miliseconds unless discard() is called in the meantime. It is mostly
|
67 |
|
|
* useful as a fallback in case this connection end gets nuked (e.g. because
|
68 |
|
|
* browser window is closed) and onDisconnect event is not dispatched to
|
69 |
|
|
* background script's Port object. This is only an issue for browsers as old as
|
70 |
|
|
* IceCat 60: https://bugzilla.mozilla.org/show_bug.cgi?id=1392067
|
71 |
|
|
*
|
72 |
|
|
* Timeout won't be scheduled at all if its value is given as 0.
|
73 |
|
|
*/
|
74 |
58fe4c7d
|
Wojtek Kosior
|
function prepare(sender_conn, channel_name, value, timeout=5000)
|
75 |
|
|
{
|
76 |
|
|
sender_conn.port.postMessage(["prepare", channel_name, value, timeout]);
|
77 |
|
|
}
|
78 |
b590eaa2
|
Wojtek Kosior
|
#EXPORT prepare
|
79 |
58fe4c7d
|
Wojtek Kosior
|
|
80 |
|
|
function discard(sender_conn)
|
81 |
|
|
{
|
82 |
|
|
sender_conn.port.postMessage(["discard"]);
|
83 |
|
|
}
|
84 |
b590eaa2
|
Wojtek Kosior
|
#EXPORT discard
|
85 |
58fe4c7d
|
Wojtek Kosior
|
|
86 |
|
|
function flush(sender_conn)
|
87 |
|
|
{
|
88 |
|
|
sender_conn.port.postMessage(["flush"]);
|
89 |
|
|
}
|
90 |
b590eaa2
|
Wojtek Kosior
|
#EXPORT flush
|
91 |
58fe4c7d
|
Wojtek Kosior
|
|
92 |
|
|
function listener_connection(cb)
|
93 |
|
|
{
|
94 |
|
|
const conn = {
|
95 |
4c6a2323
|
Wojtek Kosior
|
port: connect_to_background("broadcast_listen")
|
96 |
58fe4c7d
|
Wojtek Kosior
|
};
|
97 |
|
|
|
98 |
|
|
conn.port.onMessage.addListener(cb);
|
99 |
|
|
|
100 |
|
|
return conn;
|
101 |
|
|
}
|
102 |
b590eaa2
|
Wojtek Kosior
|
#EXPORT listener_connection
|
103 |
58fe4c7d
|
Wojtek Kosior
|
|
104 |
|
|
function subscribe(listener_conn, channel_name)
|
105 |
|
|
{
|
106 |
|
|
listener_conn.port.postMessage(["subscribe", channel_name]);
|
107 |
|
|
}
|
108 |
b590eaa2
|
Wojtek Kosior
|
#EXPORT subscribe
|
109 |
58fe4c7d
|
Wojtek Kosior
|
|
110 |
|
|
function unsubscribe(listener_conn, channel_name)
|
111 |
|
|
{
|
112 |
|
|
listener_conn.port.postMessage(["unsubscribe", channel_name]);
|
113 |
|
|
}
|
114 |
b590eaa2
|
Wojtek Kosior
|
#EXPORT unsubscribe
|
115 |
58fe4c7d
|
Wojtek Kosior
|
|
116 |
|
|
function close(conn)
|
117 |
|
|
{
|
118 |
|
|
conn.port.disconnect();
|
119 |
|
|
}
|
120 |
b590eaa2
|
Wojtek Kosior
|
#EXPORT close
|