Project

General

Profile

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

haketilo / common / storage_light.js @ 2bd35bc4

1
/**
2
 * This file is part of Haketilo.
3
 *
4
 * Function: Storage manager, lighter than the previous one.
5
 *
6
 * Copyright (C) 2021 Wojtek Kosior
7
 * Redistribution terms are gathered in the `copyright' file.
8
 */
9

    
10
/*
11
 * IMPORTS_START
12
 * IMPORT TYPE_PREFIX
13
 * IMPORT raw_storage
14
 * IMPORT is_mozilla
15
 * IMPORT observables
16
 */
17

    
18
const reg_spec = new Set(["\\", "[", "]", "(", ")", "{", "}", ".", "*", "+"]);
19
const escape_reg_special = c => reg_spec.has(c) ? "\\" + c : c;
20

    
21
function make_regex(name)
22
{
23
    return new RegExp(`^${name.split("").map(escape_reg_special).join("")}\$`);
24
}
25

    
26
const listeners_by_callback = new Map();
27

    
28
function listen(callback, prefix, name)
29
{
30
    let by_prefix = listeners_by_callback.get(callback);
31
    if (!by_prefix) {
32
	by_prefix = new Map();
33
	listeners_by_callback.set(callback, by_prefix);
34
    }
35

    
36
    let by_name = by_prefix.get(prefix);
37
    if (!by_name) {
38
	by_name = new Map();
39
	by_prefix.set(prefix, by_name);
40
    }
41

    
42
    let name_reg = by_name.get(name);
43
    if (!name_reg) {
44
	name_reg = name.test ? name : make_regex(name);
45
	by_name.set(name, name_reg);
46
    }
47
}
48

    
49
function no_listen(callback, prefix, name)
50
{
51
    const by_prefix = listeners_by_callback.get(callback);
52
    if (!by_prefix)
53
	return;
54

    
55
    const by_name = by_prefix.get(prefix);
56
    if (!by_name)
57
	return;
58

    
59
    const name_reg = by_name.get(name);
60
    if (!name_reg)
61
	return;
62

    
63
    by_name.delete(name);
64

    
65
    if (by_name.size === 0)
66
	by_prefix.delete(prefix);
67

    
68
    if (by_prefix.size === 0)
69
	listeners_by_callback.delete(callback);
70
}
71

    
72
function storage_change_callback(changes, area)
73
{
74
    if (is_mozilla && area !== "local")
75
    {console.log("area", area);return;}
76

    
77
    for (const item of Object.keys(changes)) {
78
	for (const [callback, by_prefix] of listeners_by_callback.entries()) {
79
	    const by_name = by_prefix.get(item[0]);
80
	    if (!by_name)
81
		continue;
82

    
83
	    for (const reg of by_name.values()) {
84
		if (!reg.test(item.substring(1)))
85
		    continue;
86

    
87
		try {
88
		    callback(item, changes[item]);
89
		} catch(e) {
90
		    console.error(e);
91
		}
92
	    }
93
	}
94
    }
95
}
96

    
97
raw_storage.listen(storage_change_callback);
98

    
99

    
100
const created_observables = new Map();
101

    
102
async function observe(prefix, name)
103
{
104
    const observable = observables.make();
105
    const callback = (it, ch) => observables.set(observable, ch.newValue);
106
    listen(callback, prefix, name);
107
    created_observables.set(observable, [callback, prefix, name]);
108
    observables.silent_set(observable, await raw_storage.get(prefix + name));
109

    
110
    return observable;
111
}
112

    
113
const observe_var = name => observe(TYPE_PREFIX.VAR, name);
114

    
115
function no_observe(observable)
116
{
117
    no_listen(...created_observables.get(observable) || []);
118
    created_observables.delete(observable);
119
}
120

    
121
const light_storage = {};
122
Object.assign(light_storage, raw_storage);
123
Object.assign(light_storage,
124
	      {listen, no_listen, observe, observe_var, no_observe});
125

    
126
/*
127
 * EXPORTS_START
128
 * EXPORT light_storage
129
 * EXPORTS_END
130
 */
(14-14/16)