-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref(replay): Refactor extractDomNodes & countDomNodes to return an in…
…dexed map (#58092) I want to use an indexed map to more easily merge extractedDomNode information with traces inside one tab. This refactor changes the return type for `extractDomNodes` to do that. Also `countDomNodes` to keep things consistent. I went and made a base class for them both because i noticed that `createHiddenPlayer` was copy+pasted, and then got carried away. Related to #50065
- Loading branch information
Showing
6 changed files
with
183 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import {Replayer} from '@sentry-internal/rrweb'; | ||
|
||
import type {RecordingFrame, ReplayFrame} from 'sentry/utils/replays/types'; | ||
|
||
interface Args<Frame extends ReplayFrame | RecordingFrame, CollectionData> { | ||
frames: Frame[] | undefined; | ||
onVisitFrame: ( | ||
frame: Frame, | ||
collection: Map<Frame, CollectionData>, | ||
replayer: Replayer | ||
) => void; | ||
rrwebEvents: RecordingFrame[] | undefined; | ||
shouldVisitFrame: (frame: Frame, replayer: Replayer) => boolean; | ||
startTimestampMs: number; | ||
} | ||
|
||
type FrameRef<Frame extends ReplayFrame | RecordingFrame> = { | ||
frame: Frame | undefined; | ||
}; | ||
|
||
export default function replayerStepper< | ||
Frame extends ReplayFrame | RecordingFrame, | ||
CollectionData, | ||
>({ | ||
frames, | ||
onVisitFrame, | ||
rrwebEvents, | ||
shouldVisitFrame, | ||
startTimestampMs, | ||
}: Args<Frame, CollectionData>): Promise<Map<Frame, CollectionData>> { | ||
const collection = new Map<Frame, CollectionData>(); | ||
|
||
return new Promise(resolve => { | ||
if (!frames?.length || !rrwebEvents?.length) { | ||
resolve(new Map()); | ||
return; | ||
} | ||
|
||
const replayer = createHiddenPlayer(rrwebEvents); | ||
|
||
const nextFrame = (function () { | ||
let i = 0; | ||
return () => frames[i++]; | ||
})(); | ||
|
||
const onDone = () => { | ||
resolve(collection); | ||
}; | ||
|
||
const nextOrDone = () => { | ||
const next = nextFrame(); | ||
if (next) { | ||
considerFrame(next); | ||
} else { | ||
onDone(); | ||
} | ||
}; | ||
|
||
const frameRef: FrameRef<Frame> = { | ||
frame: undefined, | ||
}; | ||
|
||
const considerFrame = (frame: Frame) => { | ||
if (shouldVisitFrame(frame, replayer)) { | ||
frameRef.frame = frame; | ||
window.setTimeout(() => { | ||
const timestamp = | ||
'offsetMs' in frame ? frame.offsetMs : frame.timestamp - startTimestampMs; | ||
replayer.pause(timestamp); | ||
}, 0); | ||
} else { | ||
frameRef.frame = undefined; | ||
nextOrDone(); | ||
} | ||
}; | ||
|
||
const handlePause = () => { | ||
onVisitFrame(frameRef.frame!, collection, replayer); | ||
nextOrDone(); | ||
}; | ||
|
||
replayer.on('pause', handlePause); | ||
considerFrame(nextFrame()); | ||
}); | ||
} | ||
|
||
function createHiddenPlayer(rrwebEvents: RecordingFrame[]): Replayer { | ||
const domRoot = document.createElement('div'); | ||
domRoot.className = 'sentry-block'; | ||
const {style} = domRoot; | ||
|
||
style.position = 'fixed'; | ||
style.inset = '0'; | ||
style.width = '0'; | ||
style.height = '0'; | ||
style.overflow = 'hidden'; | ||
|
||
document.body.appendChild(domRoot); | ||
|
||
return new Replayer(rrwebEvents, { | ||
root: domRoot, | ||
loadTimeout: 1, | ||
showWarning: false, | ||
blockClass: 'sentry-block', | ||
speed: 99999, | ||
skipInactive: true, | ||
triggerFocus: false, | ||
mouseTail: false, | ||
}); | ||
} |
Oops, something went wrong.