@@ -13,39 +13,46 @@ function isCustomEvent(event: RecordingEvent): event is ReplayFrameEvent {
1313
1414/**
1515 * Add an event to the event buffer.
16+ * In contrast to `addEvent`, this does not return a promise & does not wait for the adding of the event to succeed/fail.
17+ * Instead this returns `true` if we tried to add the event, else false.
18+ * It returns `false` e.g. if we are paused, disabled, or out of the max replay duration.
19+ *
1620 * `isCheckout` is true if this is either the very first event, or an event triggered by `checkoutEveryNms`.
1721 */
18- export async function addEvent (
22+ export function addEventSync ( replay : ReplayContainer , event : RecordingEvent , isCheckout ?: boolean ) : boolean {
23+ if ( ! shouldAddEvent ( replay , event ) ) {
24+ return false ;
25+ }
26+
27+ void _addEvent ( replay , event , isCheckout ) ;
28+
29+ return true ;
30+ }
31+
32+ /**
33+ * Add an event to the event buffer.
34+ * Resolves to `null` if no event was added, else to `void`.
35+ *
36+ * `isCheckout` is true if this is either the very first event, or an event triggered by `checkoutEveryNms`.
37+ */
38+ export function addEvent (
1939 replay : ReplayContainer ,
2040 event : RecordingEvent ,
2141 isCheckout ?: boolean ,
2242) : Promise < AddEventResult | null > {
23- if ( ! replay . eventBuffer ) {
24- // This implies that `_isEnabled` is false
25- return null ;
26- }
27-
28- if ( replay . isPaused ( ) ) {
29- // Do not add to event buffer when recording is paused
30- return null ;
43+ if ( ! shouldAddEvent ( replay , event ) ) {
44+ return Promise . resolve ( null ) ;
3145 }
3246
33- const timestampInMs = timestampToMs ( event . timestamp ) ;
34-
35- // Throw out events that happen more than 5 minutes ago. This can happen if
36- // page has been left open and idle for a long period of time and user
37- // comes back to trigger a new session. The performance entries rely on
38- // `performance.timeOrigin`, which is when the page first opened.
39- if ( timestampInMs + replay . timeouts . sessionIdlePause < Date . now ( ) ) {
40- return null ;
41- }
47+ return _addEvent ( replay , event , isCheckout ) ;
48+ }
4249
43- // Throw out events that are +60min from the initial timestamp
44- if ( timestampInMs > replay . getContext ( ) . initialTimestamp + replay . getOptions ( ) . maxReplayDuration ) {
45- logInfo (
46- `[Replay] Skipping event with timestamp ${ timestampInMs } because it is after maxReplayDuration` ,
47- replay . getOptions ( ) . _experiments . traceInternals ,
48- ) ;
50+ async function _addEvent (
51+ replay : ReplayContainer ,
52+ event : RecordingEvent ,
53+ isCheckout ?: boolean ,
54+ ) : Promise < AddEventResult | null > {
55+ if ( ! replay . eventBuffer ) {
4956 return null ;
5057 }
5158
@@ -81,6 +88,34 @@ export async function addEvent(
8188 }
8289}
8390
91+ /** Exported only for tests. */
92+ export function shouldAddEvent ( replay : ReplayContainer , event : RecordingEvent ) : boolean {
93+ if ( ! replay . eventBuffer || replay . isPaused ( ) || ! replay . isEnabled ( ) ) {
94+ return false ;
95+ }
96+
97+ const timestampInMs = timestampToMs ( event . timestamp ) ;
98+
99+ // Throw out events that happen more than 5 minutes ago. This can happen if
100+ // page has been left open and idle for a long period of time and user
101+ // comes back to trigger a new session. The performance entries rely on
102+ // `performance.timeOrigin`, which is when the page first opened.
103+ if ( timestampInMs + replay . timeouts . sessionIdlePause < Date . now ( ) ) {
104+ return false ;
105+ }
106+
107+ // Throw out events that are +60min from the initial timestamp
108+ if ( timestampInMs > replay . getContext ( ) . initialTimestamp + replay . getOptions ( ) . maxReplayDuration ) {
109+ logInfo (
110+ `[Replay] Skipping event with timestamp ${ timestampInMs } because it is after maxReplayDuration` ,
111+ replay . getOptions ( ) . _experiments . traceInternals ,
112+ ) ;
113+ return false ;
114+ }
115+
116+ return true ;
117+ }
118+
84119function maybeApplyCallback (
85120 event : RecordingEvent ,
86121 callback : ReplayPluginOptions [ 'beforeAddRecordingEvent' ] ,
0 commit comments