Skip to content

Instantly share code, notes, and snippets.

@3846masa
Last active October 2, 2018 07:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3846masa/4fb73982d4f1d771962870aaa2e7002d to your computer and use it in GitHub Desktop.
Save 3846masa/4fb73982d4f1d771962870aaa2e7002d to your computer and use it in GitHub Desktop.

Sample code

Setup

yarn

Run

yarn start
/node_modules
/.cache
/dist
#[no_mangle]
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sample Code</title>
</head>
<body>
<p>Result: <span id="result"></span></p>
<button id="run">Run</button>
<script src="./index.js"></script>
</body>
</html>
const worker = new Worker('./worker.js');
worker.addEventListener('error', console.error);
const a = 10;
const b = 20;
const onClick = () => {
const callback = ({ data: { result } }) => {
console.assert(result === a + b);
document.getElementById('result').textContent = `${a} + ${b} = ${result}`;
};
worker.addEventListener('message', callback, { once: true });
worker.postMessage({ a, b });
};
document.getElementById('run').addEventListener('click', onClick, { once: true });
{
"scripts": {
"start": "parcel index.html",
"build": "parcel build index.html"
},
"peerDependencies": {
"parcel-bundler": "github:3846masa-tmp/parcel#support-wasm-in-workers"
},
"browserslist": "last 1 Chrome version"
}
import { add } from './add.rs';
self.addEventListener('message', ({ data: { a, b } }) => {
self.postMessage({ result: add(a, b) });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment