Add ability to cancel tasks, and update to allow components to work in node #1230
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
The main purposes of this PR are to:
It also changes how the worker handles promises. Rather than having the GeneratorPool create promises, the WorkerHandler's
Post()
command now does that automatically, so every command send to the worker pool has a promise that resolves when that command is complete.This change also includes the ability to pass data back from the worker to the client via that promise. This allows the worker to send the speech structure data back via the promise rather than a client
Attach()
command. The client now uses that data to do the attach action itself. This allows the worker to start processing other commands while the client is attaching the speech from the previous one, since the worker will have finished its command when it passes back the speech structure, rather than having to wait fo the client to attach and then finish.The Details
In the worker pool configuration file, we add the verisons check (so we don't get an error message about missing version information when a node application's
LiteIFrame
loads the worker pool data viaasyncLoad()
).We add the speech component to the
a11y/util.js
so that the combined components that include the assisitive tools will include the speech component.We extend the
asyncLoad()
definition in the core component to potentially handle loading node packages (we do that in theLiteIFrame
by loading thenode:module
node package). In a node application that has set therequire
configuration option properly to either the noderequire
or to(file) => import(file)
, or an equivalent, this will allow loading of core node modules.The version of SRE in the
lab
directory now sets up the mathmaps path to work in the lab. (The typo that I had in theSREfeature
that you corrected actually allowed it to work before, but now that that has been corrected, it no longer does, but this takes care of that.)The speech and Braille controls are removed from the semantic-enrich component, since they are not used there, and are already in the speech component, where they are used.
In
speech.ts
, the GeneratorPool'sSpeech()
function now returns a promise (see below), so we take advantage of that insteach of using a try/catch construction. We also push the promise into the document'srenderPromises
array so thatMathJax.typesetPromise()
will wait for it before resolving. That way, the speech will be on the DOM nodes when the typeset promise resolves (as it is in beta.7).The MathItem's
clear()
method now cancels its speech task, if there is one.The MathDocument now gets a
done()
method that terminates the worker pool.In
GeneratorPool.ts
:Post()
command of theWorkerHandler
now creates promises automatically (see below), we don't need to get our own promises.Promise<any>
notPromise<void>
.Speech()
command now saves that promise and returns it (as donextRules
andnextStyle
).cancel()
method cancels any task saved for this MathItem.In
MessageTypes.ts
, thePromiseFunctions
are no longer needed, and we add some types for the speech structure that will be passed back from the worker.In
WebWorker.ts
:resolve()
function can now pass an argument.Post()
method now creates a promise for the task automatically.Speech()
method is moved down to be next to thenextRules
andnextStyle
methods.Cancel()
method is added to allow a pending task to be canceled (removed from the task list) and its promise rejected. We might want to make a convention for this so that warnings aren't produced when tasks are canceled explicitly.Speech()
command is now anasync
function that waits for the worker to return the speech structure and then callsAttach()
on using that. (The worker no longer callsAttach()
itself, but just returns the data, so it can go on to do other tasks while the speech is being attached.)nextRules
andnextStyle
.Attach()
function has been moved out the worker commands and is now just a regular method. It is also broken into three parts (rather than creating functions internally), andsetSpecialAttributes()
is moved here along with the other separated outsetSpeechAttribute()
andsetSpeechAttributes()
.Terminate()
method now rejects all the tasks in the list, and before terminating the worker pool.Stop()
method now waits for the termination to complete before removing the iframe.Finished()
method now returns either the result from the worker, or an error message from it.In
speech-worker.ts
:copyStructure()
commands.then()
andcatch()
rather than a try/catch structure. An error state is indicated by anerror
property of themsg
passed toFinished()
(see below).WorkerFunction
andWorkerResult
types for more specificity.feature()
command is removed, since SRE is already configured and loaded, so this would have no effect.setup()
command now waits for the setup to take effect (though the command is not used currently).Finished()
now takes amsg
parameter that is sent back to the client. Thesuccess
property is set according to whether there is an error message or not.Speech()
action is simplified a bit, and now returns the speech structure data rather than asking the client to attach. The client gets the structure data from the promise it got when posting the worker command, and does the attach operation itself.The
LiteIFrame
element indicates thatasyncLoad
needs to load a node module so that theasyncLoad
from thecore
component will not use the Package module's loader.The
loader.ts
file now allows theLoad()
function to return the results of the load operations (i.e., the exported values of the packages that were loaded). This allows theLiteIFrame
to get the exported values from thespeech-workerpool
when components are being used in node. The results are stored in newresults
properties of thePackage
data (see below).In
package.ts
, we now retain the result of loading a package, i.e., its exported values, so that the component loader can return those.In
startup.ts
, we fix a typo with the document used for thetoMML()
function (argh), and create a newMathJax.done()
function that is used to terminate the worker pool, if it was started.In
MathDocument.ts
:done()
method that can be used to clean up anything when the document is no longer needed. (We use it to terminate the worker pool in the speech handler.)clearMathItemsWithin()
now calls each MathItem'sclear()
method (used to cancel its speech task, if any).In
MathItem.ts
, we add the newclear()
method, and call it from theremoveFromDocument()
method, so that if a MathItem is removed, its tasks are canceled.Finally, in
HTMLMathItem.ts
, we make sure the super-classremoveFromDocument()
is called (so theclear()
is performed).