AuditController.js (2214B)
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 React = require("resource://devtools/client/shared/vendor/react.mjs"); 8 const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.mjs"); 9 10 class AuditController extends React.Component { 11 static get propTypes() { 12 return { 13 accessibleFront: PropTypes.object.isRequired, 14 children: PropTypes.any, 15 }; 16 } 17 18 constructor(props) { 19 super(props); 20 21 const { 22 accessibleFront: { checks }, 23 } = props; 24 this.state = { 25 checks, 26 }; 27 28 this.onAudited = this.onAudited.bind(this); 29 } 30 31 componentDidMount() { 32 const { accessibleFront } = this.props; 33 accessibleFront.on("audited", this.onAudited); 34 this.maybeRequestAudit(); 35 } 36 37 componentDidUpdate() { 38 this.maybeRequestAudit(); 39 } 40 41 componentWillUnmount() { 42 const { accessibleFront } = this.props; 43 accessibleFront.off("audited", this.onAudited); 44 } 45 46 onAudited() { 47 const { accessibleFront } = this.props; 48 if (accessibleFront.isDestroyed()) { 49 // Accessible front is being removed, stop listening for 'audited' events. 50 accessibleFront.off("audited", this.onAudited); 51 return; 52 } 53 54 this.setState({ checks: accessibleFront.checks }); 55 } 56 57 maybeRequestAudit() { 58 const { accessibleFront } = this.props; 59 if (accessibleFront.isDestroyed()) { 60 // Accessible front is being removed, stop listening for 'audited' events. 61 accessibleFront.off("audited", this.onAudited); 62 return; 63 } 64 65 if (accessibleFront.checks) { 66 return; 67 } 68 69 accessibleFront.audit().catch(error => { 70 // If the actor was destroyed (due to a connection closed for instance) do 71 // nothing, otherwise log a warning 72 if (!accessibleFront.isDestroyed()) { 73 console.warn(error); 74 } 75 }); 76 } 77 78 render() { 79 const { children } = this.props; 80 const { checks } = this.state; 81 82 return React.Children.only(React.cloneElement(children, { checks })); 83 } 84 } 85 86 module.exports = AuditController;