Tweaked Bridge shutdown sequence slightly

This commit is contained in:
Brian Vaughn
2019-05-23 11:46:28 -07:00
parent f810f6ffb0
commit 1f772acff4
+6 -11
View File
@@ -57,6 +57,12 @@ 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() {};
@@ -71,9 +77,6 @@ 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 {
@@ -83,17 +86,9 @@ 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.