Skip to content

Commit a614ad5

Browse files
committed
improve performance by detaching speech computation
1 parent 4b44784 commit a614ad5

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

ts/a11y/explorer/KeyExplorer.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@
2424

2525

2626
import {A11yDocument, HoverRegion, SpeechRegion, LiveRegion} from './Region.js';
27+
import {STATE} from '../../core/MathItem.js';
2728
import type { ExplorerMathItem } from '../explorer.js';
2829
import {Explorer, AbstractExplorer} from './Explorer.js';
2930
import {ExplorerPool} from './ExplorerPool.js';
3031
import {MmlNode} from '../../core/MmlTree/MmlNode.js';
31-
import { honk, InPlace } from '../speech/SpeechUtil.js';
32+
import { honk, InPlace, Timing } from '../speech/SpeechUtil.js';
3233
import {Sre} from '../sre.js';
3334

3435

@@ -446,6 +447,11 @@ export class SpeechExplorer extends AbstractExplorer<string> implements KeyExplo
446447
* @override
447448
*/
448449
public Start() {
450+
// In case the speech is not attached yet, we restart the explorer after a
451+
// fashion.
452+
if (this.item.state() < STATE.ATTACHSPEECH) {
453+
setTimeout(() => this.Start(), Timing.INITIAL);
454+
};
449455
if (!this.attached) return;
450456
if (this.node.hasAttribute('tabindex')) {
451457
this.node.removeAttribute('tabindex');

ts/a11y/semantic-enrich.ts

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ import {MathML} from '../input/mathml.js';
3030
import {SerializedMmlVisitor} from '../core/MmlTree/SerializedMmlVisitor.js';
3131
import {OptionList, expandable} from '../util/Options.js';
3232
import {Sre} from './sre.js';
33-
import { buildSpeech } from './speech/SpeechUtil.js';
34-
33+
import { buildSpeech, Timing } from './speech/SpeechUtil.js';
3534
import { 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
*/

ts/a11y/speech/SpeechUtil.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,12 @@ export enum InPlace {
223223
SUMMARY
224224
}
225225

226+
/**
227+
* Some timing constants for asynchronous speech computation.
228+
*/
229+
export const Timing = {
230+
THRESHOLD: 250,
231+
INITIAL: 100,
232+
INTERMEDIATE: 10
233+
};
234+

0 commit comments

Comments
 (0)