fix(tracker): remove polyfil, disable multitab on old devices

This commit is contained in:
nick-delirium 2023-06-29 11:21:03 +02:00
parent 86c144666f
commit c448fdc749
3 changed files with 22 additions and 106 deletions

View file

@ -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<Options>) {
@ -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<RickRoll>) => {
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<RickRoll>) => {
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,
})
}
}
}
}

View file

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

View file

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