This repository has been archived by the owner on Sep 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
107 lines (92 loc) · 2.66 KB
/
extension.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'use strict';
const Channel = require('./lib/Channel.js');
const LiveLoading = require('./lib/Live.js');
const channelCache = {};
const toArray = require('object-values-to-array');
const { FOLLOW, HOST, SUBSCRIPTION } = require('./lib/util');
module.exports = function(extensionApi) {
const nodecg = extensionApi;
const live = new LiveLoading(nodecg);
if (!nodecg.bundleConfig || !Object.keys(nodecg.bundleConfig).length) {
throw new Error('No config found in cfg/nodecg-mixer.json, aborting!');
}
if (!nodecg.bundleConfig.channels) {
throw new Error('No channels present in the config file aborting');
}
function log(msg) {
nodecg.log.info(msg);
nodecg.sendMessage('log', msg);
}
function onEvent(channel, type, username, ts) {
log(`${type}: username`);
nodecg.sendMessage(type, {
username,
type,
channel,
ts: ts ? ts : Date.now(),
});
}
function onUpdate(channel, data) {
nodecg.sendMessage('update', channel, data);
}
function addChannel(channelName) {
if (channelCache[channelName] !== undefined) {
return;
}
const channel = new Channel(channelName, nodecg, live);
channel.on(FOLLOW, onEvent.bind(this, channelName, FOLLOW));
channel.on(SUBSCRIPTION, onEvent.bind(this, channelName, SUBSCRIPTION));
channel.on('update', onUpdate.bind(this, channelName, 'update'));
channel.on(HOST, onEvent.bind(this, channelName, HOST));
channelCache[channelName] = channel;
}
function addChannels() {
nodecg.bundleConfig.channels.forEach(channelName =>
addChannel(channelName),
);
}
function eachChannel(func) {
return toArray(channelCache).map(func);
}
function getUnDismissed(type, cb) {
if (typeof cb !== 'function') {
return;
}
var func = 'findUnDismissedFollows';
if (type === SUBSCRIPTION) {
func = 'findUnDismissedSubscriptions';
}
const promises = eachChannel(channel => channel[func]());
Promise.all(promises)
.then(result => {
const combinedArray = result
.reduce((previous, next) => previous.concat(next), [])
.map(item => {
return {
username: item.username,
type,
ts: item[type].ts ? item[type].ts : 0,
channel: 0,
};
});
cb(null, combinedArray);
})
.catch(err => {
this.nodecg.log.error(err);
cb(err, []);
});
}
nodecg.listenFor('getFollows', function(value, cb) {
getUnDismissed(FOLLOW, cb);
});
nodecg.listenFor('getChannelData', function(value, cb) {
cb(null, channelCache[value].data);
});
nodecg.listenFor('getSubscriptions', function(value, cb) {
getUnDismissed(SUBSCRIPTION, cb);
});
nodecg.listenFor('dismiss', function(value) {
eachChannel(channel => channel.dismissEvent(value));
});
addChannels();
};