@@ -38,6 +38,7 @@ import {
3838import { shouldIgnoreCapture } from './captureIgnores'
3939import { filePersistence , getPersistence } from './persistence/isomorphic'
4040import { createHumanLog } from './errors'
41+ import { getLimitedCaptures } from './captureLimits'
4142
4243const loadedCaptures = new Map < string , CaptureDecryptedAndRevived | undefined > ( )
4344
@@ -215,77 +216,13 @@ export const liveFlytrapStorage: FlytrapStorage = {
215216
216217 // Handle capture amount limits
217218 if ( captureAmountLimit ) {
218- const parsedCaptureAmountLimit = parseCaptureAmountLimit ( captureAmountLimit )
219- if ( parsedCaptureAmountLimit . err ) {
220- // @todo : human-friendly error
221- console . error ( parsedCaptureAmountLimit . val )
219+ const limitedCapturesResult = getLimitedCaptures ( calls , functions , captureAmountLimit )
220+
221+ if ( limitedCapturesResult . err ) {
222+ console . error ( limitedCapturesResult . val )
222223 } else {
223- functions = sortCapturesByTimestamp ( functions )
224- calls = sortCapturesByTimestamp ( calls )
225-
226- const combinedFunctionsAndCalls = [ ...functions , ...calls ]
227- const sortedCombined = sortCapturesByTimestamp ( combinedFunctionsAndCalls )
228-
229- if ( parsedCaptureAmountLimit . val . type === 'files' ) {
230- const fileNamesSet = new Set < string > ( )
231-
232- for ( let i = 0 ; i < sortedCombined . length ; i ++ ) {
233- const currentFunctionOrCall = sortedCombined . at ( - ( i + 1 ) )
234- if ( ! currentFunctionOrCall ) {
235- return
236- }
237-
238- const filepath = parseFilepathFromFunctionId ( currentFunctionOrCall . id ) . unwrap ( )
239- fileNamesSet . add ( filepath )
240- if ( fileNamesSet . size >= parsedCaptureAmountLimit . val . fileLimit ) {
241- break
242- }
243- }
244-
245- // Use the filepaths of N latest to filter calls and functions
246- calls = calls . filter ( ( call ) => {
247- const parsedFilepath = parseFilepathFromFunctionId ( call . id ) . unwrap ( )
248- if ( fileNamesSet . has ( parsedFilepath ) ) {
249- return true
250- }
251- return false
252- } )
253-
254- functions = functions . filter ( ( func ) => {
255- const parsedFilepath = parseFilepathFromFunctionId ( func . id ) . unwrap ( )
256- if ( fileNamesSet . has ( parsedFilepath ) ) {
257- return true
258- }
259- return false
260- } )
261- } else {
262- const duplicateCalls : typeof calls = [ ]
263- const duplicateFunctions : typeof functions = [ ]
264-
265- let sizeCounter = 0
266-
267- for ( let i = 0 ; i < Math . max ( calls . length , functions . length ) ; i ++ ) {
268- if ( sizeCounter >= parsedCaptureAmountLimit . val . sizeLimit ) {
269- break
270- }
271-
272- const callEntry = calls . at ( - ( i + 1 ) )
273- const functionEntry = functions . at ( - ( i + 1 ) )
274-
275- if ( callEntry ) {
276- duplicateCalls . push ( callEntry )
277- sizeCounter += getCaptureSize ( callEntry )
278- }
279-
280- if ( functionEntry ) {
281- duplicateFunctions . push ( functionEntry )
282- sizeCounter += getCaptureSize ( functionEntry )
283- }
284- }
285-
286- calls = duplicateCalls
287- functions = duplicateFunctions
288- }
224+ calls = limitedCapturesResult . val . calls
225+ functions = limitedCapturesResult . val . functions
289226 }
290227 }
291228
0 commit comments