@@ -65,6 +65,10 @@ class Recorder {
6565 this . id = 1 ;
6666 this . sessionSet = new Set ( ) ;
6767
68+ this . _cachePageInfo = null ;
69+ this . _cacheSessionNew = 0 ;
70+ this . _cacheSessionTotal = 0 ;
71+
6872 this . behaviorInitStr = JSON . stringify ( {
6973 autofetch : true ,
7074 autoplay : true ,
@@ -147,18 +151,24 @@ class Recorder {
147151 await this . _stop ( domNodes ) ;
148152 }
149153
150- _stop ( domNodes = null ) {
151- clearInterval ( this . _updateId ) ;
152- clearInterval ( this . _cleanupId ) ;
154+ async _stop ( domNodes = null ) {
155+ clearInterval ( this . _updateStatusId ) ;
156+ clearInterval ( this . _loopId ) ;
153157
154158 this . flushPending ( ) ;
155159 this . running = false ;
156160 this . pendingRequests = { } ;
157161 this . numPending = 0 ;
158162
159- this . _doStop ( ) ;
163+ await this . commitPage ( this . pageInfo , domNodes , true ) ;
164+
165+ if ( this . _cleaningUp ) {
166+ await this . _cleanupStaleWait ;
167+ } else {
168+ await this . doUpdateLoop ( ) ;
169+ }
160170
161- return this . commitPage ( this . pageInfo , domNodes , true ) ;
171+ this . _doStop ( ) ;
162172 }
163173
164174 async attach ( ) {
@@ -172,24 +182,54 @@ class Recorder {
172182 this . running = true ;
173183 this . stopping = false ;
174184
175- this . _updateId = setInterval ( ( ) => this . updateStatus ( ) , 1000 ) ;
185+ this . _cachePageInfo = null ;
186+ this . _cacheSessionNew = 0 ;
187+ this . _cacheSessionTotal = 0 ;
188+ this . _cleaningUp = false ;
189+ this . _cleanupStaleWait = null ;
176190
177- this . _cleanupId = setInterval ( ( ) => this . cleanupStale ( ) , 10000 ) ;
191+ this . _updateStatusId = setInterval ( ( ) => this . updateStatus ( ) , 1000 ) ;
192+
193+ this . _loopId = setInterval ( ( ) => this . updateLoop ( ) , 10000 ) ;
178194 } ;
179195
180- cleanupStale ( ) {
181- for ( const key of Object . keys ( this . pendingRequests ) ) {
182- const reqresp = this . pendingRequests [ key ] ;
196+ updateLoop ( ) {
197+ if ( ! this . _cleaningUp ) {
198+ this . _cleanupStaleWait = this . doUpdateLoop ( ) ;
199+ }
200+ }
183201
184- if ( ( new Date ( ) - reqresp . _created ) > 20000 ) {
185- if ( this . noResponseForStatus ( reqresp . status ) ) {
186- console . log ( "Dropping stale: " + key ) ;
187- } else {
188- console . log ( `Committing stale ${ reqresp . status } ${ reqresp . url } ` ) ;
189- this . fullCommit ( reqresp , [ ] ) ;
202+ async doUpdateLoop ( ) {
203+ this . _cleaningUp = true ;
204+
205+ try {
206+ for ( const key of Object . keys ( this . pendingRequests ) ) {
207+ const reqresp = this . pendingRequests [ key ] ;
208+
209+ if ( ( new Date ( ) - reqresp . _created ) > 20000 ) {
210+ if ( this . noResponseForStatus ( reqresp . status ) ) {
211+ console . log ( "Dropping stale: " + key ) ;
212+ } else {
213+ console . log ( `Committing stale ${ reqresp . status } ${ reqresp . url } ` ) ;
214+ await this . fullCommit ( reqresp , [ ] ) ;
215+ }
216+ delete this . pendingRequests [ key ] ;
190217 }
191- delete this . pendingRequests [ key ] ;
192218 }
219+
220+ if ( this . _cachePageInfo ) {
221+ await this . _doAddPage ( this . _cachePageInfo ) ;
222+ this . _cachePageInfo = null ;
223+ }
224+
225+ if ( this . _cacheSessionTotal > 0 ) {
226+ await this . _doIncSizes ( this . _cacheSessionTotal , this . _cacheSessionNew ) ;
227+ this . _cacheSessionTotal = 0 ;
228+ this . _cacheSessionNew = 0 ;
229+ }
230+
231+ } finally {
232+ this . _cleaningUp = false ;
193233 }
194234 }
195235
@@ -605,18 +645,28 @@ class Recorder {
605645
606646 currPage . finished = finished ;
607647
608- return this . _doAddPage ( currPage ) ;
648+ const res = this . _doAddPage ( currPage ) ;
649+ if ( currPage === this . _cachePageInfo ) {
650+ this . _cachePageInfo = null ;
651+ }
652+ return res ;
609653 }
610654
611- commitResource ( data , pageInfo ) {
655+ async commitResource ( data , pageInfo ) {
612656 const payloadSize = data . payload . length ;
613657 pageInfo = pageInfo || this . pageInfo ;
614658 pageInfo . size += payloadSize ;
615659
616660 this . sizeTotal += payloadSize ;
617661 this . numUrls ++ ;
618662
619- this . _doAddResource ( data ) . then ( ( writtenSize ) => this . sizeNew += writtenSize ) ;
663+ const writtenSize = await this . _doAddResource ( data ) ;
664+
665+ this . sizeNew += writtenSize ;
666+
667+ this . _cachePageInfo = pageInfo ;
668+ this . _cacheSessionTotal += payloadSize ;
669+ this . _cacheSessionNew += writtenSize ;
620670 }
621671
622672 receiveMessageFromTarget ( params , sessions ) {
@@ -965,7 +1015,7 @@ class Recorder {
9651015 }
9661016
9671017 async fullCommit ( reqresp , sessions ) {
968- const requestId = reqresp . requestId ;
1018+ // const requestId = reqresp.requestId;
9691019
9701020 // let doneResolve;
9711021
0 commit comments