fix(tracker): contextCallback: only meaningful contexts, called once per instance

This commit is contained in:
Alex Kaminskii 2022-08-30 20:26:38 +02:00
parent 42d95ab74e
commit a4b17ba650
2 changed files with 16 additions and 6 deletions

View file

@ -229,6 +229,8 @@ export default class App {
}
attachCommitCallback(cb: CommitCallback): void {
// TODO!: what if start callback added when activityState === Active ?
// For example - attachEventListener() called during dynamic <iframe> appearance
this.commitCallbacks.push(cb)
}
attachStartCallback(cb: StartCallback): void {
@ -444,13 +446,12 @@ export default class App {
}
this.worker.postMessage(startWorkerMsg)
this.activityState = ActivityState.Active
const onStartInfo = { sessionToken: token, userUUID, sessionID }
this.startCallbacks.forEach((cb) => cb(onStartInfo)) // TODO: start as early as possible (before receiving the token)
this.observer.observe()
this.ticker.start()
this.activityState = ActivityState.Active
this.notify.log('OpenReplay tracking started.')
// get rid of onStart ?

View file

@ -12,7 +12,9 @@ export interface Options {
captureIFrames: boolean
}
type ContextCallback = (context: Window & typeof globalThis) => void
type Context = Window & typeof globalThis
type ContextCallback = (context: Context) => void
// Le truc - for defining an absolute offset for (nested) iframe documents. (To track mouse movments)
type Offset = { top: number; left: number }
@ -59,6 +61,7 @@ export default class TopObserver extends Observer {
private readonly contextCallbacks: Array<ContextCallback> = []
// Attached once per Tracker instance
private readonly contextsSet: Set<Context> = new Set()
attachContextCallback(cb: ContextCallback) {
this.contextCallbacks.push(cb)
}
@ -98,9 +101,15 @@ export default class TopObserver extends Observer {
}
}
}
if (currentWin && currentWin !== win) {
//@ts-ignore https://github.com/microsoft/TypeScript/issues/41684
this.contextCallbacks.forEach((cb) => cb(currentWin))
if (
currentWin &&
// Sometimes currentWin.window is null (not in specification). Such window object is not functional
currentWin === currentWin.window &&
!this.contextsSet.has(currentWin.window) // for each context callbacks called once per Tracker (TopObserver) instance
) {
this.contextsSet.add(currentWin.window)
//+ https://github.com/microsoft/TypeScript/issues/41684
this.contextCallbacks.forEach((cb) => cb(currentWin.window))
win = currentWin
}
})