MCPcopy
hub / github.com/markmap/markmap / loadJSItem

Function loadJSItem

packages/markmap-common/src/loader.ts:24–52  ·  view source on GitHub ↗
(item: JSItem, context: unknown)

Source from the content-addressed store, hash-verified

22const cssCache: Record<string, Promise<void> | undefined> = {};
23
24async function loadJSItem(item: JSItem, context: unknown) {
25 const src = (item.type === 'script' && item.data?.src) || '';
26 item.loaded ||= jsCache[src];
27 if (!item.loaded) {
28 const deferred = defer<void>();
29 item.loaded = deferred.promise;
30 if (item.type === 'script') {
31 document.head.append(
32 hm('script', {
33 ...item.data,
34 onLoad: () => deferred.resolve(),
35 onError: deferred.reject,
36 }),
37 );
38 if (!src) {
39 // Run inline script synchronously
40 deferred.resolve();
41 } else {
42 jsCache[src] = item.loaded;
43 }
44 }
45 if (item.type === 'iife') {
46 const { fn, getParams } = item.data;
47 fn(...(getParams?.(context) || []));
48 deferred.resolve();
49 }
50 }
51 await item.loaded;
52}
53
54async function loadCSSItem(item: CSSItem) {
55 const url = (item.type === 'stylesheet' && item.data.href) || '';

Callers 1

loadJSFunction · 0.85

Calls 3

deferFunction · 0.90
fnFunction · 0.85
getParamsFunction · 0.85

Tested by

no test coverage detected