import { Observable , Subject } from 'rxjs' ; /**
 * Creates observable from streaming XMLHttpRequest, where each event
 * corresponds to a streamed chunk.
 */ export const fromStreamingXhr = ( xhr , signal ? ) : Observable < string > => { const subject = new Subject < string > ( ) ; let index = 0 ; let aborted = false ; const processBatch = ( ) => { if ( aborted ) return ; const { responseText } = xhr ; if ( index >= responseText . length ) return ; subject . next ( responseText . substr ( index ) ) ; index = responseText . length ; } ; xhr . onprogress = processBatch ; const onBatchAbort = ( ) => { if ( xhr . readyState !== 4 ) { aborted = true ; xhr . abort ( ) ; subject . complete ( ) ; if ( signal ) signal . removeEventListener ( 'abort' , onBatchAbort ) ; } } ; if ( signal ) signal . addEventListener ( 'abort' , onBatchAbort ) ; xhr . onreadystatechange = ( ) => { if ( aborted ) return ; processBatch ( ) ; if ( xhr . readyState === 4 ) { if ( signal ) signal . removeEventListener ( 'abort' , onBatchAbort ) ; if ( xhr . status === 0 || xhr . status >= 400 ) { subject . error ( new Error ( `Batch request failed with status ${ xhr . status }` ) ) ; } else { subject . complete ( ) ; } } } ; return subject ; } ;