Forked from mattpodwysocki/gist:e99bcf4ae092fae569d3
Last active
May 6, 2024 03:20
-
-
Save jhusain/c8b7703e60bdd2a8fd5f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
* Performs a exclusive waiting for the first to finish before subscribing to another observable. | |
* Observables that come in between subscriptions will be dropped on the floor. | |
* @returns {Observable} A exclusive observable with only the results that happen when subscribed. | |
*/ | |
observableProto.exclusive = function () { | |
var sources = this; | |
return new AnonymousObservable(function (observer) { | |
var hasCurrent = false, | |
isStopped = false, | |
m = new SingleAssignmentDisposable(), | |
g = new CompositeDisposable(); | |
g.add(m); | |
m.setDisposable(sources.subscribe( | |
function (innerSource) { | |
if (!hasCurrent) { | |
hasCurrent = true; | |
isPromise(innerSource) && (innerSource = observableFromPromise(innerSource)); | |
var innerSubscription = new SingleAssignmentDisposable(); | |
g.add(innerSubscription); | |
innerSubscription.setDisposable(innerSource.subscribe( | |
observer.onNext.bind(observer), | |
function(e) { | |
hasCurrent = false; | |
observer.onError(e); | |
}, | |
function () { | |
g.remove(innerSubscription); | |
hasCurrent = false; | |
if (isStopped && g.length === 1) { | |
observer.onCompleted(); | |
} | |
)); | |
} | |
}, | |
observer.onError.bind(observer), | |
function () { | |
hasCurrent = false; | |
isStopped = true; | |
if (g.length === 1) { | |
observer.onCompleted(); | |
} | |
})); | |
return g; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment