summaryrefslogtreecommitdiffstats
path: root/devtools/client/accessibility/components/AuditController.js
blob: e6ada9a0a9a78dd56ffe194d96a6ebc873d94650 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const React = require("resource://devtools/client/shared/vendor/react.js");
const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.js");

class AuditController extends React.Component {
  static get propTypes() {
    return {
      accessibleFront: PropTypes.object.isRequired,
      children: PropTypes.any,
    };
  }

  constructor(props) {
    super(props);

    const {
      accessibleFront: { checks },
    } = props;
    this.state = {
      checks,
    };

    this.onAudited = this.onAudited.bind(this);
  }

  // FIXME: https://bugzilla.mozilla.org/show_bug.cgi?id=1774507
  UNSAFE_componentWillMount() {
    const { accessibleFront } = this.props;
    accessibleFront.on("audited", this.onAudited);
  }

  componentDidMount() {
    this.maybeRequestAudit();
  }

  componentDidUpdate() {
    this.maybeRequestAudit();
  }

  componentWillUnmount() {
    const { accessibleFront } = this.props;
    accessibleFront.off("audited", this.onAudited);
  }

  onAudited() {
    const { accessibleFront } = this.props;
    if (accessibleFront.isDestroyed()) {
      // Accessible front is being removed, stop listening for 'audited' events.
      accessibleFront.off("audited", this.onAudited);
      return;
    }

    this.setState({ checks: accessibleFront.checks });
  }

  maybeRequestAudit() {
    const { accessibleFront } = this.props;
    if (accessibleFront.isDestroyed()) {
      // Accessible front is being removed, stop listening for 'audited' events.
      accessibleFront.off("audited", this.onAudited);
      return;
    }

    if (accessibleFront.checks) {
      return;
    }

    accessibleFront.audit().catch(error => {
      // If the actor was destroyed (due to a connection closed for instance) do
      // nothing, otherwise log a warning
      if (!accessibleFront.isDestroyed()) {
        console.warn(error);
      }
    });
  }

  render() {
    const { children } = this.props;
    const { checks } = this.state;

    return React.Children.only(React.cloneElement(children, { checks }));
  }
}

module.exports = AuditController;