messages.js (3919B)
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 MSG_ADD, 9 MSG_SELECT, 10 MSG_OPEN_DETAILS, 11 MSG_CLEAR, 12 MSG_TOGGLE_FILTER_TYPE, 13 MSG_TOGGLE_CONTROL, 14 MSG_SET_FILTER_TEXT, 15 MSG_TOGGLE_COLUMN, 16 MSG_RESET_COLUMNS, 17 MSG_CLOSE_CONNECTION, 18 } = require("resource://devtools/client/netmonitor/src/constants.js"); 19 20 const { 21 getDisplayedMessages, 22 } = require("resource://devtools/client/netmonitor/src/selectors/index.js"); 23 const PAGE_SIZE_ITEM_COUNT_RATIO = 5; 24 25 /** 26 * Add message into state. 27 */ 28 function addMessage(httpChannelId, data, batch) { 29 return { 30 type: MSG_ADD, 31 httpChannelId, 32 data, 33 meta: { batch }, 34 }; 35 } 36 37 /** 38 * Select message. 39 */ 40 function selectMessage(message) { 41 return { 42 type: MSG_SELECT, 43 open: true, 44 message, 45 }; 46 } 47 48 /** 49 * Open message details panel. 50 * 51 * @param {boolean} open - expected message details panel open state 52 */ 53 function openMessageDetails(open) { 54 return { 55 type: MSG_OPEN_DETAILS, 56 open, 57 }; 58 } 59 60 /** 61 * Clear all messages from the MessageListContent 62 * component belonging to the current channelId 63 */ 64 function clearMessages() { 65 return { 66 type: MSG_CLEAR, 67 }; 68 } 69 70 /** 71 * Show filtered messages from the MessageListContent 72 * component belonging to the current channelId 73 */ 74 function toggleMessageFilterType(filter) { 75 return { 76 type: MSG_TOGGLE_FILTER_TYPE, 77 filter, 78 }; 79 } 80 81 /** 82 * Show control frames from the MessageListContent 83 * component belonging to the current channelId 84 */ 85 function toggleControlFrames() { 86 return { 87 type: MSG_TOGGLE_CONTROL, 88 }; 89 } 90 91 /** 92 * Set filter text in toolbar. 93 * 94 */ 95 function setMessageFilterText(text) { 96 return { 97 type: MSG_SET_FILTER_TEXT, 98 text, 99 }; 100 } 101 102 /** 103 * Resets all Messages columns to their default state. 104 * 105 */ 106 function resetMessageColumns() { 107 return { 108 type: MSG_RESET_COLUMNS, 109 }; 110 } 111 112 /** 113 * Toggles a Message column 114 * 115 * @param {string} column - The column that is going to be toggled 116 */ 117 function toggleMessageColumn(column) { 118 return { 119 type: MSG_TOGGLE_COLUMN, 120 column, 121 }; 122 } 123 124 /** 125 * Sets current connection status to closed 126 * 127 * @param {number} httpChannelId - Unique id identifying the channel 128 * @param {boolean} wasClean - False if connection terminated due to error 129 * @param {number} code - Error code 130 * @param {string} reason 131 */ 132 function closeConnection(httpChannelId, wasClean, code, reason) { 133 return { 134 type: MSG_CLOSE_CONNECTION, 135 httpChannelId, 136 wasClean, 137 code, 138 reason, 139 }; 140 } 141 142 /** 143 * Move the selection up to down according to the "delta" parameter. Possible values: 144 * - Number: positive or negative, move up or down by specified distance 145 * - "PAGE_UP" | "PAGE_DOWN" (String): page up or page down 146 * - +Infinity | -Infinity: move to the start or end of the list 147 */ 148 function selectMessageDelta(delta) { 149 return ({ dispatch, getState }) => { 150 const state = getState(); 151 const messages = getDisplayedMessages(state); 152 153 if (messages.length === 0) { 154 return; 155 } 156 157 const selIndex = messages.findIndex( 158 r => r === state.messages.selectedMessage 159 ); 160 161 if (delta === "PAGE_DOWN") { 162 delta = Math.ceil(messages.length / PAGE_SIZE_ITEM_COUNT_RATIO); 163 } else if (delta === "PAGE_UP") { 164 delta = -Math.ceil(messages.length / PAGE_SIZE_ITEM_COUNT_RATIO); 165 } 166 167 const newIndex = Math.min( 168 Math.max(0, selIndex + delta), 169 messages.length - 1 170 ); 171 const newItem = messages[newIndex]; 172 dispatch(selectMessage(newItem)); 173 }; 174 } 175 176 module.exports = { 177 addMessage, 178 clearMessages, 179 closeConnection, 180 openMessageDetails, 181 resetMessageColumns, 182 selectMessage, 183 selectMessageDelta, 184 setMessageFilterText, 185 toggleControlFrames, 186 toggleMessageColumn, 187 toggleMessageFilterType, 188 };