1
|
/**
|
2
|
* This file is part of Haketilo.
|
3
|
*
|
4
|
* Function: Facilitate listening to (internal, self-generated) events.
|
5
|
*
|
6
|
* Copyright (C) 2021 Wojtek Kosior
|
7
|
* Redistribution terms are gathered in the `copyright' file.
|
8
|
*/
|
9
|
|
10
|
const make = (value=undefined) => ({value, listeners: new Set()});
|
11
|
const subscribe = (observable, cb) => observable.listeners.add(cb);
|
12
|
const unsubscribe = (observable, cb) => observable.listeners.delete(cb);
|
13
|
|
14
|
const silent_set = (observable, value) => observable.value = value;
|
15
|
const broadcast = (observable, ...values) =>
|
16
|
observable.listeners.forEach(cb => cb(...values));
|
17
|
|
18
|
function set(observable, value)
|
19
|
{
|
20
|
const old_value = observable.value;
|
21
|
silent_set(observable, value);
|
22
|
broadcast(observable, value, old_value);
|
23
|
}
|
24
|
|
25
|
const observables = {make, subscribe, unsubscribe, broadcast, silent_set, set};
|
26
|
|
27
|
/*
|
28
|
* EXPORTS_START
|
29
|
* EXPORT observables
|
30
|
* EXPORTS_END
|
31
|
*/
|