ix
Version:
The Interactive Extensions for JavaScript
37 lines (35 loc) • 1.3 kB
JavaScript
import { __asyncGenerator, __await } from "tslib";
import { AsyncSink } from './asyncsink.mjs';
import { memoize } from './operators/memoize.mjs';
/**
* Converts the callback function into wrapped function which returns an async-iterable.
*
* @template TSource The type of the value returned from the callback.
* @param {Function} func The callback function to wrap as an async-iterable.
* @returns {(...args: any[]) => AsyncIterableX<TSource>} A function when invoked, returns an async-iterable from the callback.
*/
export function asyncify(func) {
return function (...args) {
const sink = new AsyncSink();
const handler = function (...innerArgs) {
sink.write(innerArgs.length === 1 ? innerArgs[0] : innerArgs);
sink.end();
};
try {
func(...args.concat(handler));
}
catch (e) {
sink.error(e);
sink.end();
}
const yielder = function () {
return __asyncGenerator(this, arguments, function* () {
for (let next; !(next = yield __await(sink.next())).done;) {
yield yield __await(next.value);
}
});
};
return memoize()(yielder());
};
}
//# sourceMappingURL=asyncify.mjs.map