fix(tracker): polyfill for bc

This commit is contained in:
nick-delirium 2023-06-29 09:38:17 +02:00
parent b8bfdd9da1
commit 86c144666f
3 changed files with 90 additions and 11 deletions

View file

@ -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' ||

View file

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

View file

@ -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'