-
Notifications
You must be signed in to change notification settings - Fork 0
/
bus.js
34 lines (26 loc) · 929 Bytes
/
bus.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
(function(root) { 'use strict'
var subscriptions = {}
function trigger(topic, value) {
delete sessionStorage.bus
sessionStorage.bus = JSON.stringify({ topic: topic, value: value })
propagateLocally(topic, value)
}
function propagateLocally(topic, value) {
if(!(topic in subscriptions)) return
subscriptions[topic].forEach(function(sub) { sub(value) })
}
addEventListener('storage', function(evt) {
if(evt.key !== 'bus' || !evt.newValue) return
var message = JSON.parse(evt.newValue)
propagateLocally(message.topic, message.value)
}, false)
function on(topic, handler) {
(subscriptions[topic] = subscriptions[topic] || []).push(handler)
}
function off(topic, handler) {
if(!(topic in subscriptions)) return
subscriptions[topic].splice(subscriptions[topic].indexOf(handler), 1)
if(!subscriptions[topic].length) delete subscriptions[topic]
}
root.bus = { trigger: trigger, on: on, off: off }
})(this)