Last active
October 18, 2017 04:41
-
-
Save mattpodwysocki/54540674352625f77a27f22e35aaee96 to your computer and use it in GitHub Desktop.
This file contains 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
import { AsyncIterableX } from '../asynciterable'; | |
import { AsyncSink } from '../asyncsink'; | |
import { memoize } from './memoize'; | |
export function asyncify<TSource>( | |
func: Function | |
): (...args: any[]) => AsyncIterableX<TSource> { | |
return function(...args: any[]) { | |
const sink = new AsyncSink<TSource>(); | |
const handler = function(...innerArgs: any[]) { | |
sink.write(<any>innerArgs); | |
}; | |
try { | |
func(...args.concat(handler)); | |
} catch (e) { | |
sink.error(e); | |
} | |
return memoize( | |
(async function*() { | |
for (let next; !(next = await sink.next()).done; ) { | |
yield next.value; | |
} | |
sink.end(); | |
})() | |
); | |
}; | |
} |
This file contains 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
import { AsyncIterableX } from '../asynciterable'; | |
import { AsyncSink } from '../asyncsink'; | |
import { memoize } from './memoize'; | |
export function asyncifyErrback<TSource>( | |
func: Function | |
): (...args: any[]) => AsyncIterableX<TSource> { | |
return function(...args: any[]) { | |
const sink = new AsyncSink<TSource>(); | |
const handler = function(err: any, ...innerArgs: any[]) { | |
if (err) { | |
sink.error(err); | |
} else { | |
sink.write(<any>innerArgs); | |
} | |
}; | |
try { | |
func(...args.concat(handler)); | |
} catch (e) { | |
sink.error(e); | |
} | |
return memoize( | |
(async function*() { | |
for (let next; !(next = await sink.next()).done; ) { | |
yield next.value; | |
} | |
sink.end(); | |
})() | |
); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment