Browse Source

improve rendering MathJax performance

Yuki Takei 8 years ago
parent
commit
6bc40a2920
1 changed files with 8 additions and 8 deletions
  1. 8 8
      resource/js/components/Page/RevisionBody.js

+ 8 - 8
resource/js/components/Page/RevisionBody.js

@@ -1,35 +1,35 @@
 import React from 'react';
 import React from 'react';
 import PropTypes from 'prop-types';
 import PropTypes from 'prop-types';
 
 
+import { debounce } from 'throttle-debounce';
+
 export default class RevisionBody extends React.Component {
 export default class RevisionBody extends React.Component {
 
 
   constructor(props) {
   constructor(props) {
     super(props);
     super(props);
+
+    // create debounced method for rendering MathJax
+    this.renderMathJaxWithDebounce = debounce(200, this.renderMathJax);
   }
   }
 
 
   componentDidMount() {
   componentDidMount() {
     const MathJax = window.MathJax;
     const MathJax = window.MathJax;
     if (MathJax != null && this.props.isMathJaxEnabled && this.props.renderMathJaxOnInit) {
     if (MathJax != null && this.props.isMathJaxEnabled && this.props.renderMathJaxOnInit) {
-      const intervalId = setInterval(() => {
-        if (MathJax.isReady) {
-          this.renderMathJax();
-          clearInterval(intervalId);
-        }
-      }, 100);
+      this.renderMathJaxWithDebounce();
     }
     }
   }
   }
 
 
   componentDidUpdate() {
   componentDidUpdate() {
     const MathJax = window.MathJax;
     const MathJax = window.MathJax;
     if (MathJax != null && this.props.isMathJaxEnabled && this.props.renderMathJaxInRealtime) {
     if (MathJax != null && this.props.isMathJaxEnabled && this.props.renderMathJaxInRealtime) {
-      this.renderMathJax();
+      this.renderMathJaxWithDebounce();
     }
     }
   }
   }
 
 
   componentWillReceiveProps(nextProps) {
   componentWillReceiveProps(nextProps) {
     const MathJax = window.MathJax;
     const MathJax = window.MathJax;
     if (MathJax != null && this.props.isMathJaxEnabled && this.props.renderMathJaxOnInit) {
     if (MathJax != null && this.props.isMathJaxEnabled && this.props.renderMathJaxOnInit) {
-      this.renderMathJax();
+      this.renderMathJaxWithDebounce();
     }
     }
   }
   }