Skip to content

Commit

Permalink
Merge branch 'release/9.2.0' of github.com:mermaid-js/mermaid into re…
Browse files Browse the repository at this point in the history
…lease/9.2.0
  • Loading branch information
knsv committed Oct 28, 2022
2 parents 8ad8d39 + 120940f commit d4c19ff
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/mermaid/src/__mocks__/mermaidAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function parse(text: string, parseError?: Function): boolean {
// original version cannot be modified since it was frozen with `Object.freeze()`
export const mermaidAPI = {
render: vi.fn(),
renderAsync: vi.fn(),
parse,
parseDirective: vi.fn(),
initialize: vi.fn(),
Expand Down
23 changes: 23 additions & 0 deletions packages/mermaid/src/mermaid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,29 @@ describe('when using mermaid and ', function () {
expect(mermaidAPI.render).toHaveBeenCalled();
});
});
describe('when using #initThrowsErrorsAsync', function () {
it('should throw error (but still render) if lazyLoadedDiagram fails', async () => {
const node = document.createElement('div');
node.appendChild(document.createTextNode('graph TD;\na;'));

mermaidAPI.setConfig({
lazyLoadedDiagrams: ['this-file-does-not-exist.mjs'],
});
await expect(mermaid.initThrowsErrorsAsync(undefined, node)).rejects.toThrowError(
// this error message is probably different on every platform
// this one is just for vite-note (node/jest/browser may be different)
'Failed to load this-file-does-not-exist.mjs'
);

// should still render, even if lazyLoadedDiagrams fails
expect(mermaidAPI.renderAsync).toHaveBeenCalled();
});

afterEach(() => {
// we modify mermaid config in some tests, so we need to make sure to reset them
mermaidAPI.reset();
});
});

describe('checking validity of input ', function () {
it('should throw for an invalid definition', function () {
Expand Down
41 changes: 32 additions & 9 deletions packages/mermaid/src/mermaid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ const init = async function (
try {
const conf = mermaidAPI.getConfig();
if (conf?.lazyLoadedDiagrams && conf.lazyLoadedDiagrams.length > 0) {
await registerLazyLoadedDiagrams(conf);
await initThrowsErrorsAsync(config, nodes, callback);
} else {
initThrowsErrors(config, nodes, callback);
Expand Down Expand Up @@ -178,10 +177,12 @@ const initThrowsErrors = function (
}
};

let lazyLoadingPromise: Promise<unknown> | undefined = undefined;
let lazyLoadingPromise: Promise<PromiseSettledResult<void>[]> | undefined = undefined;
/**
* @param conf
* @deprecated This is an internal function and should not be used. Will be removed in v10.
* This is an internal function and should not be made public, as it will likely change.
* @internal
* @param conf - Mermaid config.
* @returns An array of {@link PromiseSettledResult}, showing the status of imports.
*/
const registerLazyLoadedDiagrams = async (conf: MermaidConfig) => {
// Only lazy load once
Expand All @@ -195,7 +196,7 @@ const registerLazyLoadedDiagrams = async (conf: MermaidConfig) => {
})
);
}
await lazyLoadingPromise;
return await lazyLoadingPromise;
};

let loadingPromise: Promise<unknown> | undefined = undefined;
Expand All @@ -218,9 +219,20 @@ const loadExternalDiagrams = async (conf: MermaidConfig) => {
};

/**
* @deprecated This is an internal function and should not be used. Will be removed in v10.
* Equivalent to {@link init()}, except an error will be thrown on error.
*
* @alpha
* @deprecated This is an internal function and will very likely be modified in v10, or earlier.
* We recommend staying with {@link initThrowsErrors} if you don't need `lazyLoadedDiagrams`.
*
* @param config - **Deprecated** Mermaid sequenceConfig.
* @param nodes - One of:
* - A DOM Node
* - An array of DOM nodes (as would come from a jQuery selector)
* - A W3C selector, a la `.mermaid` (default)
* @param callback - Function that is called with the id of each generated mermaid diagram.
* @returns Resolves on success, otherwise the {@link Promise} will be rejected.
*/

const initThrowsErrorsAsync = async function (
config?: MermaidConfig,
// eslint-disable-next-line no-undef
Expand All @@ -229,6 +241,14 @@ const initThrowsErrorsAsync = async function (
callback?: Function
) {
const conf = mermaidAPI.getConfig();

const registerLazyLoadedDiagramsErrors: Error[] = [];
for (const registerResult of await registerLazyLoadedDiagrams(conf)) {
if (registerResult.status == 'rejected') {
registerLazyLoadedDiagramsErrors.push(registerResult.reason);
}
}

if (config) {
// This is a legacy way of setting config. It is not documented and should be removed in the future.
// @ts-ignore: TODO Fix ts errors
Expand Down Expand Up @@ -303,9 +323,10 @@ const initThrowsErrorsAsync = async function (
handleError(error, errors, mermaid.parseError);
}
}
if (errors.length > 0) {
const allErrors = [...registerLazyLoadedDiagramsErrors, ...errors];
if (allErrors.length > 0) {
// TODO: We should be throwing an error object.
throw errors[0];
throw allErrors[0];
}
};

Expand Down Expand Up @@ -500,6 +521,7 @@ const mermaid: {
renderAsync: typeof renderAsync;
init: typeof init;
initThrowsErrors: typeof initThrowsErrors;
initThrowsErrorsAsync: typeof initThrowsErrorsAsync;
initialize: typeof initialize;
initializeAsync: typeof initializeAsync;
contentLoaded: typeof contentLoaded;
Expand All @@ -514,6 +536,7 @@ const mermaid: {
renderAsync,
init,
initThrowsErrors,
initThrowsErrorsAsync,
initialize,
initializeAsync,
parseError: undefined,
Expand Down

0 comments on commit d4c19ff

Please sign in to comment.