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
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
160 changes: 104 additions & 56 deletions testsuite/tests/util/Retries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,88 +13,136 @@ type MathJaxGlobal = MathJaxObject & {
};
const MathJax: MathJaxGlobal = Object.assign(MJX, {
Callback: {
After(code: Function) {
setTimeout(code, 10);
After(code: () => void) {
setTimeout(code, 1);
},
mock() {return Object.assign(() => {}, {isCallback: true})}
}
});

/**********************************************************************************/
/**********************************************************************************/

describe('handleRetriesFor() and retryAfter()', () => {

/********************************************************************************/

test('handleRetriesFor() then/catch getting called', () => {
expect(handleRetriesFor(() => 'success')).resolves.toBe('success');
expect(handleRetriesFor(() => {throw Error('failed')})).rejects.toThrow('failed');
});

/********************************************************************************/

test('handleRetriesFor().then called after 3 retries', () => {
let n = 0;
expect(
handleRetriesFor(() => {
if (++n < 3) {
let p = new Promise<void>((ok, _fail) => {
setTimeout(() => ok(), 10);
});
retryAfter(p);
}
return 'success';
}).then((result: string) => {
expect(result).toBe('success');
expect(n).toBe(3);
})
)
handleRetriesFor(() => {
if (++n < 3) {
let p = new Promise<void>((ok, _fail) => {
setTimeout(() => ok(), 1);
});
retryAfter(p);
}
return 'success';
}).then((result: string) => {
expect(result).toBe('success');
expect(n).toBe(3);
});
});

/********************************************************************************/

test('handleRetriesFor().catch called for fail on 3rd retry', () => {
let n = 0;
expect(
handleRetriesFor(() => {
if (++n < 3) {
let p = new Promise<void>((ok, fail) => {
setTimeout(() => n < 2 ? ok() : fail('fail'), 10);
});
retryAfter(p);
}
return 'success';
}).catch((result: string) => {
expect(result).toBe('fail');
expect(n).toBe(2);
})
)
handleRetriesFor(() => {
if (++n < 3) {
let p = new Promise<void>((ok, fail) => {
setTimeout(() => n < 2 ? ok() : fail('fail'), 1);
});
retryAfter(p);
}
return 'success';
}).catch((result: string) => {
expect(result).toBe('fail');
expect(n).toBe(2);
});
});

/********************************************************************************/

test('handleRetriesFor().catch called for error on 3rd retry', () => {
let n = 0;
expect(
handleRetriesFor(() => {
if (++n < 3) {
let p = new Promise<void>((ok, _fail) => {
setTimeout(() => ok(), 10);
});
retryAfter(p);
}
throw Error('fail');
}).catch((err: Error) => {
expect(err.message).toBe('fail');
expect(n).toBe(3);
})
)
handleRetriesFor(() => {
if (++n < 3) {
let p = new Promise<void>((ok, _fail) => {
setTimeout(() => ok(), 1);
});
retryAfter(p);
}
throw Error('fail');
}).catch((err: Error) => {
expect(err.message).toBe('fail');
expect(n).toBe(3);
});
});

/********************************************************************************/

test('v2 retry', () => {
let n = 0;
expect(handleRetriesFor(() => {
if (++n < 3) {
throw Object.assign(new Error('restart'), {
restart: MathJax.Callback.mock() // mark this error as a v2 restart
});
}
return 'success';
}).then((result: string) => {
expect(result).toBe('success');
expect(n).toBe(3);
})
)
handleRetriesFor(() => {
if (++n < 3) {
throw Object.assign(new Error('restart'), {
restart: MathJax.Callback.mock() // mark this error as a v2 restart
});
}
return 'success';
}).then((result: string) => {
expect(result).toBe('success');
expect(n).toBe(3);
});
});

/********************************************************************************/

test('handleRetriedFor() async success', () => {
expect(handleRetriesFor(async () => {
const wait = new Promise((ok, _fail) => setTimeout(() => ok('success'), 1));
return (await wait);
})).resolves.toBe('success');
});

/********************************************************************************/

test('handleRetriedFor() async fails', () => {
expect(handleRetriesFor(async () => {
const wait = new Promise((_ok, fail) => setTimeout(() => fail('fail'), 1));
return (await wait);
})).rejects.toBe('fail');
});

/********************************************************************************/

test('handleRetriedFor() async with retry', () => {
let n = 0;
handleRetriesFor(async () => {
if (++n < 3) {
await new Promise<void>((ok, _fail) => setTimeout(ok, 1));
let p = new Promise<void>((ok, _fail) => {
setTimeout(() => ok(), 1);
});
retryAfter(p);
}
return 'success';
}).then((result: string) => {
expect(result).toBe('success');
expect(n).toBe(3);
});
});

/********************************************************************************/

});

/**********************************************************************************/
/**********************************************************************************/
2 changes: 1 addition & 1 deletion ts/a11y/speech.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export function SpeechMathItemMixin<
const promise = this.generatorPool
.Speech(this)
.catch((err) => document.options.speechError(document, this, err));
document.renderPromises.push(promise);
document.savePromise(promise);
}

/**
Expand Down
69 changes: 25 additions & 44 deletions ts/components/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ export interface MathJaxObject extends MJObject {
adaptor: DOMADAPTOR;
elements: any[];
document: MATHDOCUMENT;
promise: Promise<void>;
rerenderPromise: Promise<void>;
promise: Promise<any>;
registerConstructor(name: string, constructor: any): void;
useHandler(name: string, force?: boolean): void;
useAdaptor(name: string, force?: boolean): void;
Expand Down Expand Up @@ -181,7 +180,7 @@ export abstract class Startup {
* (called in the defaultReady() function when MathJax is finished with
* its initial typesetting)
*/
public static promiseResolve: () => void;
public static promiseResolve: (value?: any) => any;
/**
* The function that rejects the first promise defined below
* (called in the defaultReady() function when MathJax's initial
Expand All @@ -193,7 +192,7 @@ export abstract class Startup {
* The promise for the startup process (the initial typesetting).
* It is resolved or rejected in the ready() function.
*/
public static promise = new Promise<void>((resolve, reject) => {
public static promise = new Promise<any>((resolve, reject) => {
Startup.promiseResolve = resolve;
Startup.promiseReject = reject;
});
Expand All @@ -219,11 +218,11 @@ export abstract class Startup {
});

/**
* Non-null when MathJax.typeset() or MathJax.typesetPromise() have been performed
* (so the menu code can tell whether a rerender is needed when components are loaded)
* and then is equal to a promise after which rerendering can occur.
* This is true when MathJax.typeset() or MathJax.typesetPromise() have been called
* (used by the menu code to tell if a rerender action is needed when a component is
* loaded dynamically).
*/
public static rerenderPromise: Promise<void> = null;
public static hasTypeset: boolean = false;

/**
* @param {MmlNode} node The root of the tree to convert to serialized MathML
Expand Down Expand Up @@ -337,19 +336,15 @@ export abstract class Startup {
* Perform the typesetting with handling of retries
*
* @param {any[]} elements The list of elements to typeset
* @returns {Promise<void>} The promise that resolves when elements are typeset
*/
public static typesetPromise(elements: any[]): Promise<void> {
Startup.document.options.elements = elements;
Startup.document.reset();
Startup.rerenderPromise = Startup.promise =
Startup.mathjax.handleRetriesFor(() => {
Startup.document.render();
const promise = Promise.all(Startup.document.renderPromises);
Startup.document.renderPromises = [];
return promise;
});
return Startup.promise;
* @returns {Promise<any>} The promise that resolves when elements are typeset
*/
public static typesetPromise(elements: any[]): Promise<any> {
return Startup.document.whenReady(async () => {
Startup.document.options.elements = elements;
Startup.document.reset();
await Startup.document.renderPromise();
this.hasTypeset = true;
});
}

/**
Expand Down Expand Up @@ -413,15 +408,10 @@ export abstract class Startup {
Startup.document.options.elements = elements;
Startup.document.reset();
Startup.document.render();
if (!Startup.rerenderPromise) {
Startup.rerenderPromise = Startup.promise;
}
this.hasTypeset = true;
};
MathJax.typesetPromise = (elements: any[] = null) => {
Startup.rerenderPromise = Startup.promise = Startup.promise.then(() =>
Startup.typesetPromise(elements)
);
return Startup.promise;
return Startup.typesetPromise(elements);
};
MathJax.typesetClear = (elements: any[] = null) => {
if (elements) {
Expand Down Expand Up @@ -462,13 +452,8 @@ export abstract class Startup {
return Startup.document.convert(math, options);
};
MathJax[name + 'Promise'] = (math: string, options: OptionList = {}) => {
Startup.promise = Startup.promise.then(() => {
options.format = input.name;
return Startup.mathjax.handleRetriesFor(() =>
Startup.document.convert(math, options)
);
});
return Startup.promise;
options.format = input.name;
return Startup.document.convertPromise(math, options);
};
MathJax[oname + 'Stylesheet'] = () =>
Startup.output.styleSheet(Startup.document);
Expand Down Expand Up @@ -497,18 +482,14 @@ export abstract class Startup {
options.format = input.name;
return Startup.toMML(Startup.document.convert(math, options));
};
MathJax[name + '2mmlPromise'] = (
MathJax[name + '2mmlPromise'] = async (
math: string,
options: OptionList = {}
) => {
Startup.promise = Startup.promise.then(() => {
options.end = STATE.CONVERT;
options.format = input.name;
return Startup.mathjax.handleRetriesFor(() =>
Startup.toMML(Startup.document.convert(math, options))
);
});
return Startup.promise;
options.end = STATE.CONVERT;
options.format = input.name;
const node = await Startup.document.convertPromise(math, options)
return Startup.toMML(node);
};
}

Expand Down
Loading