@lynx-js/web-core
Version:
This is an internal experimental package, do not use
86 lines • 3.52 kB
JavaScript
/*
* Copyright 2025 The Lynx Authors. All rights reserved.
* Licensed under the Apache License Version 2.0 that can be found in the
* LICENSE file in the root directory of this source tree.
*/
import { templateManager } from './TemplateManager.js';
import {} from './LynxViewInstance.js';
function createMainThreadLynx(lynxViewInstance) {
const requestAnimationFrameBrowserImpl = requestAnimationFrame;
const cancelAnimationFrameBrowserImpl = cancelAnimationFrame;
const setTimeoutBrowserImpl = setTimeout;
const clearTimeoutBrowserImpl = clearTimeout;
const setIntervalBrowserImpl = setInterval;
const clearIntervalBrowserImpl = clearInterval;
return {
getJSContext() {
return lynxViewInstance.backgroundThread.jsContext;
},
requestAnimationFrame(cb) {
return requestAnimationFrameBrowserImpl(cb);
},
cancelAnimationFrame(handler) {
return cancelAnimationFrameBrowserImpl(handler);
},
__globalProps: lynxViewInstance.globalprops,
getCustomSectionSync(key) {
return templateManager.getBundle(lynxViewInstance.templateUrl)?.customSections?.[key]
?.content;
},
markPipelineTiming: lynxViewInstance.backgroundThread.markTiming.bind(lynxViewInstance.backgroundThread),
SystemInfo: lynxViewInstance.systemInfo,
setTimeout: setTimeoutBrowserImpl,
clearTimeout: clearTimeoutBrowserImpl,
setInterval: setIntervalBrowserImpl,
clearInterval: clearIntervalBrowserImpl,
};
}
export function createMainThreadGlobalAPIs(lynxViewInstance) {
let releaseSetting = '';
return {
__globalProps: lynxViewInstance.globalprops,
SystemInfo: lynxViewInstance.systemInfo,
lynx: createMainThreadLynx(lynxViewInstance),
__OnLifecycleEvent: (data) => {
lynxViewInstance.backgroundThread.jsContext.dispatchEvent({
type: '__OnLifecycleEvent',
data,
});
},
__LoadLepusChunk: (path, config) => {
try {
let entryUrl = config?.dynamicComponentEntry;
if (!entryUrl || entryUrl === '__Card__') {
entryUrl = lynxViewInstance.templateUrl;
}
path = lynxViewInstance.lepusCodeUrls.get(entryUrl)?.[path] ?? path;
lynxViewInstance.mtsRealm.loadScriptSync(path);
return true;
}
catch (e) {
console.error(`failed to load lepus chunk ${path}`, e);
return false;
}
},
_AddEventListener: () => { }, // no-op for main thread
_ReportError: (err, _) => {
lynxViewInstance.reportError?.(err, releaseSetting, 'lepus.js');
},
_SetSourceMapRelease: (errInfo) => releaseSetting = errInfo?.release,
_I18nResourceTranslation: lynxViewInstance.i18nManager
._I18nResourceTranslation.bind(lynxViewInstance.i18nManager),
__QueryComponent: (url, callback) => {
lynxViewInstance.queryComponent(url).then((lepusRootChunkExport) => {
callback?.({
code: 0,
data: {
url,
evalResult: lepusRootChunkExport,
},
});
});
return null;
},
};
}
//# sourceMappingURL=createMainThreadGlobalAPIs.js.map