diff --git a/src/bridge.js b/src/bridge.js index 1619e51852..62a3cae2c3 100644 --- a/src/bridge.js +++ b/src/bridge.js @@ -57,12 +57,6 @@ export default class Bridge extends EventEmitter { return; } - // Queue the shutdown outgoing message for subscribers. - this.send('shutdown'); - - // Mark this bridge as destroyed, i.e. disable its public API. - this._isShutdown = true; - // Disable the API inherited from EventEmitter that can add more listeners and send more messages. this.addListener = function() {}; this.emit = function() {}; @@ -77,6 +71,9 @@ export default class Bridge extends EventEmitter { wallUnlisten(); } + // Queue the shutdown outgoing message for subscribers. + this.send('shutdown'); + // Synchronously flush all queued outgoing messages. // At this step the subscribers' code may run in this call stack. do { @@ -86,9 +83,17 @@ export default class Bridge extends EventEmitter { // Make sure once again that there is no dangling timer. clearTimeout(this._timeoutID); this._timeoutID = null; + + // Mark this bridge as destroyed, i.e. disable its public API. + this._isShutdown = true; } _flush = () => { + if (this._isShutdown) { + console.warn(`Cannot flush a Bridge that has been shutdown.`); + return; + } + // This method is used after the bridge is marked as destroyed in shutdown sequence, // so we do not bail out if the bridge marked as destroyed. // It is a private method that the bridge ensures is only called at the right times.