-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (62 loc) · 1.24 KB
/
index.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* global module */
const store = require('store');
const updatePlugin = require('store/plugins/update');
store.addPlugin(updatePlugin);
const storeKey = 'gatsby:trackers';
function registerTracker({ id, label }) {
if (!getTracker(id)) {
store.update(
storeKey,
{},
(obj) => {
obj[id] = { label, enabled: false }
}
);
}
}
function updateTracker(id, enabled) {
store.update(
storeKey,
{},
(obj) => {
if (typeof obj[id] === 'object') {
obj[id].enabled = enabled
}
}
);
}
function updateTrackers(enabled) {
const trackers = getTrackers();
if (!trackers) {
return;
}
Object.keys(trackers).forEach(id => updateTracker(id, enabled));
}
function getTrackers() {
return store.get(storeKey);
}
function getTracker(id) {
const trackers = getTrackers();
return trackers && trackers[id] || null;
}
function enableTracker(id) {
updateTracker(id, true);
}
function enableTrackers() {
updateTrackers(true);
}
function disableTracker(id) {
updateTracker(id, false);
}
function disableTrackers() {
updateTrackers(false);
}
module.exports = {
registerTracker,
getTrackers,
getTracker,
enableTracker,
enableTrackers,
disableTracker,
disableTrackers,
};