@@ -84,25 +84,44 @@ export default class Resource {
8484 const timer = new Timer ( options ?. timeoutMs ?? 30e3 ) ;
8585
8686 const resourceFilter = { url : filter . url , type : filter . type } ;
87- const resourceOptions = {
88- ... ( options ?? { } ) ,
87+ const resourceOptions : IWaitForResourceOptions = {
88+ sinceCommandId : options ?. sinceCommandId ,
8989 timeoutMs : 2e3 ,
9090 throwIfTimeout : false ,
91- } as IWaitForResourceOptions ;
91+ } ;
9292
9393 let isComplete = false ;
9494 const done = ( ) : boolean => ( isComplete = true ) ;
9595
9696 do {
97- let foundResources : IResourceMeta [ ] = [ ] ;
98-
9997 try {
10098 const waitForResourcePromise = coreTab . waitForResource ( resourceFilter , resourceOptions ) ;
101- foundResources = await timer . waitForPromise (
99+ const foundResources = await timer . waitForPromise (
102100 waitForResourcePromise ,
103101 'Timeout waiting for Resource(s)' ,
104102 ) ;
105103 resourceOptions . sinceCommandId = coreTab . commandQueue . lastCommandId ;
104+
105+ for ( const resourceMeta of foundResources ) {
106+ if ( idsSeen . has ( resourceMeta . id ) ) continue ;
107+ idsSeen . add ( resourceMeta . id ) ;
108+
109+ const resource = createResource ( resourceMeta , Promise . resolve ( coreTab ) ) ;
110+
111+ let shouldInclude = true ;
112+
113+ if ( filter . filterFn ) {
114+ // resources can trigger commandQueue functions, so time them out
115+ shouldInclude = await timer . waitForPromise (
116+ Promise . resolve ( filter . filterFn ( resource , done ) ) ,
117+ 'Timeout waiting for waitResource.filterFn' ,
118+ ) ;
119+ }
120+
121+ if ( shouldInclude ) resources . push ( resource ) ;
122+
123+ if ( isComplete ) break ;
124+ }
106125 } catch ( err ) {
107126 if ( err instanceof TimeoutError ) {
108127 if ( options ?. throwIfTimeout === false ) {
@@ -112,22 +131,6 @@ export default class Resource {
112131 throw err ;
113132 }
114133
115- for ( const resourceMeta of foundResources ) {
116- if ( idsSeen . has ( resourceMeta . id ) ) continue ;
117- idsSeen . add ( resourceMeta . id ) ;
118-
119- const resource = createResource ( resourceMeta , Promise . resolve ( coreTab ) ) ;
120-
121- if ( filter . filterFn ) {
122- const shouldIncludeResource = await filter . filterFn ( resource , done ) ;
123- if ( shouldIncludeResource ) resources . push ( resource ) ;
124- } else {
125- resources . push ( resource ) ;
126- }
127-
128- if ( isComplete ) break ;
129- }
130-
131134 // if no filter callback provided, break after 1 found
132135 if ( ! filter . filterFn && resources . length ) {
133136 done ( ) ;
0 commit comments