import { Operator , Stream , InternalListener } from 'xstream' ; const NO = { } ; class PeekListener < T , U > implements InternalListener < U > { constructor ( private p ) { } _n ( u ) { const p = this . p ; if ( p . out === NO ) { return ; } p . lastVal = u ; } _e ( err ) { this . p . _e ( err ) ; } _c ( ) { this . p . peekStream . _remove ( this ) ; } } class PeekOperator < T , U > implements Operator < T , U > { public type = 'peek' ; ins : Stream < T > ; out : Stream < U > = NO as any ; lastVal = NO as U ; peekStream : Stream < U > = NO as any ; il : PeekListener < T , U > ; constructor ( ins , peekStream ) { this . ins = ins ; this . peekStream = peekStream ; } _start ( out ) { this . out = out ; this . il = new PeekListener ( this ) ; this . peekStream . _add ( this . il ) ; this . ins . _add ( this ) ; } _stop ( ) { this . ins . _remove ( this ) ; this . peekStream . _remove ( this . il ) ; this . out = null ; } _n ( t ) { if ( this . out === NO ) { return ; } if ( this . lastVal === NO ) { return ; } this . out . _n ( this . lastVal ) ; } _e ( err ) { if ( this . out ) { this . out . _e ( err ) ; } } _c ( ) { if ( this . out ) { this . out . _c ( ) ; } } } export default function peek < T , U > ( peekStream ) { return function peekOperator ( ins ) { return new Stream < U > ( new PeekOperator < T , U > ( ins , peekStream ) ) ; } ; }