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

Commit

Permalink
tidying up
Browse files Browse the repository at this point in the history
  • Loading branch information
ProbablePrime committed Jan 21, 2018
1 parent 2bfa730 commit 9d6a63d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 31 deletions.
32 changes: 17 additions & 15 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,24 @@ module.exports = function(extensionApi) {
nodecg.sendMessage('update', channel, data);
}

function addChannels() {
nodecg.bundleConfig.channels.forEach(channelName => {
if (channelCache[channelName] !== undefined) {
return;
}
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;
});
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) {
Expand Down
11 changes: 6 additions & 5 deletions lib/Channel.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const EventEmitter = require('events');
const Store = require('./store');
const _ = require('lodash');
const { FOLLOW, HOST, SUBSCRIPTION } = require('./util');

class Channel extends EventEmitter {
constructor(channelName, nodecg, live) {
Expand Down Expand Up @@ -78,14 +79,14 @@ class Channel extends EventEmitter {
}

handleHost(host) {
this.emit('host', host, Date.now());
this.emit(HOST, host, Date.now());
}

handleFollow(username) {
const ts = Date.now();
this.store.hasFollow(username, ts).then(result => {
if (!result) {
this.emit('follow', username, ts);
this.emit(FOLLOW, username, ts);
this.store.addFollow(username, ts);
}
});
Expand All @@ -95,17 +96,17 @@ class Channel extends EventEmitter {
const ts = Date.now();
this.store.hasSubscription(username, ts).then(result => {
if (!result) {
this.emit('subscription', username, ts);
this.emit(SUBSCRIPTION, username, ts);
this.store.addSubscription(username, ts);
}
});
}

dismiss(item) {
if (item.type === 'follow') {
if (item.type === FOLLOW) {
this.store.dismissFollow(item.username);
}
if (item.type === 'subscription') {
if (item.type === SUBSCRIPTION) {
this.store.dismissSubscription(item.username);
}
}
Expand Down
17 changes: 9 additions & 8 deletions lib/store/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Datastore = require('nedb');

const createUser = require('./createUser');
const { FOLLOW, HOST, SUBSCRIPTION } = require('../util');

const Store = function(name) {
var dbName;
Expand Down Expand Up @@ -62,10 +63,10 @@ Store.prototype = {
});
},
findUnDismissedFollows: function() {
return this._findUnDismissed('follow');
return this._findUnDismissed(FOLLOW);
},
findUnDismissedSubscriptions: function() {
return this._findUnDismissed('subscription');
return this._findUnDismissed(SUBSCRIPTION);
},
getUser: function(username) {
var self = this;
Expand All @@ -80,10 +81,10 @@ Store.prototype = {
});
},
hasFollow: function(username) {
return this._hasEvent(username, 'follow');
return this._hasEvent(username, FOLLOW);
},
hasSubscription: function(username, ts) {
return this._hasEvent(username, 'subscription', ts);
return this._hasEvent(username, SUBSCRIPTION, ts);
},
_hasEvent: function(username, type, ts) {
var self = this;
Expand Down Expand Up @@ -119,10 +120,10 @@ Store.prototype = {
});
},
addFollow: function(username, ts) {
return this._addEvent(username, 'follow', ts);
return this._addEvent(username, FOLLOW, ts);
},
addSubscription: function(username, ts) {
return this._addEvent(username, 'subscription', ts);
return this._addEvent(username, SUBSCRIPTION, ts);
},
_dismissEvent: function(username, type) {
var self = this;
Expand All @@ -140,10 +141,10 @@ Store.prototype = {
});
},
dismissFollow: function(username) {
return this._dismissEvent(username, 'follow');
return this._dismissEvent(username, FOLLOW);
},
dismissSubscription: function(username) {
return this._dismissEvent(username, 'subscription');
return this._dismissEvent(username, SUBSCRIPTION);
}
};
module.exports = Store;
9 changes: 6 additions & 3 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export const SUBSCRIPTION = 'subscription';
export const FOLLOW = 'follow';
export const HOST = 'host';
module.exports = {
SUBSCRIPTION: 'subscription',
FOLLOW: 'follow',
HOST: 'host',
UNFOLLOW: 'unfollow',
};

0 comments on commit 9d6a63d

Please sign in to comment.