-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathtracking.js
53 lines (46 loc) · 906 Bytes
/
tracking.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'use strict';
var listeners = {};
var listening = false;
function listen () {
if (global.addEventListener) {
global.addEventListener('storage', change, false);
} else if (global.attachEvent) {
global.attachEvent('onstorage', change);
} else {
global.onstorage = change;
}
}
function change (e) {
if (!e) {
e = global.event;
}
var all = listeners[e.key];
if (all) {
all.forEach(fire);
}
function fire (listener) {
listener(JSON.parse(e.newValue), JSON.parse(e.oldValue), e.url || e.uri);
}
}
function on (key, fn) {
if (listeners[key]) {
listeners[key].push(fn);
} else {
listeners[key] = [fn];
}
if (listening === false) {
listen();
}
}
function off (key, fn) {
var ns = listeners[key];
if (ns.length > 1) {
ns.splice(ns.indexOf(fn), 1);
} else {
listeners[key] = [];
}
}
module.exports = {
on: on,
off: off
};