Skip to content

Commit

Permalink
🎨 Add node.js partial support to the perf function
Browse files Browse the repository at this point in the history
  • Loading branch information
elbywan committed Oct 6, 2017
1 parent 6fa1d29 commit 0045672
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default {
polyfills: {
fetch: null,
FormData: null,
URLSearchParams: null
URLSearchParams: null,
PerformanceObserver: null
}
}
28 changes: 19 additions & 9 deletions src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,28 @@ export const resolver = url => (catchers: Map<number, (error: WretcherError) =>
*/
text: wrapTypeParser<string>("text"),
/**
* Performs a callback on the API performance timings when they will be available.
* Performs a callback on the API performance timings of the request.
*
* Warning: Still experimental on browsers and not supported by node.js
* Warning: Still experimental on browsers and node.js
*/
perfs: cb => {
if(cb && typeof self !== "undefined" && typeof self["PerformanceObserver"] !== "undefined") {
wrapper.then(res => {
const observer = new self["PerformanceObserver"](entries => {
if(res) cb(entries.getEntriesByName(res.url).reverse()[0])
observer.disconnect()
})
observer.observe({entryTypes: ["resource"]})
const perfObserver = conf.polyfills.PerformanceObserver || (typeof self !== "undefined" ? self["PerformanceObserver"] : null)
if(cb && perfObserver) {
let entries = null
let callback = null
const observer = new perfObserver(e => {
entries = e.getEntries()
if(callback) callback()
observer.disconnect()
})
observer.observe({ entryTypes: ["resource", "measure"] })
req.then(res => {
if(res) {
if(entries)
cb(entries.reverse().find(_ => _.name === res.url))
else
callback = () => cb(entries.reverse().find(_ => _.name === res.url))
}
})
}
return responseTypes
Expand Down

0 comments on commit 0045672

Please sign in to comment.