-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
await a connection confirmed before resolving the connection in the host
- Loading branch information
Showing
13 changed files
with
159 additions
and
95 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React from "react"; | ||
|
||
import { host } from "../../src/index"; | ||
import Worker from "./worker?worker"; | ||
|
||
function WorkerExample() { | ||
const [color, setColor] = React.useState("#fff"); | ||
const [message, setMessage] = React.useState(""); | ||
|
||
const onClick = async () => { | ||
const options = { initialValue: "initial value from host" }; | ||
const connection = await host.connect(new Worker(), options); | ||
|
||
const messageRes = await connection?.remote.getMessage(); | ||
setMessage(messageRes); | ||
|
||
const colorRes = await connection?.remote.createColor(); | ||
setColor(colorRes); | ||
}; | ||
|
||
return ( | ||
<div style={{ background: color }}> | ||
<div style={{ flex: 1 }}> | ||
<h1>HOST</h1> | ||
<button type="button" onClick={onClick}> | ||
call web worker function | ||
</button> | ||
<p>{message}</p> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export default WorkerExample; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import guest from "../../src/guest"; | ||
|
||
function createColor() { | ||
const letters = "0123456789ABCDEF"; | ||
let color = "#"; | ||
for (let i = 0; i < 6; i++) { | ||
color += letters[Math.floor(Math.random() * 16)]; | ||
} | ||
return color; | ||
} | ||
|
||
function getMessage() { | ||
return "Hello from the worker! Initialized with:" + (self as any).config?.initialValue; | ||
} | ||
|
||
const run = async () => { | ||
try { | ||
await guest.connect( | ||
{ | ||
createColor, | ||
getMessage, | ||
}, | ||
{ | ||
onConnectionSetup: async (config) => { | ||
console.log("Connection setup with config:", config); | ||
(self as any).config = config; | ||
}, | ||
} | ||
); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}; | ||
|
||
run(); |
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,11 @@ | ||
declare module "*.html?raw" { | ||
const content: any; | ||
export default content; | ||
} | ||
|
||
declare module "*?worker" { | ||
const WorkerFactory: { | ||
new (): Worker; | ||
}; | ||
export default WorkerFactory; | ||
} |
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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1 @@ | ||
/// <reference types="vite/client" /> | ||
|
||
declare module "*.html" { | ||
const content: any; | ||
export default content; | ||
} |