|
| 1 | +import Ember from 'ember'; |
| 2 | + |
| 3 | +const { typeOf } = Ember; |
| 4 | + |
| 5 | +export default Ember.Service.extend({ |
| 6 | + isMarkSupportedBrowser: true, |
| 7 | + isNowSupportedBrowser: true, |
| 8 | + |
| 9 | + secondaryPerformanceObj: {}, |
| 10 | + |
| 11 | + init() { |
| 12 | + if (!(window.performance && window.performance.mark)) { |
| 13 | + console.log('Performance.mark is not supported in this browser. Hence falling back to performance.now()'); |
| 14 | + this.set('isMarkSupportedBrowser', false); |
| 15 | + |
| 16 | + if (!(window.performance && window.performance.now)) { |
| 17 | + console.log('Performance.now is also not supported. Hence falling back to javascript Date API'); |
| 18 | + this.set('isNowSupportedBrowser', false); |
| 19 | + |
| 20 | + } |
| 21 | + } |
| 22 | + }, |
| 23 | + |
| 24 | + start(eventName) { |
| 25 | + let secondaryPerformanceObj = this.get('secondaryPerformanceObj'); |
| 26 | + secondaryPerformanceObj[eventName] = secondaryPerformanceObj[eventName] || {}; |
| 27 | + let eventObj = secondaryPerformanceObj[eventName]; |
| 28 | + |
| 29 | + if (typeOf(eventName) !== 'string') { |
| 30 | + throw 'Expected type String for invoking `start`'; |
| 31 | + } |
| 32 | + |
| 33 | + if (this.get('isNowSupportedBrowser')) { |
| 34 | + if (this.get('isMarkSupportedBrowser')) { |
| 35 | + performance.mark(`mark_${eventName}_start`); |
| 36 | + } else { |
| 37 | + eventObj.start = window.performance.now(); |
| 38 | + } |
| 39 | + } else { |
| 40 | + eventObj.start = new Date().valueOf(); |
| 41 | + } |
| 42 | + |
| 43 | + return; |
| 44 | + }, |
| 45 | + |
| 46 | + end(eventName) { |
| 47 | + let secondaryPerformanceObj = this.get('secondaryPerformanceObj'); |
| 48 | + |
| 49 | + secondaryPerformanceObj[eventName] = secondaryPerformanceObj[eventName] || {}; |
| 50 | + let eventObj = secondaryPerformanceObj[eventName]; |
| 51 | + |
| 52 | + if (typeOf(eventName) !== 'string') { |
| 53 | + throw 'Expected type String for invoking `end`'; |
| 54 | + } |
| 55 | + |
| 56 | + if (this.get('isNowSupportedBrowser')) { |
| 57 | + let startMark = `mark_${eventName}_start`; |
| 58 | + let endMark = `mark_${eventName}_end`; |
| 59 | + |
| 60 | + if (this.get('isMarkSupportedBrowser')) { |
| 61 | + performance.mark(endMark); |
| 62 | + performance.measure(eventName, startMark, endMark); |
| 63 | + } else { |
| 64 | + eventObj.end = window.performance.now(); |
| 65 | + } |
| 66 | + } else { |
| 67 | + eventObj.end = new Date().valueOf(); |
| 68 | + } |
| 69 | + return; |
| 70 | + }, |
| 71 | + |
| 72 | + measure(eventName) { |
| 73 | + let secondaryPerformanceObj = this.get('secondaryPerformanceObj'); |
| 74 | + let eventObj = secondaryPerformanceObj[eventName] || {}; |
| 75 | + let duration; |
| 76 | + |
| 77 | + if (this.get('isMarkSupportedBrowser')) { |
| 78 | + let perfEntries = performance.getEntriesByName(eventName); |
| 79 | + // poping up the last entry pushed into teh array |
| 80 | + let entry = perfEntries[perfEntries.length - 1]; |
| 81 | + if (entry) { |
| 82 | + duration = entry.duration; |
| 83 | + } |
| 84 | + } else { |
| 85 | + duration = eventObj.end - eventObj.start; |
| 86 | + } |
| 87 | + |
| 88 | + return duration || -1; |
| 89 | + } |
| 90 | + |
| 91 | +}); |
0 commit comments