summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorWojtek Kosior <koszko@koszko.org>2021-07-23 17:32:31 +0200
committerWojtek Kosior <koszko@koszko.org>2021-07-23 17:32:31 +0200
commitd42dadca04bd2c40cfe836b3f2bbe0ffbf176eda (patch)
tree66150ce87658c2d8cc931ade95b959712496c9f4 /common
parentc483ae19e110ef5c1e539883a38fbc79b3dd4e4e (diff)
downloadbrowser-extension-d42dadca04bd2c40cfe836b3f2bbe0ffbf176eda.tar.gz
browser-extension-d42dadca04bd2c40cfe836b3f2bbe0ffbf176eda.zip
extract observables implementation from storage.js
Diffstat (limited to 'common')
-rw-r--r--common/observable.js36
1 files changed, 36 insertions, 0 deletions
diff --git a/common/observable.js b/common/observable.js
new file mode 100644
index 0000000..1fb0b0a
--- /dev/null
+++ b/common/observable.js
@@ -0,0 +1,36 @@
+/**
+ * part of Hachette
+ * Facilitate listening to events
+ *
+ * Copyright (C) 2021 Wojtek Kosior
+ * Redistribution terms are gathered in the `copyright' file.
+ */
+
+function make()
+{
+ return new Set();
+}
+
+function subscribe(observable, cb)
+{
+ observable.add(cb);
+}
+
+function unsubscribe(observable, cb)
+{
+ observable.delete(cb);
+}
+
+function broadcast(observable, event)
+{
+ for (const callback of observable)
+ callback(event);
+}
+
+const observables = {make, subscribe, unsubscribe, broadcast};
+
+/*
+ * EXPORTS_START
+ * EXPORT observables
+ * EXPORTS_END
+ */