@@ -31,7 +31,6 @@ import {SerializedMmlVisitor} from '../core/MmlTree/SerializedMmlVisitor.js';
3131import { OptionList , expandable } from '../util/Options.js' ;
3232import { Sre } from './sre.js' ;
3333import { buildSpeech } from './speech/SpeechUtil.js' ;
34-
3534import { GeneratorPool } from './speech/GeneratorPool.js' ;
3635
3736/*==========================================================================*/
@@ -46,12 +45,12 @@ export type Constructor<T> = new(...args: any[]) => T;
4645/**
4746 * Add STATE value for being enriched (after COMPILED and before TYPESET)
4847 */
49- newState ( 'ENRICHED' , 30 ) ;
48+ newState ( 'ENRICHED' , STATE . COMPILED + 10 ) ;
5049
5150/**
52- * Add STATE value for adding speech (after TYPESET )
51+ * Add STATE value for adding speech (after INSERTED )
5352 */
54- newState ( 'ATTACHSPEECH' , 155 ) ;
53+ newState ( 'ATTACHSPEECH' , STATE . INSERTED + 10 ) ;
5554
5655/*==========================================================================*/
5756
@@ -243,7 +242,7 @@ export function EnrichedMathItemMixin<N, T, D, B extends Constructor<AbstractMat
243242 *
244243 * @return {[string, string] } Pair comprising speech and braille.
245244 */
246- private existingSpeech ( ) : [ string , string ] {
245+ protected existingSpeech ( ) : [ string , string ] {
247246 const attributes = this . root . attributes ;
248247 let speech = attributes . get ( 'aria-label' ) as string ;
249248 if ( ! speech ) {
@@ -379,6 +378,11 @@ export function EnrichedMathDocumentMixin<N, T, D, B extends MathDocumentConstru
379378 enrich : [ STATE . ENRICHED ] ,
380379 attachSpeech : [ STATE . ATTACHSPEECH ]
381380 } ) ,
381+ speechTiming : {
382+ initial : 100 , // initial delay until starting to add speech
383+ threshold : 250 , // time (in milliseconds) to process speech before letting screen update
384+ intermediate : 10 // delay after processing speech reaches the threshold
385+ } ,
382386 sre : expandable ( {
383387 speech : 'none' , // by default no speech is included
384388 locale : 'en' , // switch the locale
@@ -392,6 +396,16 @@ export function EnrichedMathDocumentMixin<N, T, D, B extends MathDocumentConstru
392396 } )
393397 } ;
394398
399+ /**
400+ * The list of MathItems that need to be processed for speech
401+ */
402+ protected awaitingSpeech : MathItem < N , T , D > [ ] ;
403+
404+ /**
405+ * The identifier from setTimeout for the next speech loop
406+ */
407+ protected speechTimeout : number = 0 ;
408+
395409 /**
396410 * Enrich the MathItem class used for this MathDocument, and create the
397411 * temporary MathItem used for enrchment
@@ -421,15 +435,37 @@ export function EnrichedMathDocumentMixin<N, T, D, B extends MathDocumentConstru
421435 public attachSpeech ( ) {
422436 if ( ! this . processed . isSet ( 'attach-speech' ) ) {
423437 if ( this . options . enableSpeech || this . options . enableBraille ) {
424- for ( const math of this . math ) {
425- ( math as EnrichedMathItem < N , T , D > ) . attachSpeech ( this ) ;
438+ if ( this . speechTimeout ) {
439+ clearTimeout ( this . speechTimeout ) ;
440+ this . speechTimeout = 0 ;
426441 }
442+ this . awaitingSpeech = Array . from ( this . math ) ;
443+ this . speechTimeout = setTimeout (
444+ ( ) => this . attachSpeechLoop ( ) ,
445+ this . options . speechTiming . initial
446+ ) ;
427447 }
428448 this . processed . set ( 'attach-speech' ) ;
429449 }
430450 return this ;
431451 }
432452
453+ /**
454+ * Loops through math items to attach speech until the timeout threshold is reached.
455+ */
456+ protected attachSpeechLoop ( ) {
457+ const timing = this . options . speechTiming ;
458+ const awaitingSpeech = this . awaitingSpeech ;
459+ const timeStart = new Date ( ) . getTime ( ) ;
460+ const timeEnd = timeStart + timing . threshold ;
461+ do {
462+ const math = awaitingSpeech . shift ( ) ;
463+ ( math as EnrichedMathItem < N , T , D > ) . attachSpeech ( this ) ;
464+ } while ( awaitingSpeech . length && new Date ( ) . getTime ( ) < timeEnd ) ;
465+ this . speechTimeout = awaitingSpeech . length ?
466+ setTimeout ( ( ) => this . attachSpeechLoop ( ) , timing . intermediate ) : 0 ;
467+ }
468+
433469 /**
434470 * Enrich the MathItems in this MathDocument
435471 */
0 commit comments