import { Subject } from 'rxjs' ; import { Plugin , CoreSetup , CoreStart } from '../../../src/core/server' ; import { BfetchServerSetup , BfetchServerStart } from '../../../src/plugins/bfetch/server' ; export interface BfetchExplorerSetupPlugins { bfetch : BfetchServerSetup ; } export interface BfetchExplorerStartPlugins { bfetch : BfetchServerStart ; } export class BfetchExplorerPlugin implements Plugin { public setup ( core , plugins ) { plugins . bfetch . addStreamingResponseRoute < string , string > ( '/bfetch_explorer/count' , ( ) => ( { getResponseStream : ( { data } ) => { const subject = new Subject < string > ( ) ; const countTo = Number ( data ) ; for ( let cnt = 1 ; cnt <= countTo ; cnt ++ ) { setTimeout ( ( ) => { subject . next ( String ( cnt ) ) ; } , cnt * 1000 ) ; } setTimeout ( ( ) => { subject . complete ( ) ; } , countTo * 1000 ) ; return subject ; } , } ) ) ; plugins . bfetch . addBatchProcessingRoute < { num : number ; } , { num : number ; } > ( '/bfetch_explorer/double' , ( ) => ( { onBatchItem : async ( { num } ) => { if ( num < 0 ) throw new Error ( 'Invalid number' ) ; await new Promise ( ( r ) => setTimeout ( r , num ) ) ; return { num : 2 * num } ; } , } ) ) ; } public start ( core , plugins ) { } public stop ( ) { } }