ProfilerPreferenceObserver.js (3059B)
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 // @ts-check 5 6 /** 7 * @template P 8 * @typedef {import("react-redux").ResolveThunks<P>} ResolveThunks<P> 9 */ 10 11 /** 12 * @typedef {import("../../@types/perf").State} StoreState 13 */ 14 15 /** 16 * @typedef {object} StateProps 17 * @property {import("../../@types/perf").RecordingSettings} recordingSettingsFromRedux 18 * @property {import("../../@types/perf").PageContext} pageContext 19 * @property {string[]} supportedFeatures 20 */ 21 22 /** 23 * @typedef {object} ThunkDispatchProps 24 * @property {typeof actions.updateSettingsFromPreferences} updateSettingsFromPreferences 25 */ 26 27 /** 28 * @typedef {ResolveThunks<ThunkDispatchProps>} DispatchProps 29 * @typedef {StateProps & DispatchProps} Props 30 */ 31 32 "use strict"; 33 34 // These functions live in a JSM file so that this can be used both by this 35 // CommonJS DevTools environment and the popup which isn't in such a context. 36 const { 37 getRecordingSettings, 38 setRecordingSettings, 39 addPrefObserver, 40 removePrefObserver, 41 } = ChromeUtils.importESModule( 42 "resource://devtools/shared/performance-new/prefs-presets.sys.mjs" 43 ); 44 const { 45 PureComponent, 46 } = require("resource://devtools/client/shared/vendor/react.mjs"); 47 const { 48 connect, 49 } = require("resource://devtools/client/shared/vendor/react-redux.js"); 50 51 const selectors = require("resource://devtools/client/performance-new/store/selectors.js"); 52 const actions = require("resource://devtools/client/performance-new/store/actions.js"); 53 54 /** 55 * This component mirrors the settings in the redux store and the preferences in 56 * Firefox. 57 * 58 * @augments {React.PureComponent<Props>} 59 */ 60 class ProfilerPreferenceObserver extends PureComponent { 61 componentDidMount() { 62 this._updateSettingsFromPreferences(); 63 addPrefObserver(this._updateSettingsFromPreferences); 64 } 65 66 componentDidUpdate() { 67 const { recordingSettingsFromRedux, pageContext } = this.props; 68 setRecordingSettings(pageContext, recordingSettingsFromRedux); 69 } 70 71 componentWillUnmount() { 72 removePrefObserver(this._updateSettingsFromPreferences); 73 } 74 75 _updateSettingsFromPreferences = () => { 76 const { updateSettingsFromPreferences, pageContext, supportedFeatures } = 77 this.props; 78 79 const recordingSettingsFromPrefs = getRecordingSettings( 80 pageContext, 81 supportedFeatures 82 ); 83 updateSettingsFromPreferences(recordingSettingsFromPrefs); 84 }; 85 86 render() { 87 return null; 88 } 89 } 90 91 /** 92 * @param {StoreState} state 93 * @returns {StateProps} 94 */ 95 function mapStateToProps(state) { 96 return { 97 recordingSettingsFromRedux: selectors.getRecordingSettings(state), 98 pageContext: selectors.getPageContext(state), 99 supportedFeatures: selectors.getSupportedFeatures(state), 100 }; 101 } 102 103 const mapDispatchToProps = { 104 updateSettingsFromPreferences: actions.updateSettingsFromPreferences, 105 }; 106 107 module.exports = connect( 108 mapStateToProps, 109 mapDispatchToProps 110 )(ProfilerPreferenceObserver);