threads.js (2343B)
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 * Threads reducer 7 * 8 * @module reducers/threads 9 */ 10 11 import { sortThreads } from "./sources-tree.js"; 12 13 const lazy = {}; 14 ChromeUtils.defineESModuleGetters(lazy, { 15 BinarySearch: "resource://gre/modules/BinarySearch.sys.mjs", 16 }); 17 18 export function initialThreadsState() { 19 return { 20 threads: [], 21 22 // List of thread actor IDs which are current tracing. 23 // i.e. where JavaScript tracing is enabled. 24 mutableTracingThreads: new Set(), 25 }; 26 } 27 28 export default function update(state = initialThreadsState(), action) { 29 switch (action.type) { 30 case "INSERT_THREAD": { 31 const { newThread } = action; 32 if (newThread.isTopLevel) { 33 return { 34 ...state, 35 threads: [newThread, ...state.threads], 36 }; 37 } 38 39 const index = lazy.BinarySearch.insertionIndexOf( 40 sortThreads, 41 state.threads, 42 newThread 43 ); 44 return { 45 ...state, 46 threads: state.threads.toSpliced(index, 0, newThread), 47 }; 48 } 49 50 case "REMOVE_THREAD": 51 return { 52 ...state, 53 threads: state.threads.filter( 54 thread => action.threadActorID != thread.actor 55 ), 56 }; 57 58 case "UPDATE_SERVICE_WORKER_STATUS": 59 return { 60 ...state, 61 threads: state.threads.map(t => { 62 if (t.actor == action.thread) { 63 return { ...t, serviceWorkerStatus: action.status }; 64 } 65 return t; 66 }), 67 }; 68 69 case "TRACING_TOGGLED": { 70 const { mutableTracingThreads } = state; 71 const sizeBefore = mutableTracingThreads.size; 72 if (action.enabled) { 73 mutableTracingThreads.add(action.thread); 74 } else { 75 mutableTracingThreads.delete(action.thread); 76 } 77 // We may receive toggle events when we change the logging method 78 // while we are already tracing, but the list of tracing thread stays the same. 79 const changed = mutableTracingThreads.size != sizeBefore; 80 if (changed) { 81 return { 82 ...state, 83 mutableTracingThreads, 84 }; 85 } 86 return state; 87 } 88 89 default: 90 return state; 91 } 92 }