Skip to content
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
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 0 additions & 27 deletions .eslintrc

This file was deleted.

2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@ dist/
.DS_*

# docusaurus
.docusaurus
.docusaurus/

62 changes: 31 additions & 31 deletions package.json
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"
}
2 changes: 1 addition & 1 deletion packages/examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
- Buble Sort ( complex calculation )
- External Scripts ( `removeDependencies` option example )
- Transferable ( `transferable` option example )
<!-- - LocalDeps ( `localDependencies` option example ) -->
- LocalDeps ( `localDependencies` option example )
51 changes: 51 additions & 0 deletions packages/useWorker/.eslintrc
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"] }
]
}
}
8 changes: 8 additions & 0 deletions packages/useWorker/.prettierrc.js
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",
};
39 changes: 24 additions & 15 deletions packages/useWorker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"scripts": {
"build": "microbundle src/index.ts",
"dev": "microbundle watch",
"eslint": "eslint src/ --ext .ts",
"eslint": "eslint 'src/**/*.ts*'",
"eslint:fix": "eslint --fix 'src/**/*.ts*'",
"prettier": "prettier 'src/**/*.ts*'",
"prettier:fix": "prettier --write 'src/**/*.ts*'",
"test:local": "CHROME_ONLY=1 karma start",
"test:unit": "karma start",
"test": "npm run build && npm run test:unit"
Expand All @@ -18,30 +21,35 @@
"react": "^16.8.0"
},
"devDependencies": {
"@babel/plugin-proposal-optional-chaining": "^7.9.0",
"@babel/plugin-proposal-optional-chaining": "^7.14.5",
"@testing-library/react-hooks": "^3.4.2",
"@types/react": "^16.9.34",
"@typescript-eslint/eslint-plugin": "^2.27.0",
"@types/react": "^17.0.11",
"@typescript-eslint/eslint-plugin": "^4.27.0",
"@typescript-eslint/parser": "^4.26.1",
"babel-eslint": "^10.1.0",
"babel-loader": "^8.1.0",
"chai": "^4.2.0",
"eslint": "6.8.0",
"eslint-config-airbnb": "^18.1.0",
"eslint-config-airbnb-typescript": "^7.2.1",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.19.0",
"eslint-plugin-react-hooks": "^4.0.4",
"babel-loader": "^8.2.2",
"chai": "^4.3.4",
"eslint": "^7.28.0",
"eslint-config-airbnb": "^18.2.1",
"eslint-config-prettier": "^8.3.0",
"eslint-import-resolver-typescript": "^2.4.0",
"eslint-plugin-import": "^2.23.4",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-prettier": "^3.4.0",
"eslint-plugin-react": "^7.24.0",
"eslint-plugin-react-hooks": "^4.2.0",
"karma": "^5.2.3",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^3.1.0",
"karma-detect-browsers": "^2.3.3",
"karma-mocha": "^2.0.1",
"karma-webpack": "^4.0.2",
"microbundle": "^0.12.0-next.8",
"microbundle": "^0.13.3",
"mocha": "^8.1.3",
"prettier": "^2.3.1",
"react": "^16.13.1",
"react-test-renderer": "^16.13.1",
"typescript": "^4.3.2",
"webpack": "^4.46.0"
},
"description": "useWorker() - Web worker using React hook",
Expand All @@ -57,6 +65,7 @@
"background"
],
"dependencies": {
"dequal": "^1.0.0"
"dequal": "^2.0.2",
"isoworker": "^0.1.1"
}
}
20 changes: 10 additions & 10 deletions packages/useWorker/src/hook/useDeepCallback.ts
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);
};
4 changes: 2 additions & 2 deletions packages/useWorker/src/index.ts
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";
53 changes: 53 additions & 0 deletions packages/useWorker/src/lib/createWorker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { createContext, dataPlaceholder } from "isoworker";
Copy link
Owner Author

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'

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;
36 changes: 0 additions & 36 deletions packages/useWorker/src/lib/createWorkerBlobUrl.ts

This file was deleted.

Loading