Project

General

Profile

« Previous | Next » 

Revision d42dadca

Added by koszko about 2 years ago

extract observables implementation from storage.js

View differences:

background/storage.js
16 16
 * IMPORT make_once
17 17
 * IMPORT browser
18 18
 * IMPORT is_chrome
19
 * IMPORT observables
19 20
 * IMPORTS_END
20 21
 */
21 22

  
......
98 99
    for (let item of await get_list_var(name))
99 100
	map.set(item, await get(prefix + item));
100 101

  
101
    return {map, prefix, name, listeners : new Set(), lock : make_lock()};
102
    return {map, prefix, name, observable: observables.make(),
103
	    lock: make_lock()};
102 104
}
103 105

  
104 106
var list_by_prefix = {};
......
121 123
	prefixes = [prefixes];
122 124

  
123 125
    for (let prefix of prefixes)
124
	list_by_prefix[prefix].listeners.add(cb);
126
	observables.subscribe(list_by_prefix[prefix].observable, cb);
125 127
}
126 128

  
127 129
exports.remove_change_listener = function (cb, prefixes=list_prefixes)
......
130 132
	prefixes = [prefixes];
131 133

  
132 134
    for (let prefix of prefixes)
133
	list_by_prefix[prefix].listeners.delete(cb);
134
}
135

  
136
function broadcast_change(change, list)
137
{
138
    for (let listener_callback of list.listeners)
139
	listener_callback(change);
135
	observables.unsubscribe(list_by_prefix[prefix].observable, cb);
140 136
}
141 137

  
142 138
/* Prepare some hepler functions to get elements of a list */
......
198 194
	new_val : value
199 195
    };
200 196

  
201
    broadcast_change(change, list);
197
    observables.broadcast(list.observable, change);
202 198

  
203 199
    return old_val;
204 200
}
......
236 232
	new_val : undefined
237 233
    };
238 234

  
239
    broadcast_change(change, list);
235
    observables.broadcast(list.observable, change);
240 236

  
241 237
    return old_val;
242 238
}
......
282 278
	new_val : undefined
283 279
    };
284 280

  
285
    broadcast_change(change, list);
281
    observables.broadcast(list.observable, change);
286 282

  
287 283
    list.map.set(new_item, new_val);
288 284

  
......
290 286
    change.old_val = undefined;
291 287
    change.new_val = new_val;
292 288

  
293
    broadcast_change(change, list);
289
    observables.broadcast(list.observable, change);
294 290

  
295 291
    return old_val;
296 292
}
......
375 371
	for (let [item, val] of list_entries_it(list)) {
376 372
	    change.item = item;
377 373
	    change.old_val = val;
378
	    broadcast_change(change, list);
374
	    observables.broadcast(list.observable, change);
379 375
	}
380 376

  
381 377
	list.map = new Map();
common/observable.js
1
/**
2
 * part of Hachette
3
 * Facilitate listening to events
4
 *
5
 * Copyright (C) 2021 Wojtek Kosior
6
 * Redistribution terms are gathered in the `copyright' file.
7
 */
8

  
9
function make()
10
{
11
    return new Set();
12
}
13

  
14
function subscribe(observable, cb)
15
{
16
    observable.add(cb);
17
}
18

  
19
function unsubscribe(observable, cb)
20
{
21
    observable.delete(cb);
22
}
23

  
24
function broadcast(observable, event)
25
{
26
    for (const callback of observable)
27
	callback(event);
28
}
29

  
30
const observables = {make, subscribe, unsubscribe, broadcast};
31

  
32
/*
33
 * EXPORTS_START
34
 * EXPORT observables
35
 * EXPORTS_END
36
 */

Also available in: Unified diff