diff --git a/packages/diagnostics_channel/src/index.js b/packages/diagnostics_channel/src/index.js index 0f31912711a..ead90759dcc 100644 --- a/packages/diagnostics_channel/src/index.js +++ b/packages/diagnostics_channel/src/index.js @@ -1,6 +1,7 @@ 'use strict' const { + Channel, channel } = require('diagnostics_channel') // eslint-disable-line n/no-restricted-require @@ -20,9 +21,11 @@ if (major === '19' && minor === '9') { if (!channels.has(ch)) { const subscribe = ch.subscribe + const unsubscribe = ch.unsubscribe ch.subscribe = function () { delete ch.subscribe + delete ch.unsubscribe const result = subscribe.apply(this, arguments) @@ -31,6 +34,19 @@ if (major === '19' && minor === '9') { return result } + if (ch.unsubscribe === Channel.prototype.unsubscribe) { + // Needed because another subscriber could have subscribed to something + // that we unsubscribe to before the library is loaded. + ch.unsubscribe = function () { + delete ch.subscribe + delete ch.unsubscribe + + this.subscribe(() => {}) // Keep it active forever. + + return unsubscribe.apply(this, arguments) + } + } + channels.add(ch) }