-
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.
39 add event handler for initial setup in the iframe webworker before…
… the connection is established (#40) * add onConnectionSetup event handler * 0.3.0 * await a connection confirmed before resolving the connection in the host * 0.4.0
- Loading branch information
Showing
15 changed files
with
172 additions
and
101 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; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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.
Oops, something went wrong.