mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-08-23 10:16:43 +02:00
feat: Add initializeAsync
This commit is contained in:
@@ -47,13 +47,7 @@ const init = async function (
|
|||||||
try {
|
try {
|
||||||
const conf = mermaidAPI.getConfig();
|
const conf = mermaidAPI.getConfig();
|
||||||
if (conf?.lazyLoadedDiagrams && conf.lazyLoadedDiagrams.length > 0) {
|
if (conf?.lazyLoadedDiagrams && conf.lazyLoadedDiagrams.length > 0) {
|
||||||
// Load all lazy loaded diagrams in parallel
|
await registerLazyLoadedDiagrams(conf);
|
||||||
await Promise.allSettled(
|
|
||||||
conf.lazyLoadedDiagrams.map(async (diagram: string) => {
|
|
||||||
const { id, detector, loadDiagram } = await import(diagram);
|
|
||||||
addDetector(id, detector, loadDiagram);
|
|
||||||
})
|
|
||||||
);
|
|
||||||
await initThrowsErrorsAsync(config, nodes, callback);
|
await initThrowsErrorsAsync(config, nodes, callback);
|
||||||
} else {
|
} else {
|
||||||
initThrowsErrors(config, nodes, callback);
|
initThrowsErrors(config, nodes, callback);
|
||||||
@@ -164,6 +158,26 @@ const initThrowsErrors = function (
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let lazyLoadingPromise: Promise<unknown> | undefined;
|
||||||
|
/**
|
||||||
|
* @param conf
|
||||||
|
* @deprecated This is an internal function and should not be used. Will be removed in v10.
|
||||||
|
*/
|
||||||
|
const registerLazyLoadedDiagrams = async (conf: MermaidConfig) => {
|
||||||
|
// Only lazy load once
|
||||||
|
// TODO: This is a hack. We should either throw error when new diagrams are added, or load them anyway.
|
||||||
|
if (lazyLoadingPromise === undefined) {
|
||||||
|
// Load all lazy loaded diagrams in parallel
|
||||||
|
lazyLoadingPromise = Promise.allSettled(
|
||||||
|
(conf?.lazyLoadedDiagrams ?? []).map(async (diagram: string) => {
|
||||||
|
const { id, detector, loadDiagram } = await import(diagram);
|
||||||
|
addDetector(id, detector, loadDiagram);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
await lazyLoadingPromise;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated This is an internal function and should not be used. Will be removed in v10.
|
* @deprecated This is an internal function and should not be used. Will be removed in v10.
|
||||||
*/
|
*/
|
||||||
@@ -267,6 +281,15 @@ const initialize = function (config: MermaidConfig) {
|
|||||||
mermaidAPI.initialize(config);
|
mermaidAPI.initialize(config);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param config
|
||||||
|
* @deprecated This is an internal function and should not be used. Will be removed in v10.
|
||||||
|
*/
|
||||||
|
const initializeAsync = async function (config: MermaidConfig) {
|
||||||
|
await registerLazyLoadedDiagrams(config);
|
||||||
|
mermaidAPI.initialize(config);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ##contentLoaded Callback function that is called when page is loaded. This functions fetches
|
* ##contentLoaded Callback function that is called when page is loaded. This functions fetches
|
||||||
* configuration for mermaid rendering and calls init for rendering the mermaid diagrams on the
|
* configuration for mermaid rendering and calls init for rendering the mermaid diagrams on the
|
||||||
@@ -300,7 +323,7 @@ if (typeof document !== 'undefined') {
|
|||||||
* This is provided for environments where the mermaid object can't directly have a new member added
|
* This is provided for environments where the mermaid object can't directly have a new member added
|
||||||
* to it (eg. dart interop wrapper). (Initially there is no parseError member of mermaid).
|
* to it (eg. dart interop wrapper). (Initially there is no parseError member of mermaid).
|
||||||
*
|
*
|
||||||
* @param {function (err, hash)} newParseErrorHandler New parseError() callback.
|
* @param newParseErrorHandler New parseError() callback.
|
||||||
*/
|
*/
|
||||||
const setParseErrorHandler = function (newParseErrorHandler: (err: any, hash: any) => void) {
|
const setParseErrorHandler = function (newParseErrorHandler: (err: any, hash: any) => void) {
|
||||||
mermaid.parseError = newParseErrorHandler;
|
mermaid.parseError = newParseErrorHandler;
|
||||||
@@ -309,6 +332,11 @@ const setParseErrorHandler = function (newParseErrorHandler: (err: any, hash: an
|
|||||||
const parse = (txt: string) => {
|
const parse = (txt: string) => {
|
||||||
return mermaidAPI.parse(txt, mermaid.parseError);
|
return mermaidAPI.parse(txt, mermaid.parseError);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param txt
|
||||||
|
* @deprecated This is an internal function and should not be used. Will be removed in v10.
|
||||||
|
*/
|
||||||
const parseAsync = (txt: string) => {
|
const parseAsync = (txt: string) => {
|
||||||
return mermaidAPI.parseAsync(txt, mermaid.parseError);
|
return mermaidAPI.parseAsync(txt, mermaid.parseError);
|
||||||
};
|
};
|
||||||
@@ -325,6 +353,7 @@ const mermaid: {
|
|||||||
init: typeof init;
|
init: typeof init;
|
||||||
initThrowsErrors: typeof initThrowsErrors;
|
initThrowsErrors: typeof initThrowsErrors;
|
||||||
initialize: typeof initialize;
|
initialize: typeof initialize;
|
||||||
|
initializeAsync: typeof initializeAsync;
|
||||||
contentLoaded: typeof contentLoaded;
|
contentLoaded: typeof contentLoaded;
|
||||||
setParseErrorHandler: typeof setParseErrorHandler;
|
setParseErrorHandler: typeof setParseErrorHandler;
|
||||||
} = {
|
} = {
|
||||||
@@ -337,6 +366,7 @@ const mermaid: {
|
|||||||
init,
|
init,
|
||||||
initThrowsErrors,
|
initThrowsErrors,
|
||||||
initialize,
|
initialize,
|
||||||
|
initializeAsync,
|
||||||
parseError: undefined,
|
parseError: undefined,
|
||||||
contentLoaded,
|
contentLoaded,
|
||||||
setParseErrorHandler,
|
setParseErrorHandler,
|
||||||
|
@@ -43,18 +43,18 @@ function parse(text: string, parseError?: Function): boolean {
|
|||||||
const diagram = new Diagram(text, parseError);
|
const diagram = new Diagram(text, parseError);
|
||||||
return diagram.parse(text, parseError);
|
return diagram.parse(text, parseError);
|
||||||
}
|
}
|
||||||
/* eslint-disable @typescript-eslint/ban-types */
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param text
|
* @param text
|
||||||
* @param parseError
|
* @param parseError
|
||||||
*/
|
*/
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||||
async function parseAsync(text: string, parseError?: Function): Promise<boolean> {
|
async function parseAsync(text: string, parseError?: Function): Promise<boolean> {
|
||||||
addDiagrams();
|
addDiagrams();
|
||||||
const diagram = await getDiagramFromText(text, parseError);
|
const diagram = await getDiagramFromText(text, parseError);
|
||||||
return diagram.parse(text, parseError);
|
return diagram.parse(text, parseError);
|
||||||
}
|
}
|
||||||
/* eslint-enable @typescript-eslint/ban-types */
|
|
||||||
|
|
||||||
export const encodeEntities = function (text: string): string {
|
export const encodeEntities = function (text: string): string {
|
||||||
let txt = text;
|
let txt = text;
|
||||||
|
Reference in New Issue
Block a user