Skip to content

Instantly share code, notes, and snippets.

@huang47
Created December 20, 2014 22:49
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 huang47/3ef91c4ab37ad41f3113 to your computer and use it in GitHub Desktop.
Save huang47/3ef91c4ab37ad41f3113 to your computer and use it in GitHub Desktop.
var Rx = require('rx');
var profileSwitchSuccess = new Rx.Subject();
var cacheExpiryInHomeOnForeground = new Rx.Subject();
var loginCheckDone = new Rx.Subject();
profileSwitchSuccess.asObservable().
map(function () {
return true;
}).
do(function () {
console.log('profile switch success');
}).
startWith(false).
subscribe(function (isProfileSwitching) {
cacheExpiryInHomeOnForeground.asObservable().
do(function () {
console.log('cache expiry in home on forground');
}).
flatMapLatest(function () {
return loginCheckDone.asObservable().
do(function (isLogin) {
console.log('login check done: is login %s', isLogin);
}).
take(1).
filter(function (isLogin) {
return true === isLogin;
});
}).
takeUntil(profileSwitchSuccess.asObservable()).
subscribe(function () {
isProfileSwitching = undefined;
console.log('!!!!!!!!!!!!!!!!!!!!!!!!! subscribe happen');
}, function () {}, function () {
isProfileSwitching = false;
});
});
cacheExpiryInHomeOnForeground.onNext(true);
setTimeout(function () {
loginCheckDone.onNext(true);
}, 1000);
setTimeout(function () {
profileSwitchSuccess.onNext(true);
}, 2000);
setTimeout(function () {
cacheExpiryInHomeOnForeground.onNext(true);
}, 3000);
setTimeout(function () {
loginCheckDone.onNext(true);
}, 4000);
setTimeout(function () {
profileSwitchSuccess.onNext(true);
}, 5000);
setTimeout(function () {
cacheExpiryInHomeOnForeground.onNext(true);
}, 6000);
setTimeout(function () {
loginCheckDone.onNext(false);
}, 7000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment