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 core promise-based typeset and convert functions, and simplify those in Startup. #1231

Open
wants to merge 2 commits into
base: update/node-workers
Choose a base branch
from

Conversation

dpvc
Copy link
Member

@dpvc dpvc commented Mar 5, 2025

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(), and convertPromise() methods are added to the MathDocument class, and the Startup module now takes advantage of these when creating MathJax.typesetPromise() and the various conversion functions like MathJax.tex2chtmlPromise().

The old MathJax.typesetPromise() and conversion functions had been modified to use the MathJax.startup.promise to make sure that they operated serially. The new MathDocument methods now do that using a new _readyPromise, and a new whenReady() 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 by whenReady() tries to wait on the promise that it is supposed to resolve. (See the comments in that function.) That way, even though renderPromise() uses whenReady() itself, you can still call renderPromise() from within an action that appears within another whenReady() 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 the Retries.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 routines savePromise(), clearPromises() and actionPromises() have been added in order to manipulate the list of saved promises, rather than dealing with the promise array directly. So the speech component calls document.savePromise() rather than document.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 the whenReady() function to handle the serialization. It also used to have a rerenderPromise 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 boolean hasTypeset indicating that typesetting has occurred, and a rerender is needed.

@dpvc dpvc requested a review from zorkow March 5, 2025 21:01
@dpvc dpvc added this to the v4.0 milestone Mar 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant