-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new example HelloWorldComChannelEndpoint
- Loading branch information
Showing
13 changed files
with
146 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/examples/build/vite.config.HelloWorldComChannelEndpointWorker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import path from 'path'; | ||
import { defineConfig } from 'vite'; | ||
|
||
const config = defineConfig({ | ||
build: { | ||
lib: { | ||
entry: path.resolve(__dirname, '../src/worker/HelloWorldComChannelEndpointWorker.ts'), | ||
name: 'HelloWorldComChannelEndpointWorker', | ||
fileName: (format) => `HelloWorldComChannelEndpointWorker-${format}.js`, | ||
formats: ['es', 'iife'] | ||
}, | ||
outDir: 'src/worker/generated', | ||
emptyOutDir: false | ||
} | ||
}); | ||
|
||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>ComChannelEndpoint: Hello World</title> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> | ||
<link type="text/css" rel="stylesheet" href="./main.css"> | ||
</head> | ||
|
||
<body> | ||
<canvas id="example" style="width: 100%; height: 100vh;"></canvas> | ||
<div id="info">ComChannelEndpoint: Hello World</div> | ||
<script type="module" src="./src/helloWorld/HelloWorldComChannelEndpoint.ts"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/examples/src/helloWorld/HelloWorldComChannelEndpoint.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { WorkerMessage, ComChannelEndpoint, RawPayload } from 'wtd-core'; | ||
|
||
/** | ||
* Hello World example just using the ComChannelEndpoint | ||
*/ | ||
class HelloWorldComChannelEndpointExample { | ||
|
||
async run() { | ||
const url = new URL(import.meta.env.DEV ? '../worker/HelloWorldComChannelEndpointWorker.ts' : '../worker/generated/HelloWorldComChannelEndpointWorker-es.js', import.meta.url); | ||
const endpoint = new ComChannelEndpoint({ | ||
endpointName: 'HelloWorldWorker', | ||
endpointId: 1, | ||
endpointConfig: { | ||
$type: 'WorkerConfigParams', | ||
url, | ||
workerType: 'module', | ||
}, | ||
verbose: true | ||
}); | ||
endpoint.connect(); | ||
|
||
try { | ||
const t0 = performance.now(); | ||
|
||
const result = await endpoint.sentMessage({ | ||
message: WorkerMessage.createNew({ | ||
cmd: 'hello_world' | ||
}), | ||
awaitAnswer: true, | ||
answer: 'hello_world_confirm' | ||
},); | ||
|
||
const rawPayload = result.payloads[0] as RawPayload; | ||
const answer = `Worker said: command: ${result.cmd} message: ${rawPayload.message.raw?.hello}`; | ||
const t1 = performance.now(); | ||
|
||
const msg = `${answer}\nWorker execution has been completed after ${t1 - t0}ms.`; | ||
console.log(msg); | ||
alert(msg); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
} | ||
|
||
const app = new HelloWorldComChannelEndpointExample(); | ||
app.run(); |
41 changes: 41 additions & 0 deletions
41
packages/examples/src/worker/HelloWorldComChannelEndpointWorker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { RawPayload, WorkerMessage, ComRouter, ComChannelEndpoint } from 'wtd-core'; | ||
|
||
/// <reference lib="WebWorker" /> | ||
|
||
declare const self: DedicatedWorkerGlobalScope; | ||
|
||
class ExampleComRouterWorker implements ComRouter { | ||
|
||
private endpointFs?: ComChannelEndpoint; | ||
|
||
setComChannelEndpoint(comChannelEndpoint: ComChannelEndpoint): void { | ||
this.endpointFs = comChannelEndpoint; | ||
} | ||
|
||
async hello_world(message: WorkerMessage) { | ||
// burn some time | ||
for (let i = 0; i < 25000000; i++) { | ||
i++; | ||
} | ||
|
||
await this.endpointFs?.sentAnswer({ | ||
message: WorkerMessage.createFromExisting(message, { | ||
overrideCmd: 'hello_world_confirm', | ||
overridePayloads: new RawPayload({ | ||
hello: 'Hello! I just incremented "i" 25 million times.' | ||
}) | ||
}), | ||
awaitAnswer: false | ||
}); | ||
} | ||
} | ||
|
||
new ComChannelEndpoint({ | ||
endpointId: 2000, | ||
endpointConfig: { | ||
$type: 'WorkerConfigDirect', | ||
worker: self | ||
}, | ||
verbose: true, | ||
endpointName: 'HelloWorldComChannelEndpointWorker' | ||
}).connect(new ExampleComRouterWorker()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters