import { Returnable , donePromise } from './common' ; /**
 * Convert a simple callback-taking function to an async stream.
 *
 * Example:
 * ```js
 * const animationFrames = wrapRequest(window.requestAnimationFrame);
 * ```
 *
 */ const wrapRequest = < A , B > ( request , onReturn ? ) : Returnable < A > => { const done = false ; let promise = null ; let cancel = null ; let closed = false ; let result ; return { next ( ) { if ( closed ) { return donePromise ; } if ( promise === null ) { promise = new Promise ( ( resolve , reject ) => { result = request ( ( value ) => { resolve ( { value , done } ) ; promise = null ; } ) ; cancel = reject ; } ) ; } return promise ; } , async return ( value ? ) { closed = true ; if ( cancel ) { cancel ( new Error ( 'Canceled' ) ) ; cancel = null ; } if ( onReturn ) { onReturn ( result ) ; } return { value : value ! , done : true } ; } , [ Symbol . asyncIterator ] ( ) { return this ; } , } ; } ; export default wrapRequest ;