import uuid from 'uuid' ; import { ISearchStrategy } from '../../../src/plugins/data/server' ; import { FibonacciRequest , FibonacciResponse } from '../common/types' ; export const fibonacciStrategyProvider = ( ) : ISearchStrategy < FibonacciRequest , FibonacciResponse > => { const responseMap = new Map < string , [ number [ ] , number , number ] > ( ) ; return { search : ( request ) => { const id = request . id ?? uuid ( ) ; const [ sequence , total , started ] = responseMap . get ( id ) ?? [ [ ] , request . params ?. n ?? 0 , Date . now ( ) , ] ; if ( sequence . length < 2 ) { if ( total > 0 ) sequence . push ( sequence . length ) ; } else { const [ a , b ] = sequence . slice ( - 2 ) ; sequence . push ( a + b ) ; } const loaded = sequence . length ; responseMap . set ( id , [ sequence , total , started ] ) ; if ( loaded >= total ) { responseMap . delete ( id ) ; } const isRunning = loaded < total ; const isPartial = isRunning ; const took = Date . now ( ) - started ; const values = sequence . slice ( 0 , loaded ) ; return [ { id , loaded , total , isRunning , isPartial , rawResponse : { took , values } } ] ; } , cancel : async ( id ) => { responseMap . delete ( id ) ; } , } as unknown as ISearchStrategy < FibonacciRequest , FibonacciResponse > ; } ;