Skip to content

Commit f04f5a1

Browse files
committed
Merge branch 'server-state'
2 parents 465daff + 4c0d444 commit f04f5a1

File tree

2 files changed

+45
-28
lines changed

2 files changed

+45
-28
lines changed

lib/mj-page.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,21 @@ var defaults = {
6060
timeout: 60 * 1000, // 60 second timeout before restarting MathJax
6161
};
6262

63+
//
64+
// The MathJax server states
65+
//
66+
var STATE = {
67+
STOPPED: 1, // no DOM or MathJax available
68+
STARTED: 2, // DOM loaded, MathJax starting up
69+
READY: 3, // MathJax initialized and ready to process math
70+
BUSY: 4 // MathJax currently processing math
71+
};
72+
6373
var MathJaxPath = "file://"+require.resolve('MathJax/unpacked/MathJax');
64-
var MathJaxConfig; // configuration for when starting MathJax
65-
var MathJax; // filled in once MathJax is loaded
66-
var serverStarted = false; // true when the MathJax DOM has been created
67-
var serverReady = false; // true when MathJax has done its initial typeset (loaded all components)
68-
var serverBusy = false; // true when MathJax is working on a job in the que
69-
var timer; // used to reset MathJax if it runs too long
74+
var MathJaxConfig; // configuration for when starting MathJax
75+
var MathJax; // filled in once MathJax is loaded
76+
var serverState = STATE.STOPPED; // nothing loaded yet
77+
var timer; // used to reset MathJax if it runs too long
7078

7179
var document, window, content, html; // the DOM elements
7280

@@ -312,7 +320,7 @@ function ConfigureMathJax() {
312320
MathJax.InputJax.AsciiMath.annotationEncoding = null;
313321
});
314322
}
315-
serverReady = true;
323+
serverState = STATE.READY;
316324
MathJax.Hub.Queue(StartQueue);
317325
});
318326

@@ -340,6 +348,7 @@ function Insert(dst,src) {
340348
// Load MathJax into the DOM
341349
//
342350
function StartMathJax() {
351+
serverState = STATE.STARTED;
343352
var script = document.createElement("script");
344353
script.src = MathJaxPath;
345354
document.head.appendChild(script);
@@ -527,21 +536,19 @@ function TypesetDone(result,selector) {
527536
//
528537
result.html = content.innerHTML;
529538
callback(result);
539+
serverState = STATE.READY;
530540
StartQueue();
531541
}
532542

533543
//
534544
// Start typesetting the queued expressions
535545
//
536546
function StartQueue() {
537-
serverBusy = true;
538547
data = callback = null; // clear existing equation, if any
539548
errors = []; // clear any errors
540-
if (!queue.length) { // return if nothing to do
541-
serverBusy = false;
542-
return;
543-
}
549+
if (!queue.length) return // return if nothing to do
544550

551+
serverState = STATE.BUSY;
545552
var result = {}, $$ = window.Array;
546553

547554
//
@@ -626,12 +633,10 @@ function StartQueue() {
626633
function RestartMathJax() {
627634
if (timer) {
628635
MathJax.Hub.queue.queue = []; // clear MathJax queue, so pending operations won't fire
629-
MathJax = timer = null;
636+
MathJax = timer = window = document = html = content = null;
630637
ReportError("Timeout waiting for MathJax: restarting");
631-
serverReady = false;
638+
serverState = STATE.STOPPED;
632639
}
633-
serverStarted = true;
634-
serverBusy = false;
635640
GetWindow();
636641
ConfigureMathJax();
637642
StartMathJax();
@@ -654,8 +659,8 @@ exports.typeset = function (data,callback) {
654659
options[id] = (data.hasOwnProperty(id) ? data[id]: defaults[id]);
655660
}}
656661
queue.push([options,callback]);
657-
if (!serverStarted) {RestartMathJax()}
658-
if (serverReady && !serverBusy) StartQueue();
662+
if (serverState == STATE.STOPPED) {RestartMathJax()}
663+
if (serverState == STATE.READY) StartQueue();
659664
}
660665

661666
//

lib/mj-single.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
6676
var MathJaxPath = "file://"+require.resolve('MathJax/unpacked/MathJax');
6777
var BatikRasterizerPath = path.resolve(__dirname,'..','batik/batik-rasterizer.jar');
68-
var MathJaxConfig; // configuration for when starting MathJax
78+
var MathJaxConfig; // configuration for when starting MathJax
6979
var 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
7281
var timer; // used to reset MathJax if it runs too long
7382
var 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
//
321330
function 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() {
551564
function 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

Comments
 (0)