1
1
import { EventEmitter } from 'events'
2
2
import { raf } from './raf'
3
3
4
- const BATCH_DURATION = 100
4
+ const BATCH_DURATION = 200
5
5
6
6
export class Bridge extends EventEmitter {
7
7
wall : any // @TODO
8
8
_batchingQueue : any [ ] // @TODO
9
9
_sendingQueue : any [ ] [ ] // @TODO
10
10
_receivingQueue : any [ ] // @TODO
11
11
_sending : boolean
12
- _time : number
13
12
_timer : NodeJS . Timeout
14
13
15
14
constructor ( wall ) {
@@ -27,7 +26,6 @@ export class Bridge extends EventEmitter {
27
26
this . _sendingQueue = [ ]
28
27
this . _receivingQueue = [ ]
29
28
this . _sending = false
30
- this . _time = null
31
29
}
32
30
33
31
on ( event : string | symbol , listener : ( ...args : any [ ] ) => void ) : this {
@@ -43,31 +41,13 @@ export class Bridge extends EventEmitter {
43
41
}
44
42
45
43
send ( event : string , payload ?: any ) {
46
- if ( Array . isArray ( payload ) ) {
47
- const lastIndex = payload . length - 1
48
- payload . forEach ( ( chunk , index ) => {
49
- this . _send ( {
50
- event,
51
- _chunk : chunk ,
52
- last : index === lastIndex ,
53
- } )
54
- } )
55
- this . _flush ( )
56
- } else if ( this . _time === null ) {
57
- this . _send ( [ { event, payload } ] )
58
- this . _time = Date . now ( )
59
- } else {
60
- this . _batchingQueue . push ( {
61
- event,
62
- payload,
63
- } )
44
+ this . _batchingQueue . push ( {
45
+ event,
46
+ payload,
47
+ } )
64
48
65
- const now = Date . now ( )
66
- if ( now - this . _time > BATCH_DURATION ) {
67
- this . _flush ( )
68
- } else {
69
- this . _timer = setTimeout ( ( ) => this . _flush ( ) , BATCH_DURATION )
70
- }
49
+ if ( this . _timer == null ) {
50
+ this . _timer = setTimeout ( ( ) => this . _flush ( ) , BATCH_DURATION )
71
51
}
72
52
}
73
53
@@ -82,8 +62,8 @@ export class Bridge extends EventEmitter {
82
62
_flush ( ) {
83
63
if ( this . _batchingQueue . length ) this . _send ( this . _batchingQueue )
84
64
clearTimeout ( this . _timer )
65
+ this . _timer = null
85
66
this . _batchingQueue = [ ]
86
- this . _time = null
87
67
}
88
68
89
69
// @TODO types
0 commit comments