Skip to content
This repository has been archived by the owner on Sep 18, 2020. It is now read-only.

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ProbablePrime committed Oct 29, 2016
1 parent 6ba848e commit f697682
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 30 deletions.
2 changes: 1 addition & 1 deletion dashboard/panel.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ <h4 style="margin-top: 0;" class="">Recent Follows</h4>
}

function createAlertElem(type, obj) {
//TODO: Remove jquery
//TODO: Remove jquery, also WTF is this?
var list = document.querySelector('#' + type + '_list');
if (obj.replay !== undefined && obj.replay) {
return;
Expand Down
6 changes: 3 additions & 3 deletions lib/Channel.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const EventEmitter = require('events');
const Store = require('./Store');
const Store = require('./store');
const _ = require('lodash');

class Channel extends EventEmitter{
constructor(channelName, nodecg, live) {
super();
this.log(`Spinning up ${channelName}`);

this.live = live;
this.nodecg = nodecg;
this.name = channelName;

this.store = new Store(channelName);

this.log(`Spinning up ${channelName}`);
live.addChannel(this);
live.on('connected', this.checkFollows.bind(this));
}
Expand Down
57 changes: 31 additions & 26 deletions lib/Live.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ const fetch = require('node-fetch');
const Carina = require('carina').Carina;
Carina.WebSocket = ws;

function wrappedFetch(url) {
function jsonFetch(url) {
return fetch(url).then(res => res.json());
}

function fetchChannel(channelName) {
return wrappedFetch(`https://beam.pro/api/v1/channels/${channelName}`)
return jsonFetch(`https://beam.pro/api/v1/channels/${channelName}`)
}

function fetchNumFollowers(channelId) {
return wrappedFetch(`https://beam.pro/api/v1/channels/${channelId}?fields=numFollowers`);
return jsonFetch(`https://beam.pro/api/v1/channels/${channelId}?fields=numFollowers`);
}
function fetchPage(channelId, pageNum) {
return wrappedFetch(`https://beam.pro/api/v1/channels/${channelId}/follow?limit=100&fields=id,username&page=${pageNum}`)
return jsonFetch(`https://beam.pro/api/v1/channels/${channelId}/follow?limit=100&fields=id,username&page=${pageNum}`)
}

module.exports = class Live extends EventEmitter{
Expand All @@ -44,7 +44,7 @@ module.exports = class Live extends EventEmitter{
this.nodecg.log.warn('Disconnected from beam, Attempting a reconnection');
this.emit('disconnected');
});
this.carina.socket.on('error', err => self.log(err));
this.carina.socket.on('error', err => this.log(err));
}

log(msg) {
Expand All @@ -53,35 +53,42 @@ module.exports = class Live extends EventEmitter{
}

addChannel(channel) {
const self = this;
if (!self.channels[channel.name]) {
self.channels[channel.name] = channel;
if (!this.channels[channel.name]) {
this.channels[channel.name] = channel;
}
console.log('fetching channel, '+ channel.name);
fetchChannel(channel.name)
.then(res => {
//TODO, what even is this?
channel.setData(res);
console.log(res);
self.log('Subscribing to ' + channel.name + ' events');
self.registerEvent('user', 'update', channel.userID, channel);
self.registerEvent('channel', 'subscribed', channel.id, channel);
self.registerEvent('channel', 'resubscribed', channel.id, channel);
self.registerEvent('channel', 'update', channel.id, channel);
self.registerEvent('channel', 'status', channel.id, channel);
self.registerEvent('channel', 'followed', channel.id, channel);
self.registerEvent('channel', 'hosted', channel.id, channel);
self.log('Subscribed to ' + channel.name + ' events');
channel.checkFollows();
this.log('Subscribing to ' + channel.name + ' events');
const events = [
['channel', 'update'],
['channel', 'subscribed'],
['channel', 'resubscribed'],
['channel', 'status'],
['channel', 'followed'],
['channel', 'hosted'],

['user', 'update'],
];
events.forEach(event => {
this.registerEvent(event[0], event[1], channel);
});
this.log('Subscribed to ' + channel.name + ' events');
})
.catch(err => {
console.log(err);
})
}

registerEvent (type, eventName, id, channel) {
const self = this;
const slugString = self.buildSlug(type, eventName, id);
registerEvent (type, eventName, channel) {
let id;
if (type === 'channel') {
id = channel.id;
} else {
id = channel.userID;
}
const slugString = this.buildSlug(type, eventName, id);
console.log(`subscribing to ${slugString}`);
this.carina.subscribe(slugString, data => {
channel.onUpdate(slugString, data);
Expand All @@ -92,8 +99,7 @@ module.exports = class Live extends EventEmitter{
}
scrapeFollowers(channel) {
//TODO Refactor
const self = this;
self.log('Polling for Followers due to new connection');
this.log('Polling for Followers due to new connection');
return fetchNumFollowers(channel.id)
.then(res => {
const followers = res.numFollowers;
Expand All @@ -104,7 +110,6 @@ module.exports = class Live extends EventEmitter{
}
return pages;
}).then(pages => {
console.log(pages);
const length = pages;
let pageNum = 0;
const promises = [];
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit f697682

Please sign in to comment.