@samitouri / QOS-React / commits / abcb613223

Minor Bridge code tweaks:

1. Renamed a variable 2. Coerced undefined to null 3. Added a couple of console.warn() for unexpected paths

Brian Vaughn committed Apr 30, 2019 at 09:23 UTC abcb613223c132dcd0e09a6941c628a0177a1d7a
1 file changed +12 -7
src/bridge.js
+12 -7
@@ -12,9 +12,9 @@ type Message = {|
12 |};
13
14 export default class Bridge extends EventEmitter {
15 + _isShutdown: boolean = false;
16 _messageQueue: Array<any> = [];
17 _timeoutID: TimeoutID | null = null;
17 - _destroyed: boolean = false;
18 _wall: Wall;
19 _wallUnlisten: Function | null = null;
20
@@ -23,13 +23,17 @@ export default class Bridge extends EventEmitter {
23
24 this._wall = wall;
25
26 - this._wallUnlisten = wall.listen((message: Message) => {
27 - this.emit(message.event, message.payload);
28 - });
26 + this._wallUnlisten =
27 + wall.listen((message: Message) => {
28 + this.emit(message.event, message.payload);
29 + }) || null;
30 }
31
32 send(event: string, payload: any, transferable?: Array<any>) {
32 - if (this._destroyed) {
33 + if (this._isShutdown) {
34 + console.warn(
35 + `Cannot send message "${event}" through a Bridge that has been shutdown.`
36 + );
37 return;
38 }
39
@@ -48,12 +52,13 @@ export default class Bridge extends EventEmitter {
52 }
53
54 shutdown() {
51 - if (this._destroyed) {
55 + if (this._isShutdown) {
56 + console.warn('Bridge was already shutdown.');
57 return;
58 }
59
60 // Mark this bridge as destroyed, i.e. disable its public API.
56 - this._destroyed = true;
61 + this._isShutdown = true;
62
63 // Disable the API inherited from EventEmitter that can add more listeners and send more messages.
64 this.addListener = function() {};