@@ -13,39 +13,46 @@ function isCustomEvent(event: RecordingEvent): event is ReplayFrameEvent {
13
13
14
14
/**
15
15
* 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
+ *
20
+ * `isCheckout` is true if this is either the very first event, or an event triggered by `checkoutEveryNms`.
21
+ */
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
+ *
16
36
* `isCheckout` is true if this is either the very first event, or an event triggered by `checkoutEveryNms`.
17
37
*/
18
38
export async function addEvent (
19
39
replay : ReplayContainer ,
20
40
event : RecordingEvent ,
21
41
isCheckout ?: boolean ,
22
42
) : Promise < AddEventResult | null > {
23
- if ( ! replay . eventBuffer ) {
24
- // This implies that `_isEnabled` is false
43
+ if ( ! shouldAddEvent ( replay , event ) ) {
25
44
return null ;
26
45
}
27
46
28
- if ( replay . isPaused ( ) ) {
29
- // Do not add to event buffer when recording is paused
30
- return null ;
31
- }
32
-
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
+ }
42
49
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 ) {
49
56
return null ;
50
57
}
51
58
@@ -81,6 +88,34 @@ export async function addEvent(
81
88
}
82
89
}
83
90
91
+ function shouldAddEvent ( replay : ReplayContainer , event : RecordingEvent ) : boolean {
92
+ if ( ! replay . eventBuffer || replay . isPaused ( ) || ! replay . isEnabled ( ) ) {
93
+ // This implies that `_isEnabled` is false
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
+
84
119
function maybeApplyCallback (
85
120
event : RecordingEvent ,
86
121
callback : ReplayPluginOptions [ 'beforeAddRecordingEvent' ] ,
0 commit comments