/
Transport.js
87 lines (75 loc) · 2.01 KB
/
Transport.js
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
/**
* Defines the transport abstraction, and implements behaviours common to all
* transports.
*/
export default class Transport {
/**
* Construct a base transport, which must be subclassed to provide a concrete
* transport mechanism.
*/
constructor() {
// our m2 event listeners
this.listeners = new EventTarget()
// a flag and a queue for supporting paused mode
this.paused = false
this.eventQ = []
}
/**
* Connect the transport, passing in an implementation specific config. Optional
* for subclasses.
*/
connect(config) {
}
/**
* Listen for the specified `event` and notify caller by invoking
* `listener`.
*/
addEventListener(event, listener) {
this.listeners.addEventListener(event, listener)
}
/**
* Remove the listener identified by the `event` and `listener`.
*/
removeEventListener(event, listener) {
this.listeners.removeEventListener(event, listener)
}
/**
* Reconnect the transport. This exists for stale connection detection, and the base
* class version does nothing.
*/
reconnect() {
}
/**
* Send an event to the M2. These are the same high-level events that the server
* supports. Needs to be implemented by subclasses.
*/
send(event, data) {
throw new Error('Transport.send() must be implemented by subclasses')
}
/**
* Pause a transport. All events will be held until resume() is called.
*/
pause() {
this.paused = true
}
/**
* Resume a paused transport, and drain the events that accumulated while the
* transport was paused.
*/
resume() {
this.paused = false
while (this.eventQ.length > 0) {
this.dispatchEvent(this.eventQ.shift())
}
}
/**
* Dispatch the specified m2 event to listeners, queuing the event if the transport
* is currently paused.
*/
dispatchEvent(event, data) {
if (this.paused) {
return this.eventQ.push({ event, data })
}
this.listeners.dispatchEvent(new CustomEvent(event, { detail: data }))
}
}