timing-markers.js (1773B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const { 8 ADD_REQUEST, 9 ADD_TIMING_MARKER, 10 CLEAR_TIMING_MARKERS, 11 CLEAR_REQUESTS, 12 } = require("resource://devtools/client/netmonitor/src/constants.js"); 13 14 function TimingMarkers() { 15 return { 16 firstDocumentDOMContentLoadedTimestamp: -1, 17 firstDocumentLoadTimestamp: -1, 18 firstDocumentRequestStartTimestamp: +Infinity, 19 }; 20 } 21 22 function addRequest(state, action) { 23 const { startedMs } = action.data; 24 if (startedMs < state.firstDocumentRequestStartTimestamp) { 25 return { 26 ...state, 27 firstDocumentRequestStartTimestamp: startedMs, 28 }; 29 } 30 31 return state; 32 } 33 34 function addTimingMarker(state, action) { 35 if ( 36 action.marker.name === "dom-interactive" && 37 state.firstDocumentDOMContentLoadedTimestamp === -1 38 ) { 39 return { 40 ...state, 41 firstDocumentDOMContentLoadedTimestamp: action.marker.time, 42 }; 43 } 44 45 if ( 46 action.marker.name === "dom-complete" && 47 state.firstDocumentLoadTimestamp === -1 48 ) { 49 return { 50 ...state, 51 firstDocumentLoadTimestamp: action.marker.time, 52 }; 53 } 54 55 return state; 56 } 57 58 function clearTimingMarkers() { 59 return new TimingMarkers(); 60 } 61 62 function timingMarkers(state = new TimingMarkers(), action) { 63 switch (action.type) { 64 case ADD_REQUEST: 65 return addRequest(state, action); 66 67 case ADD_TIMING_MARKER: 68 return addTimingMarker(state, action); 69 70 case CLEAR_REQUESTS: 71 case CLEAR_TIMING_MARKERS: 72 return clearTimingMarkers(state); 73 74 default: 75 return state; 76 } 77 } 78 79 module.exports = { 80 TimingMarkers, 81 timingMarkers, 82 };