targets.js (1940B)
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 "use strict"; 5 6 const initialReducerState = { 7 // Array of targetFront 8 targets: [], 9 // The selected targetFront instance 10 selected: null, 11 // timestamp of the last time a target was updated (i.e. url/title was updated). 12 // This is used by the EvaluationContextSelector component to re-render the list of 13 // targets when the list itself did not change (no addition/removal) 14 lastTargetRefresh: Date.now(), 15 }; 16 17 function update(state = initialReducerState, action) { 18 switch (action.type) { 19 case "SELECT_TARGET": { 20 const { targetActorID } = action; 21 22 if (state.selected?.actorID === targetActorID) { 23 return state; 24 } 25 26 const selectedTarget = state.targets.find( 27 target => target.actorID === targetActorID 28 ); 29 30 // It's possible that the target reducer is missing a target 31 // e.g. workers, remote iframes, etc. (Bug 1594754) 32 if (!selectedTarget) { 33 return state; 34 } 35 36 return { ...state, selected: selectedTarget }; 37 } 38 39 case "REGISTER_TARGET": { 40 return { 41 ...state, 42 targets: [...state.targets, action.targetFront], 43 }; 44 } 45 46 case "REFRESH_TARGETS": { 47 // The data _in_ targetFront was updated, so we only need to mutate the state, 48 // while keeping the same values. 49 return { 50 ...state, 51 lastTargetRefresh: Date.now(), 52 }; 53 } 54 55 case "UNREGISTER_TARGET": { 56 const targets = state.targets.filter( 57 target => target !== action.targetFront 58 ); 59 60 let { selected } = state; 61 if (selected === action.targetFront) { 62 selected = null; 63 } 64 65 return { ...state, targets, selected }; 66 } 67 } 68 return state; 69 } 70 module.exports = update;