Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ export default function slackin ({
app.use(cors())
}

// Temporarily increase the client connection count to allow
// slack to restart the fetch interval (if needed). This allows
// static connections to pull new data if dynamic clients are
// not connected and slack fetching has been paused.
let pingSlack = function() {
slack.clientConnected()
setTimeout(function() {
slack.clientDisconnected()
}, slack.interval * 4)
}

// splash page
app.get('/', (req, res) => {
let { name, logo } = slack.org
Expand All @@ -101,6 +112,7 @@ export default function slackin ({
})

app.get('/data', (req, res) => {
pingSlack()
let { name, logo } = slack.org
let { active, total } = slack.users
res.send({
Expand Down Expand Up @@ -262,6 +274,7 @@ export default function slackin ({

// badge rendering
app.get('/badge.svg', (req, res) => {
pingSlack()
res.type('svg')
res.set('Cache-Control', 'max-age=0, no-cache')
res.set('Pragma', 'no-cache')
Expand All @@ -273,8 +286,10 @@ export default function slackin ({
socket.emit('data', slack.users)
let change = (key, val) => socket.emit(key, val)
slack.on('change', change)
slack.clientConnected()
socket.on('disconnect', () => {
slack.removeListener('change', change)
slack.clientDisconnected()
})
})

Expand Down
7 changes: 6 additions & 1 deletion lib/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ export default function log (slack, silent){
out('fetching')
})

slack.on('log', (message) => {
out(message)
})

slack.on('data', online)

// log online users
function online (){
out('online %d, total %d %s',
out('online %d, total %d, clients %d %s',
slack.users.active,
slack.users.total,
slack.clientConnectionCount,
last ? `(+${new Date - last}ms)` : '')
}

Expand Down
39 changes: 36 additions & 3 deletions lib/slack.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export default class SlackData extends EventEmitter {
this.ready = false
this.org = {}
this.users = {}
this.clientConnectionCount = 0
this.fetchTimeout = null
this.channelsByName = {}
this.init()
this.fetch()
Expand Down Expand Up @@ -44,7 +46,36 @@ export default class SlackData extends EventEmitter {
})
}

clientConnected() {
if (this.clientConnectionCount == 0) {
this.emit('log', 'restarting fetch')
this.fetch()
}
this.clientConnectionCount++
}

clientDisconnected() {
this.clientConnectionCount--
if (this.clientConnectionCount == 0) {
this.emit('log', 'pausing fetch')
this.stopFetchTimeout()
}
}

stopFetchTimeout() {
if (this.fetchTimeout) {
clearTimeout(this.fetchTimeout)
this.fetchTimeout = null
}
}

retryFetch(interval) {
this.stopFetchTimeout()
this.fetchTimeout = setTimeout(this.fetch.bind(this), interval)
}

fetch (){
this.stopFetchTimeout()
request
.get(`https://${this.host}.slack.com/api/users.list`)
.query({ token: this.token, presence: 1 })
Expand All @@ -59,9 +90,9 @@ export default class SlackData extends EventEmitter {
return channel ? channel.id: null
}

retry (){
retry () {
let interval = this.interval * 2
setTimeout(this.fetch.bind(this), interval)
this.retryFetch(interval)
this.emit('retry')
}

Expand Down Expand Up @@ -107,7 +138,9 @@ export default class SlackData extends EventEmitter {
this.emit('ready')
}

setTimeout(this.fetch.bind(this), this.interval)
if (this.clientConnectionCount > 0) {
this.retryFetch(this.interval)
}
this.emit('data')
}

Expand Down