source-actors.js (4088B)
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 /** 6 * Tells if a given Source Actor is registered in the redux store 7 * 8 * @param {object} state 9 * @param {string} sourceActorId 10 * Source Actor ID 11 * @return {boolean} 12 */ 13 export function hasSourceActor(state, sourceActorId) { 14 return state.sourceActors.mutableSourceActors.has(sourceActorId); 15 } 16 17 /** 18 * Get the Source Actor object. See create.js:createSourceActor() 19 * 20 * @param {object} state 21 * @param {string} sourceActorId 22 * Source Actor ID 23 * @return {object} 24 * The Source Actor object (if registered) 25 */ 26 export function getSourceActor(state, sourceActorId) { 27 return state.sourceActors.mutableSourceActors.get(sourceActorId); 28 } 29 30 /** 31 * Reports if the Source Actor relates to a valid source map / original source. 32 * 33 * @param {object} state 34 * @param {string} sourceActorId 35 * Source Actor ID 36 * @return {boolean} 37 * True if it has a valid source map/original object. 38 */ 39 export function isSourceActorWithSourceMap(state, sourceActorId) { 40 return state.sourceActors.mutableSourceActorsWithSourceMap.has(sourceActorId); 41 } 42 43 export function getSourceMapErrorForSourceActor(state, sourceActorId) { 44 return state.sourceActors.mutableSourceMapErrors.get(sourceActorId); 45 } 46 47 export function getSourceMapResolvedURL(state, sourceActorId) { 48 return state.sourceActors.mutableResolvedSourceMapURL.get(sourceActorId); 49 } 50 51 // Used by threads selectors 52 /** 53 * Get all Source Actor objects for a given thread. See create.js:createSourceActor() 54 * 55 * @param {object} state 56 * @param {Array<string>} threadActorIDs 57 * List of Thread IDs 58 * @return {Array<object>} 59 */ 60 export function getSourceActorsForThread(state, threadActorIDs) { 61 if (!Array.isArray(threadActorIDs)) { 62 threadActorIDs = [threadActorIDs]; 63 } 64 const actors = []; 65 for (const sourceActor of state.sourceActors.mutableSourceActors.values()) { 66 if (threadActorIDs.includes(sourceActor.thread)) { 67 actors.push(sourceActor); 68 } 69 } 70 return actors; 71 } 72 73 /** 74 * Get the list of all breakable lines for a given source actor. 75 * 76 * @param {object} state 77 * @param {string} sourceActorId 78 * Source Actor ID 79 * @return {Promise<Array<Number>> | <Array<Number> | null} 80 * - null when the breakable lines have not been requested yet 81 * - Promise when the breakable lines are in process of being retrieved 82 * - Array when the breakable lines are available 83 */ 84 export function getSourceActorBreakableLines(state, sourceActorId) { 85 return state.sourceActors.mutableBreakableLines.get(sourceActorId); 86 } 87 88 // Used by sources selectors 89 /** 90 * Get the list of all breakable lines for a set of source actors. 91 * 92 * This is typically used to fetch the breakable lines of HTML sources 93 * which are made of multiple source actors (one per inline script). 94 * 95 * @param {object} state 96 * @param {Array<string>} sourceActors 97 * List of Source Actors 98 * @param {boolean} isHTML 99 * True, if we are fetching the breakable lines for an HTML source. 100 * For them, we have to aggregate the lines of each source actors. 101 * Otherwise, we might still have many source actors, but one per thread. 102 * In this case, we simply return the first source actor to have the lines ready. 103 * @return {Array<number>} 104 * List of all the breakable lines. 105 */ 106 export function getBreakableLinesForSourceActors(state, sourceActors, isHTML) { 107 const allBreakableLines = []; 108 for (const sourceActor of sourceActors) { 109 const breakableLines = getSourceActorBreakableLines(state, sourceActor.id); 110 111 // Ignore the source actor if its breakable lines are still being fetched 112 // from the server 113 if (!breakableLines || breakableLines instanceof Promise) { 114 continue; 115 } 116 117 if (isHTML) { 118 allBreakableLines.push(...breakableLines); 119 } else { 120 return breakableLines; 121 } 122 } 123 return allBreakableLines; 124 }