Skip to content

Instantly share code, notes, and snippets.

@simoneb
Last active February 15, 2016 22:42
Show Gist options
  • Save simoneb/b946a254cdd48813719b to your computer and use it in GitHub Desktop.
Save simoneb/b946a254cdd48813719b to your computer and use it in GitHub Desktop.
repro
let stream;
let events = ['message', 'tweet', 'delete', 'limit', 'scrub_geo', 'disconnect',
'connect', 'connected', 'reconnect', 'warning', 'status_withheld',
'user_withheld', 'friends', 'direct_message', 'user_event'];
function start(path, params) {
if (stream) stream.stop();
let T = new Twit({
consumer_key: ...,
consumer_secret: ...,
access_token: ...,
access_token_secret: ...,
timeout_ms: 60 * 1000 // optional HTTP request timeout to apply to all requests.
});
subscribe(stream = T.stream(path, params));
disableReconnectWhenStopped(stream);
}
function subscribe() {
events.forEach(event =>
stream.on(event, msg =>
$rootScope.$apply(() =>
$rootScope.$broadcast('twitter:' + event, msg))));
}
function disableReconnectWhenStopped(_stream) {
_stream.on('reconnect', () => {
if (!stream) _stream.stop();
});
}
function stop() {
if (stream) {
stream.stop();
stream = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment