diff --git a/tracker/tracker/src/main/app/index.ts b/tracker/tracker/src/main/app/index.ts index 9acb47a20..8abd67c39 100644 --- a/tracker/tracker/src/main/app/index.ts +++ b/tracker/tracker/src/main/app/index.ts @@ -118,7 +118,7 @@ export default class App { private compressionThreshold = 24 * 1000 private restartAttempts = 0 - private readonly bc: BroadcastChannel = new BroadcastChannel('rick') + private readonly bc: BroadcastChannel | null = null public attributeSender: AttributeSender constructor(projectKey: string, sessionToken: string | undefined, options: Partial) { @@ -149,6 +149,10 @@ export default class App { options, ) + if (!this.options.forceSingleTab && globalThis && 'BroadcastChannel' in globalThis) { + this.bc = new BroadcastChannel('rick') + } + this.revID = this.options.revID this.localStorage = this.options.localStorage ?? window.localStorage this.sessionStorage = this.options.sessionStorage ?? window.sessionStorage @@ -229,24 +233,26 @@ export default class App { const thisTab = this.session.getTabId() - if (!this.session.getSessionToken() && !this.options.forceSingleTab) { + if (!this.session.getSessionToken() && this.bc) { this.bc.postMessage({ line: 'never-gonna-give-you-up', source: thisTab }) } - this.bc.onmessage = (ev: MessageEvent) => { - if (ev.data.source === thisTab) return - if (ev.data.line === 'never-gonna-let-you-down') { - const sessionToken = ev.data.token - this.session.setSessionToken(sessionToken) - } - if (ev.data.line === 'never-gonna-give-you-up') { - const token = this.session.getSessionToken() - if (token) { - this.bc.postMessage({ - line: 'never-gonna-let-you-down', - token, - source: thisTab, - }) + if (this.bc) { + this.bc.onmessage = (ev: MessageEvent) => { + if (ev.data.source === thisTab) return + if (ev.data.line === 'never-gonna-let-you-down') { + const sessionToken = ev.data.token + this.session.setSessionToken(sessionToken) + } + if (ev.data.line === 'never-gonna-give-you-up') { + const token = this.session.getSessionToken() + if (token && this.bc) { + this.bc.postMessage({ + line: 'never-gonna-let-you-down', + token, + source: thisTab, + }) + } } } } diff --git a/tracker/tracker/src/main/app/polyfills.js b/tracker/tracker/src/main/app/polyfills.js deleted file mode 100644 index 5d060a643..000000000 --- a/tracker/tracker/src/main/app/polyfills.js +++ /dev/null @@ -1,89 +0,0 @@ -;(function (global) { - let channels = [] - - function BroadcastChannel(channel) { - let $this = this - channel = String(channel) - - // eslint-disable-next-line @typescript-eslint/restrict-plus-operands - let id = '$BroadcastChannel$' + channel + '$' - - channels[id] = channels[id] || [] - channels[id].push(this) - - this._name = channel - this._id = id - this._closed = false - // eslint-disable-next-line no-undef - this._mc = new MessageChannel() - this._mc.port1.start() - this._mc.port2.start() - - global.addEventListener('storage', function (e) { - if (e.storageArea !== global.localStorage) return - if (e.newValue == null || e.newValue === '') return - if (e.key.substring(0, id.length) !== id) return - let data = JSON.parse(e.newValue) - $this._mc.port2.postMessage(data) - }) - } - - BroadcastChannel.prototype = { - // BroadcastChannel API - get name() { - return this._name - }, - postMessage: function (message) { - let $this = this - if (this._closed) { - let e = new Error() - e.name = 'InvalidStateError' - throw e - } - let value = JSON.stringify(message) - - // Broadcast to other contexts via storage events... - let key = this._id + String(Date.now()) + '$' + String(Math.random()) - global.localStorage.setItem(key, value) - // eslint-disable-next-line no-undef - setTimeout(function () { - global.localStorage.removeItem(key) - }, 500) - - // Broadcast to current context via ports - channels[this._id].forEach(function (bc) { - if (bc === $this) return - bc._mc.port2.postMessage(JSON.parse(value)) - }) - }, - close: function () { - if (this._closed) return - this._closed = true - this._mc.port1.close() - this._mc.port2.close() - - let index = channels[this._id].indexOf(this) - channels[this._id].splice(index, 1) - }, - - // EventTarget API - get onmessage() { - return this._mc.port1.onmessage - }, - set onmessage(value) { - this._mc.port1.onmessage = value - }, - addEventListener: function (/*type, listener , useCapture*/) { - return this._mc.port1.addEventListener.apply(this._mc.port1, arguments) - }, - removeEventListener: function (/*type, listener , useCapture*/) { - return this._mc.port1.removeEventListener.apply(this._mc.port1, arguments) - }, - dispatchEvent: function (/*event*/) { - return this._mc.port1.dispatchEvent.apply(this._mc.port1, arguments) - }, - } - - global.BroadcastChannel = global.BroadcastChannel || BroadcastChannel - // eslint-disable-next-line no-undef -})(self) diff --git a/tracker/tracker/src/main/index.ts b/tracker/tracker/src/main/index.ts index 97eecb204..c1a5212ba 100644 --- a/tracker/tracker/src/main/index.ts +++ b/tracker/tracker/src/main/index.ts @@ -1,4 +1,3 @@ -import './app/polyfills.js' // bc polyfill import App, { DEFAULT_INGEST_POINT } from './app/index.js' export { default as App } from './app/index.js'