Skip to content

Instantly share code, notes, and snippets.

@monkey-codes
Created November 22, 2016 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monkey-codes/4ab8ec54111295faeb7fe31844f28d68 to your computer and use it in GitHub Desktop.
Save monkey-codes/4ab8ec54111295faeb7fe31844f28d68 to your computer and use it in GitHub Desktop.
rxjs - Example of Pull vs Push
//Traditional imperative pull based
window.setInterval(() => {
const tweets = getNewTweets();
for(let tweet of tweets){
if(!tweet.message.contains("Greetings!")) continue;
getUserAvatar(tweet.user)
.then(avatar => {
const $avatar = $(`<img />`);
$avatar.attr('src', avatar.url);
$avatars.append($avatar);
});
}
}, 5000);
// Reactive push based
observeTweets()
.filter(s => s.message.contains('Greetings!'))
.flatMap(s => getUserAvatar(s.user))
.map( a => $('< img/>').attr('src', a.url))
.subscribe($avatar => {
$avatars.append($avatar);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment