-
Notifications
You must be signed in to change notification settings - Fork 106
feat added localDependencies #178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import { WORKER_STATUS, useWorker } from "@koale/useworker"; | ||
| import React, { useState } from "react"; | ||
| import toast from "react-hot-toast"; | ||
|
|
||
| const pow = (a) => a * a; | ||
|
|
||
| function App() { | ||
| const [inputValue, setInputValue] = useState(2); // State for input value | ||
| const [localDepsWorker, { status: localDepsWorkerStatus, kill: killWorker }] = | ||
| useWorker((numbers) => pow(numbers), { | ||
| autoTerminate: false, | ||
| localDependencies: [pow], | ||
| }); | ||
|
|
||
| React.useEffect(() => { | ||
| console.log("WORKER:", localDepsWorkerStatus); | ||
| }, [localDepsWorkerStatus]); | ||
|
|
||
| const onWorkerSortClick = () => { | ||
| // Use the inputValue from state | ||
| localDepsWorker(Number(inputValue)).then((result) => { | ||
| console.log("localdeps useWorker()", result); | ||
| toast.success( | ||
| "Finished: localdeps using useWorker." + `The result is ${result}` | ||
| ); | ||
| }); | ||
| }; | ||
|
|
||
| // Handle input change | ||
| const handleInputChange = (e) => { | ||
| setInputValue(e.target.value); | ||
| }; | ||
|
|
||
| return ( | ||
| <div> | ||
| <section className="App-section"> | ||
| {/* Input field for dynamic value */} | ||
| <div style={{ display: "flex", flexDirection: "column" }}> | ||
| <pre>{"const pow = (a) => a * a;"}</pre> | ||
| <input | ||
| type="number" | ||
| value={inputValue} | ||
| onChange={handleInputChange} | ||
| placeholder="Enter a number" | ||
| className="App-input" | ||
| /> | ||
| <button | ||
| type="button" | ||
| disabled={localDepsWorkerStatus === WORKER_STATUS.RUNNING} | ||
| className="App-button" | ||
| onClick={() => onWorkerSortClick()} | ||
| > | ||
| {localDepsWorkerStatus === WORKER_STATUS.RUNNING | ||
| ? `Loading...` | ||
| : `LocalDeps useWorker()`} | ||
| </button> | ||
| {localDepsWorkerStatus === WORKER_STATUS.RUNNING ? ( | ||
| <button | ||
| type="button" | ||
| className="App-button" | ||
| onClick={() => killWorker()} | ||
| > | ||
| Kill Worker | ||
| </button> | ||
| ) : null} | ||
| </div> | ||
| </section> | ||
| <section className="App-section"> | ||
| <span style={{ color: "white" }}> | ||
| Open DevTools console to see the results. | ||
| </span> | ||
| </section> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| export default App; |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,18 +3,34 @@ | |||||||||||||||||||||||||||
| * Concatenates the remote dependencies into a comma separated string. | ||||||||||||||||||||||||||||
| * this string will then be passed as an argument to the "importScripts" function | ||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||
| * @param {Array.<String>}} deps array of string | ||||||||||||||||||||||||||||
| * @param {Array.<String>} deps array of string | ||||||||||||||||||||||||||||
| * @param {Array.<Function>} localDeps array of function | ||||||||||||||||||||||||||||
| * @returns {String} a string composed by the concatenation of the array | ||||||||||||||||||||||||||||
| * elements "deps" and "importScripts". | ||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||
| * @example | ||||||||||||||||||||||||||||
| * remoteDepsParser(['http://js.com/1.js', 'http://js.com/2.js']) // importScripts('http://js.com/1.js', 'http://js.com/2.js') | ||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
| const remoteDepsParser = (deps: string[]) => { | ||||||||||||||||||||||||||||
| if (deps.length === 0) return '' | ||||||||||||||||||||||||||||
| const remoteDepsParser = (deps: string[], localDeps: Function[]) => { | ||||||||||||||||||||||||||||
| if (deps.length === 0 && localDeps.length === 0) return ""; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const depsString = deps.map((dep) => `'${dep}'`).toString() | ||||||||||||||||||||||||||||
| return `importScripts(${depsString})` | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| const depsString = deps.map((dep) => `'${dep}'`).toString(); | ||||||||||||||||||||||||||||
| const depsFunctionString = localDeps | ||||||||||||||||||||||||||||
| .filter((dep) => typeof dep === "function") | ||||||||||||||||||||||||||||
| .map((fn) => { | ||||||||||||||||||||||||||||
| const str = fn.toString(); | ||||||||||||||||||||||||||||
| if (str.trim().startsWith("function")) { | ||||||||||||||||||||||||||||
| return str; | ||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||
| const name = fn.name; | ||||||||||||||||||||||||||||
|
Comment on lines
+20
to
+25
|
||||||||||||||||||||||||||||
| .map((fn) => { | |
| const str = fn.toString(); | |
| if (str.trim().startsWith("function")) { | |
| return str; | |
| } else { | |
| const name = fn.name; | |
| .map((fn, index) => { | |
| const str = fn.toString(); | |
| if (str.trim().startsWith("function")) { | |
| return str; | |
| } else { | |
| const name = | |
| fn.name && fn.name.trim().length > 0 ? fn.name : `_localDep_${index}`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
createWorkerBlobUrlsignature now includes alocalDepsparameter, but the JSDoc above still only documentsfnanddeps, which can confuse consumers and future maintainers about how to pass local dependencies. Please update the JSDoc to include thelocalDepsparameter (its expected type and behavior) so the documentation stays consistent with the function signature and the newlocalDependenciesoption exposed viauseWorker.