@lifaon/rx-js-light
Version:
Blazing fast Observables
30 lines (29 loc) • 860 B
JavaScript
import {createDeferredPromise} from "../../../../../misc/helpers/create-deferred-promise.mjs";
export async function* toAsyncIterable(subscribe) {
const notifications = [];
let notificationPromise = createDeferredPromise();
const unsubscribe = subscribe(notification => {
notifications.push(notification);
notificationPromise.resolve();
});
try {
while (true) {
await notificationPromise.promise;
notificationPromise = createDeferredPromise();
while (notifications.length > 0) {
const notification = notifications.shift();
switch (notification.name) {
case 'next':
yield notification.value;
break;
case 'complete':
return;
case 'error':
throw notification.value;
}
}
}
} finally {
unsubscribe();
}
}