Skip to content

Commit 88312f8

Browse files
committed
fix(samples): await Native.* calls + drop broken svelteLogo reference
Sample-side bugs that surfaced once the merge brought main's plugin chain in. Pre-existing in v2-new-samples, not caused by the merge: 1. Missing await on Native.method() — Comlink wraps the embind module when it runs inside a worker (mt mode for the vite/webpack plugins, and effectively for nuxt/next even in st because they instantiate in a deferred context). All proxy calls return Promises, so the v2-new-samples authoring pattern of `setMessage(Native.sample())` rendered "[object Promise]" instead of the result. Fixed in: svelte-vite-mt/src/App.svelte react-vite-mt/src/App.jsx nuxt-vite/app/example.vue nuxt-vite-mt/app/example.vue next-webpack-mt/app/page.tsx remix-vite/app/welcome/welcome.tsx 2. svelte-vite-mt/src/App.svelte referenced an undefined `svelteLogo` binding (the import was missing and there's no svelte logo asset in the package). Removed the <div class="logo">…</div> block — it caused the whole component to render blank in Svelte 5. For mt samples the thread-result poll also moved into setTimeout's async callback so getThreadResult() actually completes before being assigned (previously it raced runOnThread()). Verified end-to-end: every listed sample renders the matrix result in the browser.
1 parent 7294e3f commit 88312f8

6 files changed

Lines changed: 28 additions & 27 deletions

File tree

  • cppjs-samples
    • cppjs-sample-web-next-webpack-multithread/app
    • cppjs-sample-web-nuxt-vite-multithread/app
    • cppjs-sample-web-nuxt-vite/app
    • cppjs-sample-web-react-vite-multithread/src
    • cppjs-sample-web-remix-vite/app/welcome
    • cppjs-sample-web-svelte-vite-multithread/src

cppjs-samples/cppjs-sample-web-next-webpack-multithread/app/page.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@ export default function Home() {
1313
wasm: '/cpp.wasm',
1414
js: '/cpp.js',
1515
}
16-
}).then(() => {
16+
}).then(async () => {
1717
// AllSymbols contains all exported symbols after initialization
1818
console.log('AllSymbols:', AllSymbols);
1919
console.log('AllSymbols.Native:', AllSymbols.Native);
2020

2121
if (AllSymbols.Native) {
22-
const result = AllSymbols.Native.sample();
22+
const result = await AllSymbols.Native.sample();
2323
setMessage(result);
24-
AllSymbols.Native.runOnThread();
25-
setThreadResult(AllSymbols.Native.getThreadResult());
24+
await AllSymbols.Native.runOnThread();
25+
setTimeout(async () => {
26+
setThreadResult(await AllSymbols.Native.getThreadResult());
27+
}, 1000);
2628
} else {
2729
setMessage('Native class not found in AllSymbols');
2830
}

cppjs-samples/cppjs-sample-web-nuxt-vite-multithread/app/example.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ onMounted(async () => {
1212
try {
1313
await initCppJs()
1414
console.log('initCppJs completed, Native:', Native)
15-
message.value = Native.sample()
15+
message.value = await Native.sample()
1616
console.log('Result:', message.value)
17-
Native.runOnThread()
18-
threadResult.value = Native.getThreadResult()
17+
await Native.runOnThread()
18+
setTimeout(async () => {
19+
threadResult.value = await Native.getThreadResult()
20+
}, 1000)
1921
} catch (e) {
2022
console.error('Error in initCppJs:', e)
2123
message.value = 'Error: ' + e.message

cppjs-samples/cppjs-sample-web-nuxt-vite/app/example.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ onMounted(async () => {
1111
try {
1212
await initCppJs()
1313
console.log('initCppJs completed, Native:', Native)
14-
message.value = Native.sample()
14+
message.value = await Native.sample()
1515
console.log('Result:', message.value)
1616
} catch (e) {
1717
console.error('Error in initCppJs:', e)

cppjs-samples/cppjs-sample-web-react-vite-multithread/src/App.jsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ function App() {
77
const [threadResult, setThreadResult] = useState('...')
88

99
useEffect(() => {
10-
initCppJs().then(() => {
11-
setStandardResult(Native.sample());
12-
10+
initCppJs().then(async () => {
11+
setStandardResult(await Native.sample());
12+
1313
// Run computation on a separate thread
14-
Native.runOnThread();
15-
14+
await Native.runOnThread();
15+
1616
// Poll for thread result after a short delay
17-
setTimeout(() => {
18-
setThreadResult(Native.getThreadResult());
17+
setTimeout(async () => {
18+
setThreadResult(await Native.getThreadResult());
1919
}, 1000);
2020
});
2121
}, []);

cppjs-samples/cppjs-sample-web-remix-vite/app/welcome/welcome.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export function Welcome() {
77
const [message, setMessage] = useState('compiling ...')
88

99
useEffect(() => {
10-
initCppJs().then(() => {
11-
setMessage(Native.sample());
10+
initCppJs().then(async () => {
11+
setMessage(await Native.sample());
1212
});
1313
}, []);
1414
return (

cppjs-samples/cppjs-sample-web-svelte-vite-multithread/src/App.svelte

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,20 @@
44
let standardResult = $state('compiling ...');
55
let threadResult = $state('...');
66
7-
initCppJs().then(() => {
8-
standardResult = Native.sample();
9-
7+
initCppJs().then(async () => {
8+
standardResult = await Native.sample();
9+
1010
// Run computation on a separate thread
11-
Native.runOnThread();
12-
11+
await Native.runOnThread();
12+
1313
// Poll for thread result after a short delay
14-
setTimeout(() => {
15-
threadResult = Native.getThreadResult();
14+
setTimeout(async () => {
15+
threadResult = await Native.getThreadResult();
1616
}, 1000);
1717
});
1818
</script>
1919

2020
<main>
21-
<div class="logo">
22-
<img src={svelteLogo} alt="Svelte Logo" />
23-
</div>
2421
<p>Matrix multiplier with c++</p>
2522
<br />
2623
<p>Standard Result &nbsp;&nbsp;:&nbsp;&nbsp; {standardResult}</p>

0 commit comments

Comments
 (0)