Skip to content

Commit 503b5ae

Browse files
committed
fix: performance issue in bridge
related: #1875
1 parent 96a4f79 commit 503b5ae

File tree

1 file changed

+8
-28
lines changed

1 file changed

+8
-28
lines changed

packages/shared-utils/src/bridge.ts

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import { EventEmitter } from 'events'
22
import { raf } from './raf'
33

4-
const BATCH_DURATION = 100
4+
const BATCH_DURATION = 200
55

66
export class Bridge extends EventEmitter {
77
wall: any // @TODO
88
_batchingQueue: any[] // @TODO
99
_sendingQueue: any[][] // @TODO
1010
_receivingQueue: any[] // @TODO
1111
_sending: boolean
12-
_time: number
1312
_timer: NodeJS.Timeout
1413

1514
constructor (wall) {
@@ -27,7 +26,6 @@ export class Bridge extends EventEmitter {
2726
this._sendingQueue = []
2827
this._receivingQueue = []
2928
this._sending = false
30-
this._time = null
3129
}
3230

3331
on (event: string | symbol, listener: (...args: any[]) => void): this {
@@ -43,31 +41,13 @@ export class Bridge extends EventEmitter {
4341
}
4442

4543
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+
})
6448

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)
7151
}
7252
}
7353

@@ -82,8 +62,8 @@ export class Bridge extends EventEmitter {
8262
_flush () {
8363
if (this._batchingQueue.length) this._send(this._batchingQueue)
8464
clearTimeout(this._timer)
65+
this._timer = null
8566
this._batchingQueue = []
86-
this._time = null
8767
}
8868

8969
// @TODO types

0 commit comments

Comments
 (0)