Add core promise-based typeset and convert functions, and simplify those in Startup. #1231
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.
Now that both speech and output (when font data is loaded) use promises and retries, it seems appropriate to add promise-based calls to the core MathDocument for typesetting and conversion, like the ones in the Startup module.
This PR adds those functions. In particular, new
renderPromise()
,rerenderPromise()
, andconvertPromise()
methods are added to theMathDocument
class, and the Startup module now takes advantage of these when creatingMathJax.typesetPromise()
and the various conversion functions likeMathJax.tex2chtmlPromise()
.The old
MathJax.typesetPromise()
and conversion functions had been modified to use theMathJax.startup.promise
to make sure that they operated serially. The new MathDocument methods now do that using a new_readyPromise
, and a newwhenReady()
method is added to the MathDocument to make that easier. This function takes an action to be performed when the current_readyPromise
has been resolved, and resets that promise to a new promise that resolves when the action is complete. (This is effectively makes a queue of actions to be performed serially, with actions that return promises causing the following actions to wait until the earlier ones complete.) Some care is taken to prevent a circular dependency where the action performed bywhenReady()
tries to wait on the promise that it is supposed to resolve. (See the comments in that function.) That way, even thoughrenderPromise()
useswhenReady()
itself, you can still callrenderPromise()
from within an action that appears within anotherwhenReady()
call, as is done in some of the functions created by the Startup module.The
handleRetriesFor()
function, which handles promises created by loading of extensions, is extended to handle async functions, since those are used in the new promise-based rendering and conversion functions. New tests are added to cover this case in theRetries.test.ts
file.All of the promise-based functions wait not just for retry promises, but also for any promises created during processing of the document's renderActions (like those created for handling the speech creation). The variable use to store these has been made private and renamed
_actionPromises
, and service routinessavePromise()
,clearPromises()
andactionPromises()
have been added in order to manipulate the list of saved promises, rather than dealing with the promise array directly. So the speech component callsdocument.savePromise()
rather thandocument.renderPromises.push()
as it used to.The Startup module has been updated to take advantage of the new promise-based code of the document. In the past, it used its own
Startup.promise
to serialize the promise-based calls, but it now uses that only to indicate when the initial typesetting is complete, and falls back on thewhenReady()
function to handle the serialization. It also used to have arerenderPromise
that was used for the menu code when a re-render action was necessary when a component was loaded (it is only needed of anything has been typset already, so no re-rendering needs to be done during startup when the menu may be loading user-selected components.) This promise has been replaced by a booleanhasTypeset
indicating that typesetting has occurred, and a rerender is needed.