/* global lynx, __MATH_BUNDLE_URL__, __FORMAT_BUNDLE_URL__ */ import { runOnMainThread, useEffect, useState } from '@lynx-js/react'; function formatSumOnMainThread(mathBundle, formatBundle, a, b) { 'main thread'; const stylesheet = globalThis.__LoadStyleSheet('math:CSS', mathBundle); if (stylesheet === null) throw new Error('Missing math:CSS section'); globalThis.__AdoptStyleSheet(stylesheet); const math = lynx.loadScript('math__main-thread', { bundleName: mathBundle }); const format = lynx.loadScript('format__main-thread', { bundleName: formatBundle, }); return format.sumLabel(a, b, math.add(a, b)); } async function fetchBundleName(url) { 'background only'; const response = await lynx.fetchBundle(url, {}); if (response.code !== 0) throw new Error(response.errorMsg); return response.url; } async function loadResults() { 'background only'; const [mathBundle, formatBundle] = await Promise.all([ fetchBundleName(__MATH_BUNDLE_URL__), fetchBundleName(__FORMAT_BUNDLE_URL__), ]); const math = lynx.loadScript('math', { bundleName: mathBundle }); const format = lynx.loadScript('format', { bundleName: formatBundle }); const a = 6; const b = 7; const productLabel = format.productLabel(a, b, math.multiply(a, b)); const sumLabel = await runOnMainThread(formatSumOnMainThread)( mathBundle, formatBundle, a, b, ); return { sumLabel, productLabel }; } export function App() { const [results, setResults] = useState(null); const [error, setError] = useState(''); useEffect(() => { loadResults().then(setResults, reason => setError(String(reason))); }, []); return ( External bundle results {results ? ( <> {results.sumLabel} {results.productLabel} ) : {error || 'Loading resources…'}} ); }