import Deferred from '../Deferred' ; import { PushAdapter , doneResult } from '../common' ; import fromDom from '../fromDom' ; import fromEmitter from '../fromEmitter' ; export default class LastResult < A > implements PushAdapter < A > { private buffer : Deferred < IteratorResult < A > > = new Deferred ( ) ; private closed = false ; private resolved = false ; private requested = false ; static fromDom = fromDom ( ( ) => new LastResult ( ) ) ; static fromEmitter = fromEmitter ( ( ) => new LastResult ( ) ) ; push ( value , done = false ) { if ( this . closed ) { throw Error ( 'Iterator closed' ) ; } const result = { value , done , } ; if ( this . resolved === false ) { this . resolved = true ; } else { this . buffer = new Deferred ( ) ; this . resolved = false ; } this . requested = false ; this . buffer . resolve ( result ) ; return this . buffer . promise ; } async next ( ) { if ( this . closed ) { return doneResult ; } this . requested = true ; return this . buffer . promise ; } async return ( value ? ) { this . closed = true ; if ( ! this . resolved && this . requested ) { this . buffer . resolve ( doneResult ) ; } return Promise . resolve ( { value : value ! , done : true , } ) ; } wrap ( onReturn ? ) { const wrapped = { next : ( ) => this . next ( ) , [ Symbol . asyncIterator ] ( ) { return this ; } , return : ( value ? ) => { if ( onReturn ) { onReturn ( ) ; } return this . return ( value ) ; } , } ; return wrapped ; } [ Symbol . asyncIterator ] ( ) { return this ; } }