From 86c144666ffd9431d3d4cf4295a2014d61aed167 Mon Sep 17 00:00:00 2001 From: nick-delirium Date: Thu, 29 Jun 2023 09:38:17 +0200 Subject: [PATCH] fix(tracker): polyfill for bc --- tracker/tracker/src/main/app/index.ts | 10 --- tracker/tracker/src/main/app/polyfills.js | 89 +++++++++++++++++++++++ tracker/tracker/src/main/index.ts | 2 +- 3 files changed, 90 insertions(+), 11 deletions(-) create mode 100644 tracker/tracker/src/main/app/polyfills.js diff --git a/tracker/tracker/src/main/app/index.ts b/tracker/tracker/src/main/app/index.ts index 6d16f277a..9acb47a20 100644 --- a/tracker/tracker/src/main/app/index.ts +++ b/tracker/tracker/src/main/app/index.ts @@ -115,7 +115,6 @@ export default class App { private activityState: ActivityState = ActivityState.NotActive private readonly version = 'TRACKER_VERSION' // TODO: version compatability check inside each plugin. private readonly worker?: TypedWorker - private featureFlags: string[] = [] private compressionThreshold = 24 * 1000 private restartAttempts = 0 @@ -451,14 +450,6 @@ export default class App { return this.activityState === ActivityState.Active } - isFeatureActive(feature: string): boolean { - return this.featureFlags.includes(feature) - } - - getFeatureFlags(): string[] { - return this.featureFlags - } - resetNextPageSession(flag: boolean) { if (flag) { this.sessionStorage.setItem(this.options.session_reset_key, 't') @@ -564,7 +555,6 @@ export default class App { userOS, userState, } = r - // TODO: insert feature flags here if ( typeof token !== 'string' || typeof userUUID !== 'string' || diff --git a/tracker/tracker/src/main/app/polyfills.js b/tracker/tracker/src/main/app/polyfills.js new file mode 100644 index 000000000..5d060a643 --- /dev/null +++ b/tracker/tracker/src/main/app/polyfills.js @@ -0,0 +1,89 @@ +;(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 7b13e067c..97eecb204 100644 --- a/tracker/tracker/src/main/index.ts +++ b/tracker/tracker/src/main/index.ts @@ -1,3 +1,4 @@ +import './app/polyfills.js' // bc polyfill import App, { DEFAULT_INGEST_POINT } from './app/index.js' export { default as App } from './app/index.js' @@ -28,7 +29,6 @@ import Selection from './modules/selection.js' import Tabs from './modules/tabs.js' import { IN_BROWSER, deprecationWarn, DOCS_HOST } from './utils.js' import FeatureFlags, { IFeatureFlag } from './modules/featureFlags.js' - import type { Options as AppOptions } from './app/index.js' import type { Options as ConsoleOptions } from './modules/console.js' import type { Options as ExceptionOptions } from './modules/exception.js'