Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move pubsub topic manager to a separate file
- Loading branch information
Showing
3 changed files
with
100 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,51 @@ | ||
const Promise = require("bluebird"); | ||
|
||
const TwitchPubSubConnection = require("./src/twitch-pubsub-connection"); | ||
const TwitchPubSubConnection = require("./src/twitch/pubsub-connection"); | ||
const TwitchPubSubManager = require("./src/twitch/pubsub-manager"); | ||
|
||
// TODO: better token/config handling. | ||
const twitchWebSocketUri = "wss://pubsub-edge.twitch.tv/"; | ||
const twitchAppAccessToken = process.env["TWITCH_APP_ACCESS_TOKEN"]; | ||
const twitchUserAccessToken = process.env["TWITCH_USER_ACCESS_TOKEN"]; | ||
const twitchChannelId = 148460096; | ||
const MAX_LISTEN_TIME_MILLISECONDS = 5 * 60 * 1000; | ||
const MAX_LISTEN_TIME_MILLISECONDS = 5 * 1000; | ||
|
||
const twitchPubSubConnection = new TwitchPubSubConnection(twitchWebSocketUri); | ||
const twitchPubSubManager = new TwitchPubSubManager(twitchPubSubConnection, twitchChannelId, twitchUserAccessToken); | ||
|
||
twitchPubSubConnection.connect().then(() => { | ||
console.log("Connected."); | ||
Promise.resolve().then(() => twitchPubSubConnection.connect()).then(() => { | ||
const disconnect = (error) => twitchPubSubConnection.disconnect().then(() => { | ||
if (error) { | ||
console.error("Disconnected.", error); | ||
} else { | ||
console.log("Disconnected."); | ||
} | ||
|
||
const disconnect = () => twitchPubSubConnection.disconnect().then(() => { | ||
console.log("Disconnected."); | ||
return undefined; | ||
}); | ||
|
||
return Promise.resolve().then(() => { | ||
const topics = [`channel-bits-events-v1.${twitchChannelId}`, `channel-subscribe-events-v1.${twitchChannelId}`, `channel-commerce-events-v1.${twitchChannelId}`, `whispers.${twitchChannelId}`]; | ||
const dataHandler = (topic, data) => { | ||
console.log("dataHandler", topic, JSON.stringify(data, null, 2)); | ||
}; | ||
return Promise.resolve().then(() => twitchPubSubManager.start()).then(() => { | ||
console.log("Started."); | ||
|
||
return twitchPubSubConnection.listen(twitchUserAccessToken, topics, dataHandler).then((killSwitch) => { | ||
console.log(`Listening for ${MAX_LISTEN_TIME_MILLISECONDS} milliseconds.`); | ||
const stop = (error) => twitchPubSubManager.stop().then(() => { | ||
if (error) { | ||
console.error("Stopped.", error); | ||
} else { | ||
console.log("Stopped."); | ||
} | ||
|
||
return Promise.delay(MAX_LISTEN_TIME_MILLISECONDS).then(() => killSwitch(), () => killSwitch()); | ||
return undefined; | ||
}); | ||
}).then(() => disconnect(), (error) => { | ||
console.error("Error.", error); | ||
|
||
return disconnect(); | ||
}).then(() => { | ||
process.exit(); | ||
}); | ||
console.log(`Online for ${MAX_LISTEN_TIME_MILLISECONDS} milliseconds.`); | ||
|
||
// TODO: perform more work here. | ||
return Promise.delay(MAX_LISTEN_TIME_MILLISECONDS).then(() => stop(), (error) => stop(error)); | ||
}).then(() => disconnect(), (error) => disconnect(error)) | ||
}).then(() => { | ||
process.exit(); | ||
}, (error) => { | ||
console.log("Error.", error); | ||
|
||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const Promise = require("bluebird"); | ||
|
||
module.exports = class PubSubManager { | ||
constructor(pubSubConnection, channelId, userAccessToken) { | ||
this._pubSubConnection = pubSubConnection; | ||
this._channelId = channelId; | ||
this._userAccessToken = userAccessToken; | ||
|
||
// TODO: one class per listen-topic, or one class per concern? | ||
this._topics = [`channel-bits-events-v1.${this._channelId}`, `channel-subscribe-events-v1.${this._channelId}`, `channel-commerce-events-v1.${this._channelId}`, `whispers.${this._channelId}`]; | ||
|
||
this._killSwitch = null; | ||
} | ||
|
||
start() { | ||
return this._pubSubConnection.listen(this._userAccessToken, this._topics, this._dataHandler.bind(this)).then((killSwitch) => { | ||
this._killSwitch = killSwitch; | ||
}).tapCatch(() => this._executeKillSwitch()); | ||
} | ||
|
||
stop() { | ||
// TODO: assert killSwitch? | ||
return Promise.try(() => { | ||
if (typeof this._killSwitch === "function") { | ||
this._executeKillSwitch(); | ||
} | ||
}); | ||
} | ||
|
||
_dataHandler(topic, data) { | ||
console.log("dataHandler", topic, JSON.stringify(data, null, 2)); | ||
} | ||
|
||
_executeKillSwitch() { | ||
return Promise.try(() => { | ||
const killSwitch = this._killSwitch; | ||
this._killSwitch = null; | ||
killSwitch(); | ||
}); | ||
} | ||
} |