@@ -63,12 +63,21 @@ var defaults = {
6363 timeout : 5 * 1000 , // 5 second timeout before restarting MathJax
6464} ;
6565
66+ //
67+ // The MathJax server states
68+ //
69+ var STATE = {
70+ STOPPED : 1 , // no DOM or MathJax available
71+ STARTED : 2 , // DOM loaded, MathJax starting up
72+ READY : 3 , // MathJax initialized and ready to process math
73+ BUSY : 4 // MathJax currently processing math
74+ } ;
75+
6676var MathJaxPath = "file://" + require . resolve ( 'MathJax/unpacked/MathJax' ) ;
6777var BatikRasterizerPath = path . resolve ( __dirname , '..' , 'batik/batik-rasterizer.jar' ) ;
68- var MathJaxConfig ; // configuration for when starting MathJax
78+ var MathJaxConfig ; // configuration for when starting MathJax
6979var MathJax ; // filled in once MathJax is loaded
70- var serverStarted = false ; // true when the MathJax DOM has been created
71- var serverReady = false ; // true when MathJax has done its initial typeset (loaded all components)
80+ var serverState = STATE . STOPPED ; // nothing loaded yet
7281var timer ; // used to reset MathJax if it runs too long
7382var tmpfile = "/tmp/mj-single-svg" ; // file name prefix to use for temp files
7483
@@ -293,7 +302,7 @@ function ConfigureMathJax() {
293302 MathJax . Hub . Register . StartupHook ( "End" , function ( ) {
294303 MathJax . OutputJax . SVG . resetGlyphs ( true ) ;
295304 MathJax . ElementJax . mml . ID = 0 ;
296- serverReady = true ;
305+ serverState = STATE . READY ;
297306 MathJax . Hub . Queue ( StartQueue ) ;
298307 } ) ;
299308 }
@@ -319,6 +328,7 @@ function Insert(dst,src) {
319328// Load MathJax into the DOM
320329//
321330function StartMathJax ( ) {
331+ serverState = STATE . STARTED ;
322332 var script = document . createElement ( "script" ) ;
323333 script . src = MathJaxPath ;
324334 document . head . appendChild ( script ) ;
@@ -457,6 +467,7 @@ function TypesetDone(result) {
457467 }
458468 if ( errors . length ) { result . errors = errors }
459469 callback ( result ) ;
470+ serverState = STATE . READY ;
460471 StartQueue ( ) ;
461472}
462473
@@ -468,6 +479,9 @@ function StartQueue() {
468479 errors = [ ] ; // clear any errors
469480 if ( ! queue . length ) return ; // return if nothing to do
470481
482+ serverState = STATE . BUSY ;
483+ var result = { } , $$ = window . Array ;
484+
471485 //
472486 // Get the math data and callback
473487 // and set the content with the proper delimiters
@@ -533,7 +547,6 @@ function StartQueue() {
533547 // TypesetDone routine
534548 //
535549 timer = setTimeout ( RestartMathJax , data . timeout ) ;
536- var result = { } , $$ = window . Array ;
537550 HUB . Queue (
538551 $$ ( "Typeset" , HUB ) ,
539552 $$ ( GetMML , result ) ,
@@ -551,11 +564,10 @@ function StartQueue() {
551564function RestartMathJax ( ) {
552565 if ( timer ) {
553566 MathJax . Hub . queue . queue = [ ] ; // clear MathJax queue, so pending operations won't fire
554- MathJax = timer = null ;
567+ MathJax = timer = window = document = html = content = null ;
555568 ReportError ( "Timeout waiting for MathJax: restarting" ) ;
556- serverReady = false ;
569+ serverState = STATE . STOPPED ;
557570 }
558- serverStarted = true ;
559571 GetWindow ( ) ;
560572 ConfigureMathJax ( ) ;
561573 StartMathJax ( ) ;
@@ -581,8 +593,8 @@ exports.typeset = function (data,callback) {
581593 if ( data . state ) { options . state = data . state }
582594 if ( ! delimiters [ options . format ] ) { ReportError ( "Unknown format: " + options . format , callback ) ; return }
583595 queue . push ( [ options , callback ] ) ;
584- if ( ! serverStarted ) { RestartMathJax ( ) }
585- if ( serverReady ) StartQueue ( ) ;
596+ if ( serverState == STATE . STOPPED ) { RestartMathJax ( ) }
597+ if ( serverState == STATE . READY ) StartQueue ( ) ;
586598}
587599
588600//
0 commit comments