Skip to content

Commit

Permalink
fix tracer unsubscribing from externally active channel
Browse files Browse the repository at this point in the history
  • Loading branch information
rochdev committed Apr 13, 2023
1 parent 84458ef commit 6fd28e0
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/diagnostics_channel/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const {
Channel,
channel
} = require('diagnostics_channel') // eslint-disable-line n/no-restricted-require

Expand All @@ -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)

Expand All @@ -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)
}

Expand Down

0 comments on commit 6fd28e0

Please sign in to comment.