-
Notifications
You must be signed in to change notification settings - Fork 66
/
VirtualPageLoadTimer.ts
260 lines (238 loc) · 8.85 KB
/
VirtualPageLoadTimer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import { PerformanceNavigationTimingEvent } from '../events/performance-navigation-timing';
import { PERFORMANCE_NAVIGATION_EVENT_TYPE } from '../plugins/utils/constant';
import { MonkeyPatched } from '../plugins/MonkeyPatched';
import { Config } from '../orchestration/Orchestration';
import { RecordEvent } from '../plugins/types';
import { PageManager, Page } from './PageManager';
type Fetch = typeof fetch;
type Send = () => void;
type Patching = Pick<XMLHttpRequest & Window, 'fetch' | 'send'>;
/**
* Maintains the core logic for virtual page load timing functionality.
* (1) Holds all virtual page load timing related resources
* (2) Intercepts outgoing XMLHttpRequests and Fetch requests and listens for DOM changes
* (3) Records virtual page load
*/
export class VirtualPageLoadTimer extends MonkeyPatched<
Patching,
'fetch' | 'send'
> {
protected get patches() {
return [
{
nodule: XMLHttpRequest.prototype as unknown as Patching,
name: 'send' as const,
wrapper: this.sendWrapper as any
},
{
nodule: window as unknown as Patching,
name: 'fetch' as const,
wrapper: this.fetchWrapper as any
}
];
}
/** Latest interaction time by user on the document */
public latestInteractionTime: number;
/** Unique ID of virtual page load periodic checker. */
private periodicCheckerId: number | undefined;
/** Unique ID of virtual page load timeout checker. */
private timeoutCheckerId: number | undefined;
/** Observer to liten for DOM changes. */
private domMutationObserver: MutationObserver;
/** Set to hold outgoing XMLHttpRequests for current virtual page. */
private ongoingRequests: Set<XMLHttpRequest>;
/** Buffer to hold outgoing XMLHttpRequests before new page is created. */
private requestBuffer: Set<XMLHttpRequest>;
/** Indicate the number of active Fetch requests. */
private fetchCounter: number;
/** Indicate the status of the current Page */
private isPageLoaded: boolean;
/** Indicate the current page's load end time value. */
private latestEndTime: number;
private config: Config;
private pageManager: PageManager;
// @ts-ignore
private readonly record: RecordEvent;
constructor(pageManager: PageManager, config: Config, record: RecordEvent) {
super('virtual-page-load-timer');
this.periodicCheckerId = undefined;
this.timeoutCheckerId = undefined;
this.domMutationObserver = new MutationObserver(this.resetInterval);
this.ongoingRequests = new Set<XMLHttpRequest>();
this.requestBuffer = new Set();
this.fetchCounter = 0;
this.isPageLoaded = true;
this.latestEndTime = 0;
this.latestInteractionTime = 0;
this.config = config;
this.pageManager = pageManager;
this.record = record;
this.enable();
// Start tracking the timestamps
document.addEventListener(
'mousedown',
this.updateLatestInteractionTime
);
document.addEventListener('keydown', this.updateLatestInteractionTime);
}
/** Initializes timing related resources for current page. */
public startTiming() {
this.latestEndTime = Date.now();
// Clean up existing timer objects and mutationObserver
if (this.periodicCheckerId) {
clearInterval(this.periodicCheckerId);
}
if (this.timeoutCheckerId) {
clearTimeout(this.timeoutCheckerId);
}
this.domMutationObserver.disconnect();
// Initialize timer objects and start observing
this.periodicCheckerId = setInterval(
this.checkLoadStatus,
this.config.routeChangeComplete
) as unknown as number;
this.timeoutCheckerId = setTimeout(
this.declareTimeout,
this.config.routeChangeTimeout
) as unknown as number;
// observing the add/delete of nodes
this.domMutationObserver.observe(document, {
subtree: true,
childList: true,
attributes: false,
characterData: false
});
// Indicate page has not loaded, and carry over buffered requests.
this.isPageLoaded = false;
this.requestBuffer.forEach(this.moveItemsFromBuffer);
this.requestBuffer.clear();
}
private sendWrapper = (): ((original: Send) => Send) => {
const self = this;
return (original: Send): Send => {
return function (this: XMLHttpRequest): void {
self.recordXhr(this);
this.addEventListener('loadend', self.endTracking);
return original.apply(this, arguments as any);
};
};
};
private recordXhr(item: XMLHttpRequest) {
const page = this.pageManager.getPage();
if (page && this.isPageLoaded === false) {
this.ongoingRequests.add(item);
} else {
this.requestBuffer.add(item);
}
}
private removeXhr(item: XMLHttpRequest, currTime: number) {
const page = this.pageManager.getPage();
if (page && this.ongoingRequests.has(item)) {
this.ongoingRequests.delete(item);
this.latestEndTime = currTime;
} else if (this.requestBuffer.has(item)) {
this.requestBuffer.delete(item);
}
}
/**
* Removes the current event from either requestBuffer or ongoingRequests set.
*
* @param event
*/
private endTracking = (e: Event) => {
const currTime = Date.now();
const xhr: XMLHttpRequest = e.target as XMLHttpRequest;
xhr.removeEventListener('loadend', this.endTracking);
this.removeXhr(xhr, currTime);
};
private fetch = (
original: Fetch,
thisArg: Fetch,
argsArray: IArguments
): Promise<Response> => {
return original
.apply(thisArg, argsArray as any)
.catch((error) => {
throw error;
})
.finally(this.decrementFetchCounter);
};
/**
* Increment the fetch counter in PageManager when fetch is beginning
*/
private fetchWrapper = (): ((original: Fetch) => Fetch) => {
const self = this;
return (original: Fetch): Fetch => {
return function (
this: Fetch,
input: RequestInfo | URL,
init?: RequestInit
): Promise<Response> {
self.fetchCounter += 1;
return self.fetch(original, this, arguments);
};
};
};
private decrementFetchCounter = () => {
if (!this.isPageLoaded) {
this.latestEndTime = Date.now();
}
this.fetchCounter -= 1;
};
/**
* Checks whether the virtual page is still being loaded.
* If completed:
* (1) Clear the timers
* (2) Record data using PerformanceNavigationTimingEvent
* (3) Indicate current page has finished loading
*/
private checkLoadStatus = () => {
if (this.ongoingRequests.size === 0 && this.fetchCounter === 0) {
clearInterval(this.periodicCheckerId);
clearTimeout(this.timeoutCheckerId);
this.domMutationObserver.disconnect();
this.recordRouteChangeNavigationEvent(
this.pageManager.getPage() as Page
);
this.periodicCheckerId = undefined;
this.timeoutCheckerId = undefined;
this.isPageLoaded = true;
}
};
/** Clears timers and disconnects observer to stop page timing. */
private declareTimeout = () => {
clearInterval(this.periodicCheckerId);
this.periodicCheckerId = undefined;
this.timeoutCheckerId = undefined;
this.domMutationObserver.disconnect();
this.isPageLoaded = true;
};
private resetInterval = () => {
this.latestEndTime = Date.now();
clearInterval(this.periodicCheckerId);
this.periodicCheckerId = setInterval(
this.checkLoadStatus,
this.config.routeChangeComplete
) as unknown as number;
};
private moveItemsFromBuffer = (item: XMLHttpRequest) => {
this.ongoingRequests.add(item);
};
private recordRouteChangeNavigationEvent(page: Page) {
const virtualPageNavigationEvent = {
initiatorType: 'route_change',
type: 'navigate',
startTime: page.start,
duration: this.latestEndTime - page.start
} as PerformanceNavigationTimingEvent;
if (this.record) {
this.record(
PERFORMANCE_NAVIGATION_EVENT_TYPE,
virtualPageNavigationEvent
);
}
}
private updateLatestInteractionTime = (e: Event) => {
this.latestInteractionTime = Date.now();
};
}