@@ -30,8 +30,7 @@ import {MathML} from '../input/mathml.js';
3030import { SerializedMmlVisitor } from '../core/MmlTree/SerializedMmlVisitor.js' ;
3131import { OptionList , expandable } from '../util/Options.js' ;
3232import { Sre } from './sre.js' ;
33- import { buildSpeech } from './speech/SpeechUtil.js' ;
34-
33+ import { buildSpeech , Timing } from './speech/SpeechUtil.js' ;
3534import { GeneratorPool } from './speech/GeneratorPool.js' ;
3635
3736/*==========================================================================*/
@@ -51,7 +50,7 @@ newState('ENRICHED', 30);
5150/**
5251 * Add STATE value for adding speech (after TYPESET)
5352 */
54- newState ( 'ATTACHSPEECH' , 155 ) ;
53+ newState ( 'ATTACHSPEECH' , 205 ) ;
5554
5655/*==========================================================================*/
5756
@@ -415,21 +414,49 @@ export function EnrichedMathDocumentMixin<N, T, D, B extends MathDocumentConstru
415414 ) ;
416415 }
417416
417+ private DELAY : number = Timing . INITIAL ;
418+ private awaitingSpeech : MathItem < N , T , D > [ ] = [ ] ;
419+
418420 /**
419421 * Attach speech from a MathItem to a node
420422 */
421423 public attachSpeech ( ) {
422424 if ( ! this . processed . isSet ( 'attach-speech' ) ) {
423425 if ( this . options . enableSpeech || this . options . enableBraille ) {
424- for ( const math of this . math ) {
425- ( math as EnrichedMathItem < N , T , D > ) . attachSpeech ( this ) ;
426- }
426+ this . awaitingSpeech = Array . from ( this . math ) ;
427+ this . attachSpeechStart ( ) ;
427428 }
428- this . processed . set ( 'attach-speech' ) ;
429429 }
430430 return this ;
431431 }
432432
433+ /**
434+ * Start attaching speech to nodes.
435+ */
436+ private attachSpeechStart ( ) {
437+ if ( this . awaitingSpeech . length ) {
438+ setTimeout (
439+ ( ) => this . attachSpeechLoop ( ) ,
440+ this . DELAY ) ;
441+ } else {
442+ this . processed . set ( 'attach-speech' ) ;
443+ }
444+ }
445+
446+ /**
447+ * Loops through math items to attach speech until the timeout threshold is reached.
448+ */
449+ private attachSpeechLoop ( ) {
450+ const timeStart = new Date ( ) . getTime ( ) ;
451+ const timeEnd = timeStart + Timing . THRESHOLD ;
452+ while ( this . awaitingSpeech . length && new Date ( ) . getTime ( ) < timeEnd ) {
453+ const math = this . awaitingSpeech . shift ( ) ;
454+ ( math as EnrichedMathItem < N , T , D > ) . attachSpeech ( this ) ;
455+ }
456+ this . DELAY = Timing . INTERMEDIATE ;
457+ this . attachSpeechStart ( ) ;
458+ }
459+
433460 /**
434461 * Enrich the MathItems in this MathDocument
435462 */
0 commit comments