Project

General

Profile

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

haketilo / common / storage_client.js @ 263d03d5

1
/**
2
 * This file is part of Haketilo.
3
 *
4
 * Function: Storage through messages (client side).
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
 * license. Although I request that you do not make use this code in a
41
 * proprietary program, I am not going to enforce this in court.
42
 */
43

    
44
/*
45
 * IMPORTS_START
46
 * IMPORT CONNECTION_TYPE
47
 * IMPORT list_prefixes
48
 * IMPORT make_once
49
 * IMPORT browser
50
 * IMPORTS_END
51
 */
52

    
53
var call_id = 0;
54
var port;
55
var calls_waiting = new Map();
56

    
57
function set_call_callback(resolve, reject, func, args)
58
{
59
    port.postMessage([call_id, func, args]);
60
    calls_waiting.set(call_id++, [resolve, reject]);
61
}
62

    
63
async function remote_call(func, args)
64
{
65
    return new Promise((resolve, reject) =>
66
		       set_call_callback(resolve, reject, func, args));
67
}
68

    
69
function handle_message(message)
70
{
71
    let callbacks = calls_waiting.get(message.call_id);
72
    if (callbacks === undefined) {
73
	handle_change(message);
74
	return;
75
    }
76

    
77
    let [resolve, reject] = callbacks;
78
    calls_waiting.delete(message.call_id);
79
    if (message.error !== undefined)
80
	setTimeout(reject, 0, message.error);
81
    else
82
	setTimeout(resolve, 0, message.result);
83
}
84

    
85
const list_by_prefix = {};
86

    
87
for (const prefix of list_prefixes)
88
    list_by_prefix[prefix] = {prefix, listeners : new Set()};
89

    
90
var resolve_init;
91

    
92
function handle_first_message(message)
93
{
94
    for (let prefix of Object.keys(message))
95
	list_by_prefix[prefix].map = new Map(message[prefix]);
96

    
97
    port.onMessage.removeListener(handle_first_message);
98
    port.onMessage.addListener(handle_message);
99

    
100
    resolve_init();
101
}
102

    
103
function handle_change(change)
104
{
105
    let list = list_by_prefix[change.prefix];
106

    
107
    if (change.new_val === undefined)
108
	list.map.delete(change.item);
109
    else
110
	list.map.set(change.item, change.new_val);
111

    
112
    for (let listener_callback of list.listeners)
113
	listener_callback(change);
114
}
115

    
116
var exports = {};
117

    
118
function start_connection(resolve)
119
{
120
    resolve_init = resolve;
121
    port = browser.runtime.connect({name : CONNECTION_TYPE.REMOTE_STORAGE});
122
    port.onMessage.addListener(handle_first_message);
123
}
124

    
125
async function init() {
126
    await new Promise((resolve, reject) => start_connection(resolve));
127
    return exports;
128
}
129

    
130
for (let call_name of ["set", "remove", "replace", "clear"])
131
    exports [call_name] = (...args) => remote_call(call_name, args);
132

    
133
// TODO: Much of the code below is copy-pasted from /background/storage.mjs.
134
//       This should later be refactored into a separate module
135
//       to avoid duplication.
136

    
137
/*
138
 * Facilitate listening to changes
139
 */
140

    
141
exports.add_change_listener = function (cb, prefixes=list_prefixes)
142
{
143
    if (typeof(prefixes) === "string")
144
	prefixes = [prefixes];
145

    
146
    for (let prefix of prefixes)
147
	list_by_prefix[prefix].listeners.add(cb);
148
}
149

    
150
exports.remove_change_listener = function (cb, prefixes=list_prefixes)
151
{
152
    if (typeof(prefixes) === "string")
153
	prefixes = [prefixes];
154

    
155
    for (let prefix of prefixes)
156
	list_by_prefix[prefix].listeners.delete(cb);
157
}
158

    
159
/* Prepare some hepler functions to get elements of a list */
160

    
161
function list_items_it(list, with_values=false)
162
{
163
    return with_values ? list.map.entries() : list.map.keys();
164
}
165

    
166
function list_entries_it(list)
167
{
168
    return list_items_it(list, true);
169
}
170

    
171
function list_items(list, with_values=false)
172
{
173
    let array = [];
174

    
175
    for (let item of list_items_it(list, with_values))
176
	array.push(item);
177

    
178
    return array;
179
}
180

    
181
function list_entries(list)
182
{
183
    return list_items(list, true);
184
}
185

    
186
exports.get = function (prefix, item)
187
{
188
    return list_by_prefix[prefix].map.get(item);
189
}
190

    
191
exports.get_all_names = function (prefix)
192
{
193
    return list_items(list_by_prefix[prefix]);
194
}
195

    
196
exports.get_all_names_it = function (prefix)
197
{
198
    return list_items_it(list_by_prefix[prefix]);
199
}
200

    
201
exports.get_all = function (prefix)
202
{
203
    return list_entries(list_by_prefix[prefix]);
204
}
205

    
206
exports.get_all_it = function (prefix)
207
{
208
    return list_entries_it(list_by_prefix[prefix]);
209
}
210

    
211
const get_remote_storage = make_once(init);
212

    
213
/*
214
 * EXPORTS_START
215
 * EXPORT get_remote_storage
216
 * EXPORTS_END
217
 */
(13-13/16)