@lifaon/rx-js-light
Version:
Blazing fast Observables
40 lines (39 loc) • 1.01 kB
JavaScript
import {asyncUnsubscribe} from "../../../../../misc/helpers/async-unsubscribe.mjs";
export function thenObservable(subscribe, onFulfilled, onRejected) {
return emit => {
let running = true;
let childUnsubscribe;
let lastValue;
const end = () => {
if (running) {
asyncUnsubscribe(() => unsubscribe);
} else {
childUnsubscribe();
}
};
const unsubscribe = subscribe(notification => {
switch (notification.name) {
case 'next':
lastValue = notification.value;
break;
case 'complete':
childUnsubscribe = onFulfilled(lastValue)(emit);
end();
break;
case 'error':
childUnsubscribe = onRejected(notification.value)(emit);
end();
break;
}
});
return () => {
if (running) {
running = false;
unsubscribe();
if (childUnsubscribe !== void 0) {
childUnsubscribe();
}
}
};
};
}