-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
when web worker perf.getEntries() is stubbed, use fallback method
- Loading branch information
1 parent
45b0be0
commit 0faffa8
Showing
5 changed files
with
180 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,85 @@ | ||
// @flow | ||
|
||
// Wraps performance.getEntriesByName to facilitate testing | ||
import type {RequestParameters} from '../util/ajax'; | ||
|
||
// Wraps performance to facilitate testing | ||
// Not incorporated into browser.js because the latter is poisonous when used outside the main thread | ||
const exported = { | ||
getEntriesByName: (url: string) => { | ||
if ((typeof performance !== 'undefined') && performance && performance.getEntriesByName) | ||
return performance.getEntriesByName(url); | ||
else | ||
return false; | ||
} | ||
const performanceExists = typeof performance !== 'undefined'; | ||
const wrapper = {}; | ||
|
||
wrapper.getEntriesByName = (url: string) => { | ||
if (performanceExists && performance && performance.getEntriesByName) | ||
return performance.getEntriesByName(url); | ||
else | ||
return false; | ||
}; | ||
|
||
wrapper.mark = (name: string) => { | ||
if (performanceExists && performance && performance.mark) | ||
return performance.mark(name); | ||
else | ||
return false; | ||
}; | ||
|
||
wrapper.measure = (name: string, startMark: string, endMark: string) => { | ||
if (performanceExists && performance && performance.measure) | ||
return performance.measure(name, startMark, endMark); | ||
else | ||
return false; | ||
}; | ||
|
||
wrapper.clearMarks = (name: string) => { | ||
if (performanceExists && performance && performance.clearMarks) | ||
return performance.clearMarks(name); | ||
else | ||
return false; | ||
}; | ||
|
||
wrapper.clearMeasures = (name: string) => { | ||
if (performanceExists && performance && performance.clearMeasures) | ||
return performance.clearMeasures(name); | ||
else | ||
return false; | ||
}; | ||
|
||
export default exported; | ||
/** | ||
* Safe wrapper for the performance resource timing API in web workers with graceful degradation | ||
* | ||
* @param {RequestParameters} request | ||
* @private | ||
*/ | ||
class Performance { | ||
_marks: {start: string, end: string, measure: string}; | ||
|
||
constructor (request: RequestParameters) { | ||
this._marks = { | ||
start: [request.url, 'start'].join('#'), | ||
end: [request.url, 'end'].join('#'), | ||
measure: request.url.toString() | ||
}; | ||
|
||
wrapper.mark(this._marks.start); | ||
} | ||
|
||
finish() { | ||
wrapper.mark(this._marks.end); | ||
let resourceTimingData = wrapper.getEntriesByName(this._marks.measure); | ||
|
||
// fallback if web worker implementation of perf.getEntriesByName returns empty | ||
if (resourceTimingData.length === 0) { | ||
wrapper.measure(this._marks.measure, this._marks.start, this._marks.end); | ||
resourceTimingData = wrapper.getEntriesByName(this._marks.measure); | ||
|
||
// cleanup | ||
wrapper.clearMarks(this._marks.start); | ||
wrapper.clearMarks(this._marks.end); | ||
wrapper.clearMeasures(this._marks.measure); | ||
} | ||
|
||
return resourceTimingData; | ||
} | ||
} | ||
|
||
wrapper.Performance = Performance; | ||
|
||
export default wrapper; |
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