@@ -40,6 +40,8 @@ import { ApiStore } from '../api/api-store';
40
40
import { RulesStore } from '../rules/rules-store' ;
41
41
import { findItem , isRuleGroup } from '../rules/rules-structure' ;
42
42
43
+ import { ObservableEventsList } from './observable-events-list' ;
44
+
43
45
import { parseHar } from '../http/har' ;
44
46
45
47
import { FailedTlsConnection } from '../tls/failed-tls-connection' ;
@@ -183,40 +185,16 @@ export class EventsStore {
183
185
}
184
186
}
185
187
186
- readonly events = observable . array < CollectedEvent > ( [ ] , { deep : false } ) ;
187
-
188
- @computed . struct
189
- get exchanges ( ) : Array < HttpExchange > {
190
- return this . events . filter ( ( e ) : e is HttpExchange => e . isHttp ( ) ) ;
191
- }
192
-
193
- @computed . struct
194
- get websockets ( ) : Array < WebSocketStream > {
195
- return this . exchanges . filter ( ( e ) : e is WebSocketStream => e . isWebSocket ( ) ) ;
196
- }
197
-
198
- @computed . struct
199
- get rtcConnections ( ) : Array < RTCConnection > {
200
- return this . events . filter ( ( e ) : e is RTCConnection => e . isRTCConnection ( ) ) ;
201
- }
202
-
203
- @computed . struct
204
- get rtcDataChannels ( ) : Array < RTCDataChannel > {
205
- return this . events . filter ( ( e ) : e is RTCDataChannel => e . isRTCDataChannel ( ) ) ;
206
- }
188
+ readonly eventsList = new ObservableEventsList ( ) ;
207
189
208
- @computed . struct
209
- get rtcMediaTracks ( ) : Array < RTCMediaTrack > {
210
- return this . events . filter ( ( e ) : e is RTCMediaTrack => e . isRTCMediaTrack ( ) ) ;
211
- }
212
-
213
- @computed . struct
214
- get activeSources ( ) {
215
- return _ ( this . exchanges )
216
- . map ( e => e . request . source )
217
- . uniqBy ( s => s . summary )
218
- . value ( ) ;
219
- }
190
+ get events ( ) { return this . eventsList . events ; }
191
+ get exchanges ( ) { return this . eventsList . exchanges ; }
192
+ get websockets ( ) { return this . eventsList . websockets ; }
193
+ get tlsFailures ( ) { return this . eventsList . tlsFailures ; }
194
+ get rtcConnections ( ) { return this . eventsList . rtcConnections ; }
195
+ get rtcDataChannels ( ) { return this . eventsList . rtcDataChannels ; }
196
+ get rtcMediaTracks ( ) { return this . eventsList . rtcMediaTracks ; }
197
+ get activeSources ( ) { return this . eventsList . activeSources ; }
220
198
221
199
@action . bound
222
200
private flushQueuedUpdates ( ) {
@@ -316,11 +294,11 @@ export class EventsStore {
316
294
private addInitiatedRequest ( request : InputInitiatedRequest ) {
317
295
// Due to race conditions, it's possible this request already exists. If so,
318
296
// we just skip this - the existing data will be more up to date.
319
- const existingEventIndex = _ . findIndex ( this . events , { id : request . id } ) ;
320
- if ( existingEventIndex === - 1 ) {
321
- const exchange = new HttpExchange ( this . apiStore , request ) ;
322
- this . events . push ( exchange ) ;
323
- }
297
+ const existingEvent = this . eventsList . getById ( request . id ) ;
298
+ if ( existingEvent ) return ;
299
+
300
+ const exchange = new HttpExchange ( this . apiStore , request ) ;
301
+ this . eventsList . push ( exchange ) ;
324
302
}
325
303
326
304
private getMatchedRule ( request : InputCompletedRequest ) {
@@ -343,23 +321,23 @@ export class EventsStore {
343
321
// are received, and this one later when the full body is received.
344
322
// We add the request from scratch if it's somehow missing, which can happen given
345
323
// races or if the server doesn't support request-initiated events.
346
- const existingEventIndex = _ . findIndex ( this . events , { id : request . id } ) ;
324
+ const existingEvent = this . eventsList . getById ( request . id ) ;
347
325
348
326
let event : HttpExchange ;
349
- if ( existingEventIndex >= 0 ) {
350
- event = this . events [ existingEventIndex ] as HttpExchange ;
327
+ if ( existingEvent ) {
328
+ event = existingEvent as HttpExchange
351
329
} else {
352
330
event = new HttpExchange ( this . apiStore , { ...request } ) ;
353
331
// ^ This mutates request to use it, so we have to shallow-clone to use it below too:
354
- this . events . push ( event ) ;
332
+ this . eventsList . push ( event ) ;
355
333
}
356
334
357
335
event . updateFromCompletedRequest ( request , this . getMatchedRule ( request ) ) ;
358
336
}
359
337
360
338
@action
361
339
private markRequestAborted ( request : InputFailedRequest ) {
362
- const exchange = _ . find ( this . exchanges , { id : request . id } ) ;
340
+ const exchange = this . eventsList . getExchangeById ( request . id ) ;
363
341
364
342
if ( ! exchange ) {
365
343
// Handle this later, once the request has arrived
@@ -372,7 +350,7 @@ export class EventsStore {
372
350
373
351
@action
374
352
private setResponse ( response : InputResponse ) {
375
- const exchange = _ . find ( this . exchanges , { id : response . id } ) ;
353
+ const exchange = this . eventsList . getExchangeById ( response . id ) ;
376
354
377
355
if ( ! exchange ) {
378
356
// Handle this later, once the request has arrived
@@ -389,12 +367,12 @@ export class EventsStore {
389
367
// ^ This mutates request to use it, so we have to shallow-clone to use it below too
390
368
391
369
stream . updateFromCompletedRequest ( request , this . getMatchedRule ( request ) ) ;
392
- this . events . push ( stream ) ;
370
+ this . eventsList . push ( stream ) ;
393
371
}
394
372
395
373
@action
396
374
private addAcceptedWebSocketResponse ( response : InputResponse ) {
397
- const stream = _ . find ( this . websockets , { id : response . id } ) ;
375
+ const stream = this . eventsList . getWebSocketById ( response . id ) ;
398
376
399
377
if ( ! stream ) {
400
378
// Handle this later, once the request has arrived
@@ -408,7 +386,7 @@ export class EventsStore {
408
386
409
387
@action
410
388
private addWebSocketMessage ( message : InputWebSocketMessage ) {
411
- const stream = _ . find ( this . websockets , { id : message . streamId } ) ;
389
+ const stream = this . eventsList . getWebSocketById ( message . streamId ) ;
412
390
413
391
if ( ! stream ) {
414
392
// Handle this later, once the request has arrived
@@ -424,7 +402,7 @@ export class EventsStore {
424
402
425
403
@action
426
404
private markWebSocketClosed ( close : InputWebSocketClose ) {
427
- const stream = _ . find ( this . websockets , { id : close . streamId } ) ;
405
+ const stream = this . eventsList . getWebSocketById ( close . streamId ) ;
428
406
429
407
if ( ! stream ) {
430
408
// Handle this later, once the request has arrived
@@ -439,12 +417,12 @@ export class EventsStore {
439
417
440
418
@action
441
419
private addTlsTunnel ( openEvent : InputTlsPassthrough ) {
442
- this . events . push ( new TlsTunnel ( openEvent ) ) ;
420
+ this . eventsList . push ( new TlsTunnel ( openEvent ) ) ;
443
421
}
444
422
445
423
@action
446
424
private markTlsTunnelClosed ( closeEvent : InputTlsPassthrough ) {
447
- const tunnel = _ . find ( this . events , { id : closeEvent . id } ) as TlsTunnel | undefined ;
425
+ const tunnel = this . eventsList . getTlsTunnelById ( closeEvent . id ) ;
448
426
449
427
if ( ! tunnel ) {
450
428
// Handle this later, once the tunnel open event has arrived
@@ -459,13 +437,12 @@ export class EventsStore {
459
437
460
438
@action
461
439
private addFailedTlsRequest ( request : InputTlsFailure ) {
462
- if ( _ . some ( this . events , ( event ) =>
463
- event . isTlsFailure ( ) &&
464
- event . upstreamHostname === request . hostname &&
465
- event . remoteIpAddress === request . remoteIpAddress
440
+ if ( this . tlsFailures . some ( ( failure ) =>
441
+ failure . upstreamHostname === request . hostname &&
442
+ failure . remoteIpAddress === request . remoteIpAddress
466
443
) ) return ; // Drop duplicate TLS failures
467
444
468
- this . events . push ( new FailedTlsConnection ( request ) ) ;
445
+ this . eventsList . push ( new FailedTlsConnection ( request ) ) ;
469
446
}
470
447
471
448
@action
@@ -497,12 +474,12 @@ export class EventsStore {
497
474
exchange . setResponse ( error . response ) ;
498
475
}
499
476
500
- this . events . push ( exchange ) ;
477
+ this . eventsList . push ( exchange ) ;
501
478
}
502
479
503
480
@action
504
481
private addRuleEvent ( event : InputRuleEvent ) {
505
- const exchange = _ . find ( this . exchanges , { id : event . requestId } ) ;
482
+ const exchange = this . eventsList . getExchangeById ( event . requestId ) ;
506
483
507
484
if ( ! exchange ) {
508
485
// Handle this later, once the request has arrived
@@ -527,12 +504,12 @@ export class EventsStore {
527
504
528
505
@action
529
506
private addRTCPeerConnection ( event : InputRTCPeerConnected ) {
530
- this . events . push ( new RTCConnection ( event ) ) ;
507
+ this . eventsList . push ( new RTCConnection ( event ) ) ;
531
508
}
532
509
533
510
@action
534
511
private attachExternalRTCPeer ( event : InputRTCExternalPeerAttached ) {
535
- const conn = this . rtcConnections . find ( c => c . id === event . sessionId ) ;
512
+ const conn = this . eventsList . getRTCConnectionById ( event . sessionId ) ;
536
513
const otherHalf = this . rtcConnections . find ( c => c . isOtherHalfOf ( event ) ) ;
537
514
538
515
if ( conn ) {
@@ -545,7 +522,7 @@ export class EventsStore {
545
522
546
523
@action
547
524
private markRTCPeerDisconnected ( event : InputRTCPeerDisconnected ) {
548
- const conn = this . rtcConnections . find ( c => c . id === event . sessionId ) ;
525
+ const conn = this . eventsList . getRTCConnectionById ( event . sessionId ) ;
549
526
if ( conn ) {
550
527
conn . markClosed ( event ) ;
551
528
} else {
@@ -555,10 +532,10 @@ export class EventsStore {
555
532
556
533
@action
557
534
private addRTCDataChannel ( event : InputRTCDataChannelOpened ) {
558
- const conn = this . rtcConnections . find ( c => c . id === event . sessionId ) ;
535
+ const conn = this . eventsList . getRTCConnectionById ( event . sessionId ) ;
559
536
if ( conn ) {
560
537
const dc = new RTCDataChannel ( event , conn ) ;
561
- this . events . push ( dc ) ;
538
+ this . eventsList . push ( dc ) ;
562
539
conn . addStream ( dc ) ;
563
540
} else {
564
541
this . orphanedEvents [ event . sessionId ] = { type : 'data-channel-opened' , event } ;
@@ -587,10 +564,10 @@ export class EventsStore {
587
564
588
565
@action
589
566
private addRTCMediaTrack ( event : InputRTCMediaTrackOpened ) {
590
- const conn = this . rtcConnections . find ( c => c . id === event . sessionId ) ;
567
+ const conn = this . eventsList . getRTCConnectionById ( event . sessionId ) ;
591
568
if ( conn ) {
592
569
const track = new RTCMediaTrack ( event , conn ) ;
593
- this . events . push ( track ) ;
570
+ this . eventsList . push ( track ) ;
594
571
conn . addStream ( track ) ;
595
572
} else {
596
573
this . orphanedEvents [ event . sessionId ] = { type : 'media-track-opened' , event } ;
@@ -619,7 +596,7 @@ export class EventsStore {
619
596
620
597
@action . bound
621
598
deleteEvent ( event : CollectedEvent ) {
622
- this . events . remove ( event ) ;
599
+ this . eventsList . remove ( event ) ;
623
600
624
601
if ( event . isRTCDataChannel ( ) || event . isRTCMediaTrack ( ) ) {
625
602
event . rtcConnection . removeStream ( event ) ;
@@ -638,10 +615,10 @@ export class EventsStore {
638
615
clearPinned ? ( ) => false : ( ex ) => ex . pinned
639
616
) ;
640
617
641
- this . events . clear ( ) ;
618
+ this . eventsList . clear ( ) ;
642
619
unpinnedEvents . forEach ( ( event ) => { if ( 'cleanup' in event ) event . cleanup ( ) } ) ;
643
620
644
- this . events . push ( ...pinnedEvents ) ;
621
+ this . eventsList . push ( ...pinnedEvents ) ;
645
622
this . orphanedEvents = { } ;
646
623
647
624
// If GC is exposed (desktop 0.1.22+), trigger it when data is cleared,
@@ -699,7 +676,7 @@ export class EventsStore {
699
676
// ^ This mutates request to use it, so we have to shallow-clone to use it below too:
700
677
exchange . updateFromCompletedRequest ( request , false ) ;
701
678
702
- this . events . push ( exchange ) ;
679
+ this . eventsList . push ( exchange ) ;
703
680
return exchange ;
704
681
}
705
682
0 commit comments