-
Notifications
You must be signed in to change notification settings - Fork 101
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
useWorker@next #113
Draft
alewin
wants to merge
1
commit into
master
Choose a base branch
from
next
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
useWorker@next #113
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -65,5 +65,5 @@ dist/ | |
.DS_* | ||
|
||
# docusaurus | ||
.docusaurus | ||
.docusaurus/ | ||
|
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,32 +1,32 @@ | ||
{ | ||
"name": "useworker", | ||
"version": "1.0.0", | ||
"description": "⚛️ useWorker() - A React Hook for Blocking-Free Background Tasks", | ||
"devDependencies": { | ||
"@changesets/cli": "^2.13.1" | ||
}, | ||
"scripts": { | ||
"preinstall": "npx only-allow pnpm", | ||
"changeset": "changeset", | ||
"ci:build": "pnpm run build -r", | ||
"ci:publish": "pnpm run build -r && changeset publish", | ||
"ci:version": "changeset version" | ||
}, | ||
"repository": "alewin/useworker", | ||
"keywords": [ | ||
"useWorker", | ||
"react", | ||
"hooks", | ||
"worker", | ||
"web worker", | ||
"blocking ui", | ||
"javscript", | ||
"background" | ||
], | ||
"author": "alewin", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/alewin/useWorker/issues" | ||
}, | ||
"homepage": "https://github.com/alewin/useWorker#readme" | ||
} | ||
"name": "useworker", | ||
"version": "1.0.0", | ||
"description": "⚛️ useWorker() - A React Hook for Blocking-Free Background Tasks", | ||
"devDependencies": { | ||
"@changesets/cli": "^2.16.0" | ||
}, | ||
"scripts": { | ||
"preinstall": "npx only-allow pnpm", | ||
"changeset": "changeset", | ||
"ci:build": "pnpm run build -r", | ||
"ci:publish": "pnpm run build -r && changeset publish", | ||
"ci:version": "changeset version" | ||
}, | ||
"repository": "alewin/useworker", | ||
"keywords": [ | ||
"useWorker", | ||
"react", | ||
"hooks", | ||
"worker", | ||
"web worker", | ||
"blocking ui", | ||
"javscript", | ||
"background" | ||
], | ||
"author": "alewin", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/alewin/useWorker/issues" | ||
}, | ||
"homepage": "https://github.com/alewin/useWorker#readme" | ||
} |
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,51 @@ | ||
{ | ||
"env": { | ||
"browser": true, | ||
"es2021": true | ||
}, | ||
"settings": { | ||
"import/resolver": { | ||
"typescript": {}, // this loads <rootdir>/tsconfig.json to eslint | ||
"node": { | ||
"extensions": [".js", ".jsx", ".ts", ".tsx"] | ||
} | ||
} | ||
}, | ||
"extends": [ | ||
"plugin:react/recommended", | ||
"airbnb", | ||
"plugin:import/typescript", | ||
"prettier" | ||
], | ||
"parser": "@typescript-eslint/parser", | ||
"parserOptions": { | ||
"ecmaFeatures": { | ||
"jsx": true | ||
}, | ||
"ecmaVersion": 12, | ||
"sourceType": "module" | ||
}, | ||
"plugins": [ | ||
"react", | ||
"react-hooks", | ||
"@typescript-eslint" | ||
], | ||
"rules": { | ||
"@typescript-eslint/semi": 0, | ||
"@typescript-eslint/no-shadow": "error", | ||
"@typescript-eslint/no-unused-vars": "error", | ||
"import/prefer-default-export": 0, | ||
"import/no-named-as-default": 0, | ||
"react-hooks/rules-of-hooks": "error", | ||
"react-hooks/exhaustive-deps": "warn", | ||
"no-shadow": "off", | ||
"no-undef": 0, | ||
"import/extensions": 0, | ||
"arrow-parens": 0, | ||
"no-underscore-dangle": 0, | ||
"no-unused-vars": "off", | ||
"react/jsx-filename-extension": [ | ||
1, { "extensions": [".js", ".jsx", ".ts", ".tsx"] } | ||
] | ||
} | ||
} |
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,8 @@ | ||
module.exports = { | ||
semi: true, | ||
trailingComma: "all", | ||
singleQuote: false, | ||
printWidth: 120, | ||
tabWidth: 2, | ||
arrowParens: "always", | ||
}; |
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import React, { DependencyList } from 'react' | ||
import dequal from 'dequal' | ||
import React, { DependencyList } from "react"; | ||
import { dequal } from "dequal"; | ||
|
||
export const useDeepCallback = <T extends (...args: any[]) => any>( | ||
callback: T, dependencies: DependencyList, | ||
) => { | ||
const prevDependencies = React.useRef<DependencyList>(dependencies) | ||
const areDeepsEqual = dequal(prevDependencies.current, dependencies) | ||
// eslint-disable-next-line no-unused-vars | ||
export const useDeepCallback = <T extends (...args: any[]) => any>(callback: T, dependencies: DependencyList) => { | ||
const prevDependencies = React.useRef<DependencyList>(dependencies); | ||
const areDeepsEqual = dequal(prevDependencies.current, dependencies); | ||
if (!areDeepsEqual) { | ||
prevDependencies.current = dependencies | ||
prevDependencies.current = dependencies; | ||
} | ||
|
||
return React.useCallback(callback, prevDependencies.current) | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
return React.useCallback(callback, prevDependencies.current); | ||
}; |
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,2 +1,2 @@ | ||
export { useWorker } from './useWorker' | ||
export { WORKER_STATUS } from './lib/status' | ||
export { useWorker } from "./useWorker"; | ||
export * from "./lib/types"; |
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,53 @@ | ||
import { createContext, dataPlaceholder } from "isoworker"; | ||
import jobRunner from "./jobRunner"; | ||
import remoteDepsParser from "./remoteDepsParser"; | ||
import { DepList, TRANSFERABLE_TYPE } from "./types"; | ||
/** | ||
* Converts the "fn" function into a worker that takes parameters from messages and returns via messages | ||
* | ||
* @param {Function} fn the function to run with web worker | ||
* @param {Array.<String>} deps array of strings, imported into the worker through "importScripts" | ||
* | ||
* @returns {Worker} a worker created via a blob url containing the code of "fn" as a string | ||
* | ||
* @example | ||
* createWorker((a,b) => a+b, []) | ||
* // return "onmessage=return Promise.resolve((a,b) => a + b) | ||
* .then(postMessage(['SUCCESS', result])) | ||
* .catch(postMessage(['ERROR', error])" | ||
*/ | ||
const createWorker = (fn: Function, deps: string[], transferable: TRANSFERABLE_TYPE, localDeps: DepList) => { | ||
const [context, runtimeCode, transferables] = createContext(localDeps); | ||
const initMessage: unknown[] = []; | ||
let onInitMessage = ""; | ||
|
||
for (let part of runtimeCode) { | ||
if (typeof part !== "string") { | ||
const partIndex = initMessage.push(part[1]) - 1; | ||
part = part[0].replace(dataPlaceholder, `event.data[${partIndex}]`); | ||
} | ||
onInitMessage += part; | ||
} | ||
|
||
const blobCode = ` | ||
${remoteDepsParser(deps)}; | ||
${context} | ||
onmessage = function(event) { | ||
${onInitMessage} | ||
onmessage = (${jobRunner})({ | ||
fn: (${fn}), | ||
transferable: '${transferable}' | ||
}) | ||
} | ||
`; | ||
|
||
const blob = new Blob([blobCode], { type: "text/javascript" }); | ||
const url = URL.createObjectURL(blob); | ||
const worker: Worker & { _url?: string } = new Worker(url); | ||
worker._url = url; | ||
worker.postMessage(initMessage, transferable === TRANSFERABLE_TYPE.AUTO ? (transferables as Transferable[]) : []); | ||
|
||
return worker; | ||
}; | ||
|
||
export default createWorker; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@101arrowz
what is
dataPlaceholder
? 🤔I saw that "isoworker" has no exported member 'dataPlaceholder'