Skip to content

Commit

Permalink
chore: worklet example
Browse files Browse the repository at this point in the history
  • Loading branch information
hardfist committed Sep 23, 2023
1 parent 25b34ba commit 164e10b
Show file tree
Hide file tree
Showing 11 changed files with 381 additions and 0 deletions.
12 changes: 12 additions & 0 deletions examples/worklet/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
10 changes: 10 additions & 0 deletions examples/worklet/loader/worklet-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const esbuild = require("esbuild");
module.exports = function workletLoader(source) {
const result = esbuild.buildSync({
entryPoints: [this.resource],
write: false,
bundle: true
});
const content = result.outputFiles[0].text;
return content;
};
19 changes: 19 additions & 0 deletions examples/worklet/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "example-basic",
"version": "1.0.0",
"description": "",
"main": "index.js",
"private": true,
"scripts": {
"dev": "rspack serve",
"build": "rspack build"
},
"devDependencies": {
"esbuild": "0.19.3",
"@rspack/cli": "workspace:*"
},
"sideEffects": false,
"keywords": [],
"author": "",
"license": "MIT"
}
41 changes: 41 additions & 0 deletions examples/worklet/rspack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/** @type {import('@rspack/cli').Configuration} */
const config = {
context: __dirname,
mode: "development",
entry: {
main: "./src/index.js"
},
devServer: {
devMiddleware: {
writeToDisk: true
}
},
module: {
rules: [
{
resourceQuery: /url/,
type: "asset"
},
{
test: /complex\.worklet/,
use: [
{
loader: "./loader/worklet-loader.js"
}
],
type: "asset"
}
]
},
builtins: {
html: [
{
template: "./index.html"
}
],
react: {
refresh: false
}
}
};
module.exports = config;
1 change: 1 addition & 0 deletions examples/worklet/src/answer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const answer = 42;
23 changes: 23 additions & 0 deletions examples/worklet/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import simpleWorklet from "./worklet/simple.worklet?url";
import complexWorklet from "./worklet/complex.worklet";
let context = new AudioContext();

context.audioWorklet.addModule(simpleWorklet).then(() => {
let node = new AudioWorkletNode(context, "port-processor");
node.port.onmessage = event => {
// Handling data from the processor.
console.log(event.data);
};

node.port.postMessage("Hello!");
});

context.audioWorklet.addModule(complexWorklet).then(() => {
let node = new AudioWorkletNode(context, "complex-processor");
node.port.onmessage = event => {
// Handling data from the processor.
console.log(event.data);
};

node.port.postMessage("Hello!");
});
1 change: 1 addition & 0 deletions examples/worklet/src/lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a = 3;
1 change: 1 addition & 0 deletions examples/worklet/src/worklet/answer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const answer = 42;
19 changes: 19 additions & 0 deletions examples/worklet/src/worklet/complex.worklet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { answer } from "./answer";
class PortProcessor extends AudioWorkletProcessor {
constructor() {
super();
this.port.onmessage = event => {
// Handling data from the node.
console.log(event.data);
};

this.port.postMessage("Hi!" + answer);
}

process(inputs, outputs, parameters) {
// Do nothing, producing silent output.
return true;
}
}

registerProcessor("complex-processor", PortProcessor);
18 changes: 18 additions & 0 deletions examples/worklet/src/worklet/simple.worklet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class PortProcessor extends AudioWorkletProcessor {
constructor() {
super();
this.port.onmessage = event => {
// Handling data from the node.
console.log(event.data);
};

this.port.postMessage("Hi!");
}

process(inputs, outputs, parameters) {
// Do nothing, producing silent output.
return true;
}
}

registerProcessor("port-processor", PortProcessor);
Loading

0 comments on commit 164e10b

Please sign in to comment.