Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to cancel tasks, and update to allow components to work in node #1230

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions components/mjs/a11y/sre/workerpool/speech-workerpool.js
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
import {VERSION} from '#js/components/version.js';
export * from '#js/a11y/sre/speech-workerpool.js';

if (typeof MathJax !== 'undefined' && MathJax.loader) {
MathJax.loader.checkVersion('sre/speech-workerpool', VERSION, 'sre/speech-workerpool');
}
2 changes: 2 additions & 0 deletions components/mjs/a11y/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import {Loader} from '#js/components/loader.js';
import '../input/mml/init.js';
import {Sre} from './sre/sre.js';
import './semantic-enrich/semantic-enrich.js';
import './speech/speech.js';
import './explorer/explorer.js';

Loader.preLoaded(
'a11y/sre',
'a11y/semantic-enrich',
'a11y/speech',
'a11y/explorer'
);

Expand Down
7 changes: 6 additions & 1 deletion components/mjs/core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@ if (MathJax.startup) {
MathJax.startup.useAdaptor('browserAdaptor');
}
if (MathJax.loader) {
MathJax._.mathjax.mathjax.asyncLoad = (name => MathJax.loader.load(name));
const config = MathJax.config.loader;
MathJax._.mathjax.mathjax.asyncLoad = (
(name) => name.substring(0, 5) === 'node:'
? config.require(name)
: MathJax.loader.load(name).then(result => result[0])
);
}
3 changes: 3 additions & 0 deletions lab/build/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
global.SREfeature = {
json: '../../../bundle/sre/mathmaps'
};
1 change: 1 addition & 0 deletions lab/build/sre.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import './init.js';
export * from '#js/a11y/sre/sre.js';
2 changes: 0 additions & 2 deletions ts/a11y/semantic-enrich.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,6 @@ export function EnrichedMathDocumentMixin<
public static OPTIONS: OptionList = {
...BaseDocument.OPTIONS,
enableEnrichment: true,
enableSpeech: true,
enableBraille: true,
enrichError: (
doc: EnrichedMathDocument<N, T, D>,
math: EnrichedMathItem<N, T, D>,
Expand Down
29 changes: 22 additions & 7 deletions ts/a11y/speech.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,20 @@ export function SpeechMathItemMixin<
if (
this.isEscaped ||
!(document.options.enableSpeech || document.options.enableBraille)
)
) {
return;
}
document.getWebworker();
this.generatorPool.init(
document.options,
document.adaptor,
document.webworker
);
try {
this.outputData.mml = this.toMathML(this.root, this);
this.generatorPool.Speech(this);
} catch (err) {
document.options.speechError(document, this, err);
}
this.outputData.mml = this.toMathML(this.root, this);
const promise = this.generatorPool
.Speech(this)
.catch((err) => document.options.speechError(document, this, err));
document.renderPromises.push(promise);
}

/**
Expand All @@ -136,6 +136,13 @@ export function SpeechMathItemMixin<
// // typeset math here. trhis should undo whatever was done
// // by the attachSpeech() method.
// }

/**
* @override
*/
clear() {
this.generatorPool.cancel(this);
}
};
}

Expand Down Expand Up @@ -326,6 +333,14 @@ export function SpeechMathDocumentMixin<
}
return this;
}

/**
* @override
*/
public async done() {
await this.webworker.Stop();
return super.done();
}
};
}

Expand Down
55 changes: 28 additions & 27 deletions ts/a11y/speech/GeneratorPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
* limitations under the License.
*/

/**
* @file Speech generator collections for enrichment and explorers.
*
* @author v.sorge@mathjax.org (Volker Sorge)
*/

import { OptionList } from '../../util/Options.js';
import { LiveRegion } from '../explorer/Region.js';
import {
Expand All @@ -26,13 +32,6 @@ import {
import { DOMAdaptor } from '../../core/DOMAdaptor.js';
import { MathItem } from '../../core/MathItem.js';
import { WorkerHandler } from './WebWorker.js';
import { PromiseFunctions } from './MessageTypes.js';

/**
* @file Speech generator collections for enrichment and explorers.
*
* @author v.sorge@mathjax.org (Volker Sorge)
*/

/**
* @template N The HTMLElement node class
Expand All @@ -56,14 +55,6 @@ export class GeneratorPool<N, T, D> {
*/
public promise: Promise<void> = Promise.resolve();

protected getPromise() {
let chain: PromiseFunctions;
this.promise = new Promise<void>((res, rej) => {
chain = { resolve: res, reject: rej };
});
return chain;
}

/**
* The adaptor to work with typeset nodes.
*/
Expand Down Expand Up @@ -124,11 +115,21 @@ export class GeneratorPool<N, T, D> {
* Compute speech using the original MathML element as reference.
*
* @param {MathItem} item The MathItem to add speech to
* @returns {Promise<void>} The promise that resolves when the command is complete
*/
public Speech(item: MathItem<N, T, D>) {
public Speech(item: MathItem<N, T, D>): Promise<void> {
const mml = item.outputData.mml;
const options = Object.assign({}, this.options, { modality: 'speech' });
this.webworker.Speech(mml, options, item, this.getPromise());
return (this.promise = this.webworker.Speech(mml, options, item));
}

/**
* Cancel a pending speech task
*
* @param {MathItem} item The MathItem whose task is to be cancelled
*/
public cancel(item: MathItem<N, T, D>) {
this.webworker.Cancel(item);
}

/**
Expand Down Expand Up @@ -258,34 +259,34 @@ export class GeneratorPool<N, T, D> {
* Cycles rule sets for the speech generator.
*
* @param {MathItem} item The MathItem whose rule set is changing
* @returns {Promise<void>} A promise that resolves when the command completes
*/
public nextRules(item: MathItem<N, T, D>) {
public nextRules(item: MathItem<N, T, D>): Promise<void> {
const options = this.getOptions(item.typesetRoot);
this.update(options);
this.webworker.nextRules(
return (this.promise = this.webworker.nextRules(
item.outputData.mml,
Object.assign({}, this.options, { modality: 'speech' }),
item,
this.getPromise()
);
item
));
}

/**
* Cycles style or preference settings for the speech generator.
*
* @param {N} node The typeset node.
* @param {MathItem} item The MathItem whose preferences are changing
* @returns {Promise<void>} A promise that resolves when the command completes
*/
public nextStyle(node: N, item: MathItem<N, T, D>) {
public nextStyle(node: N, item: MathItem<N, T, D>): Promise<void> {
const options = this.getOptions(item.typesetRoot);
this.update(options);
this.webworker.nextStyle(
return (this.promise = this.webworker.nextStyle(
item.outputData.mml,
Object.assign({}, this.options, { modality: 'speech' }),
this.adaptor.getAttribute(node, 'data-semantic-id'),
item,
this.getPromise()
);
item
));
}

/**
Expand Down
8 changes: 3 additions & 5 deletions ts/a11y/speech/MessageTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@
* @author v.sorge@mathjax.org (Volker Sorge)
*/

export interface PromiseFunctions {
resolve: () => void;
reject: (cmd: string) => void;
}

export type Message = { [key: string]: any };

export type WorkerCommand = {
Expand All @@ -38,3 +33,6 @@ export type PoolCommand = {
cmd: string;
data: WorkerCommand | Message;
};

export type Structure = { [id: string]: any };
export type StructureData = Structure | string;
Loading