File size: 2,645 Bytes
4d70170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// eslint-disable-next-line unicorn/prefer-node-protocol
import { EventEmitter } from 'events'
import { raf } from './raf'

const BATCH_DURATION = 100

export class Bridge extends EventEmitter {
  wall: any // @TODO
  _batchingQueue: any[] // @TODO
  _sendingQueue: any[][] // @TODO
  _receivingQueue: any[] // @TODO
  _sending: boolean
  _timer: NodeJS.Timeout

  constructor(wall) {
    super()
    this.setMaxListeners(Number.POSITIVE_INFINITY)
    this.wall = wall
    wall.listen((messages) => {
      if (Array.isArray(messages)) {
        messages.forEach(message => this._emit(message))
      }
      else {
        this._emit(messages)
      }
    })
    this._batchingQueue = []
    this._sendingQueue = []
    this._receivingQueue = []
    this._sending = false
  }

  on(event: string | symbol, listener: (...args: any[]) => void): this {
    const wrappedListener = async (...args) => {
      try {
        await listener(...args)
      }
      catch (e) {
        console.error(`[Bridge] Error in listener for event ${event.toString()} with args:`, args)
        console.error(e)
      }
    }
    return super.on(event, wrappedListener)
  }

  send(event: string, payload?: any) {
    this._batchingQueue.push({
      event,
      payload,
    })

    if (this._timer == null) {
      this._timer = setTimeout(() => this._flush(), BATCH_DURATION)
    }
  }

  /**
   * Log a message to the devtools background page.
   */

  log(message: string) {
    this.send('log', message)
  }

  _flush() {
    if (this._batchingQueue.length) {
      this._send(this._batchingQueue)
    }
    clearTimeout(this._timer)
    this._timer = null
    this._batchingQueue = []
  }

  // @TODO types
  _emit(message) {
    if (typeof message === 'string') {
      this.emit(message)
    }
    else if (message._chunk) {
      this._receivingQueue.push(message._chunk)
      if (message.last) {
        this.emit(message.event, this._receivingQueue)
        this._receivingQueue = []
      }
    }
    else if (message.event) {
      this.emit(message.event, message.payload)
    }
  }

  // @TODO types
  _send(messages) {
    this._sendingQueue.push(messages)
    this._nextSend()
  }

  _nextSend() {
    if (!this._sendingQueue.length || this._sending) {
      return
    }
    this._sending = true
    const messages = this._sendingQueue.shift()
    try {
      this.wall.send(messages)
    }
    catch (err) {
      if (err.message === 'Message length exceeded maximum allowed length.') {
        this._sendingQueue.splice(0, 0, messages.map(message => [message]))
      }
    }
    this._sending = false
    raf(() => this._nextSend())
  }
}