react-hooks-worker
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -5,2 +5,6 @@ # Change Log | ||
## [1.0.1] - 2021-10-13 | ||
### Changed | ||
- Fix package.json properly for ESM | ||
## [1.0.0] - 2020-08-15 | ||
@@ -7,0 +11,0 @@ ### Changed |
@@ -1,2 +0,2 @@ | ||
import{useState as e,useMemo as r,useRef as s,useEffect as o}from"react";const t={};function a(a,n){const[f,c]=e(t),l=r(a,[a]),i=s(l);return o(()=>{i.current=l;let e=e=>c(e);return l.onmessage=r=>e({result:r.data}),l.onerror=()=>e({error:"error"}),l.onmessageerror=()=>e({error:"messageerror"}),()=>{e=()=>null,l.terminate(),c(t)}},[l]),o(()=>{i.current.postMessage(n)},[n]),f}function n(e){self.onmessage=async r=>{const s=e(r.data);if(s[Symbol.asyncIterator])for await(const e of s)self.postMessage(e);else if(s[Symbol.iterator])for(const e of s)self.postMessage(e);else self.postMessage(await s)}}export{n as exposeWorker,a as useWorker}; | ||
//# sourceMappingURL=index.modern.js.map | ||
import{useState as r,useMemo as e,useRef as t,useEffect as o}from"react";const a={};function n(n,s){const[l,i]=r(a),c=e(n,[n]),f=t(c);return o(()=>{f.current=c;let r=r=>i(r);return c.onmessage=e=>r({result:e.data}),c.onerror=()=>r({error:"error"}),c.onmessageerror=()=>r({error:"messageerror"}),()=>{r=()=>null,c.terminate(),i(a)}},[c]),o(()=>{f.current.postMessage(s)},[s]),l}function s(r){self.onmessage=async e=>{const t=r(e.data);if(t[Symbol.asyncIterator]){var o,a=!1,n=!1;try{for(var s,l=function(r){var e;if("undefined"!=typeof Symbol&&(Symbol.asyncIterator&&(e=r[Symbol.asyncIterator]),null==e&&Symbol.iterator&&(e=r[Symbol.iterator])),null==e&&(e=r["@@asyncIterator"]),null==e&&(e=r["@@iterator"]),null==e)throw new TypeError("Object is not async iterable");return e.call(r)}(t);a=!(s=await l.next()).done;a=!1)self.postMessage(s.value)}catch(r){n=!0,o=r}finally{try{a&&null!=l.return&&await l.return()}finally{if(n)throw o}}}else if(t[Symbol.iterator])for(const r of t)self.postMessage(r);else self.postMessage(await t)}}export{s as exposeWorker,n as useWorker}; | ||
//# sourceMappingURL=index.modern.mjs.map |
@@ -1,2 +0,2 @@ | ||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((n=n||self).reactHooksWorker={},n.react)}(this,function(n,t){var r={};function e(n,t){(null==t||t>n.length)&&(t=n.length);for(var r=0,e=new Array(t);r<t;r++)e[r]=n[r];return e}function o(n,t,r){if(!n.s){if(r instanceof i){if(!r.s)return void(r.o=o.bind(null,n,t));1&t&&(t=r.s),r=r.v}if(r&&r.then)return void r.then(o.bind(null,n,t),o.bind(null,n,2));n.s=t,n.v=r;var e=n.o;e&&e(n)}}var i=function(){function n(){}return n.prototype.then=function(t,r){var e=new n,i=this.s;if(i){var u=1&i?t:r;if(u){try{o(e,1,u(this.v))}catch(n){o(e,2,n)}return e}return this}return this.o=function(n){try{var i=n.v;1&n.s?o(e,1,t?t(i):i):r?o(e,1,r(i)):o(e,2,i)}catch(n){o(e,2,n)}},e},n}();function u(n){return n instanceof i&&1&n.s}function f(n,t){try{var r=n()}catch(n){return t(!0,n)}return r&&r.then?r.then(t.bind(null,!1),t.bind(null,!0)):t(!1,r)}n.exposeWorker=function(n){self.onmessage=function(t){try{var r=n(t.data);return Promise.resolve(function(){if(r[Symbol.asyncIterator]){var n,t=!0,a=!1;return f(function(){return function(n,e){try{var f=function(){var n,e,f=function(n){var t;if("undefined"!=typeof Symbol){if(Symbol.asyncIterator&&null!=(t=n[Symbol.asyncIterator]))return t.call(n);if(Symbol.iterator&&null!=(t=n[Symbol.iterator]))return t.call(n)}throw new TypeError("Object is not async iterable")}(r),a=function(n,t,r){for(var e;;){var f=n();if(u(f)&&(f=f.v),!f)return a;if(f.then){e=0;break}var a=r();if(a&&a.then){if(!u(a)){e=1;break}a=a.s}if(t){var c=t();if(c&&c.then&&!u(c)){e=2;break}}}var s=new i,l=o.bind(null,s,2);return(0===e?f.then(v):1===e?a.then(h):c.then(d)).then(void 0,l),s;function h(e){a=e;do{if(t&&(c=t())&&c.then&&!u(c))return void c.then(d).then(void 0,l);if(!(f=n())||u(f)&&!f.v)return void o(s,1,a);if(f.then)return void f.then(v).then(void 0,l);u(a=r())&&(a=a.v)}while(!a||!a.then);a.then(h).then(void 0,l)}function v(n){n?(a=r())&&a.then?a.then(h).then(void 0,l):h(a):o(s,1,a)}function d(){(f=n())?f.then?f.then(v).then(void 0,l):v(f):o(s,1,a)}}(function(){return!!Promise.resolve(f.next()).then(function(r){return t=n.done,n=r,Promise.resolve(n.value).then(function(n){return e=n,!t})})},function(){return!!(t=!0)},function(){self.postMessage(e)});if(a&&a.then)return a.then(function(){})}()}catch(n){return e(n)}return f&&f.then?f.then(void 0,e):f}(0,function(t){a=!0,n=t})},function(r,e){function o(n){if(r)throw e;return e}var i=f(function(){var n=function(){if(!t&&null!=_iterator.return)return Promise.resolve(_iterator.return()).then(function(){})}();if(n&&n.then)return n.then(function(){})},function(t,r){if(a)throw n;if(t)throw r;return r});return i&&i.then?i.then(o):o()})}var c=function(){if(!r[Symbol.iterator]){var n=self,t=n.postMessage;return Promise.resolve(r).then(function(r){t.call(n,r)})}for(var o,i=function(n,t){var r;if("undefined"==typeof Symbol||null==n[Symbol.iterator]){if(Array.isArray(n)||(r=function(n,t){if(n){if("string"==typeof n)return e(n,void 0);var r=Object.prototype.toString.call(n).slice(8,-1);return"Object"===r&&n.constructor&&(r=n.constructor.name),"Map"===r||"Set"===r?Array.from(n):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?e(n,void 0):void 0}}(n))){r&&(n=r);var o=0;return function(){return o>=n.length?{done:!0}:{done:!1,value:n[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(r=n[Symbol.iterator]()).next.bind(r)}(r);!(o=i()).done;)self.postMessage(o.value)}();if(c&&c.then)return c.then(function(){})}())}catch(t){return Promise.reject(t)}}},n.useWorker=function(n,e){var o=t.useState(r),i=o[0],u=o[1],f=t.useMemo(n,[n]),a=t.useRef(f);return t.useEffect(function(){a.current=f;var n=function(n){return u(n)};return f.onmessage=function(t){return n({result:t.data})},f.onerror=function(){return n({error:"error"})},f.onmessageerror=function(){return n({error:"messageerror"})},function(){n=function(){return null},f.terminate(),u(r)}},[f]),t.useEffect(function(){a.current.postMessage(e)},[e]),i}}); | ||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((n||self).reactHooksWorker={},n.react)}(this,function(n,t){var r={};function e(n,t){(null==t||t>n.length)&&(t=n.length);for(var r=0,e=new Array(t);r<t;r++)e[r]=n[r];return e}function o(n,t,r){if(!n.s){if(r instanceof i){if(!r.s)return void(r.o=o.bind(null,n,t));1&t&&(t=r.s),r=r.v}if(r&&r.then)return void r.then(o.bind(null,n,t),o.bind(null,n,2));n.s=t,n.v=r;var e=n.o;e&&e(n)}}var i=/*#__PURE__*/function(){function n(){}return n.prototype.then=function(t,r){var e=new n,i=this.s;if(i){var u=1&i?t:r;if(u){try{o(e,1,u(this.v))}catch(n){o(e,2,n)}return e}return this}return this.o=function(n){try{var i=n.v;1&n.s?o(e,1,t?t(i):i):r?o(e,1,r(i)):o(e,2,i)}catch(n){o(e,2,n)}},e},n}();function u(n){return n instanceof i&&1&n.s}function f(n,t){try{var r=n()}catch(n){return t(!0,n)}return r&&r.then?r.then(t.bind(null,!1),t.bind(null,!0)):t(!1,r)}n.exposeWorker=function(n){self.onmessage=function(t){try{var r=n(t.data);return Promise.resolve(function(){if(r[Symbol.asyncIterator]){var n,t=!1,a=!1;return f(function(){return function(n,e){try{var f=function(){var n,e=function(n){var t;if("undefined"!=typeof Symbol&&(Symbol.asyncIterator&&(t=n[Symbol.asyncIterator]),null==t&&Symbol.iterator&&(t=n[Symbol.iterator])),null==t&&(t=n["@@asyncIterator"]),null==t&&(t=n["@@iterator"]),null==t)throw new TypeError("Object is not async iterable");return t.call(n)}(r),f=function(n,t,r){for(var e;;){var f=n();if(u(f)&&(f=f.v),!f)return a;if(f.then){e=0;break}var a=r();if(a&&a.then){if(!u(a)){e=1;break}a=a.s}if(t){var c=t();if(c&&c.then&&!u(c)){e=2;break}}}var s=new i,l=o.bind(null,s,2);return(0===e?f.then(v):1===e?a.then(h):c.then(d)).then(void 0,l),s;function h(e){a=e;do{if(t&&(c=t())&&c.then&&!u(c))return void c.then(d).then(void 0,l);if(!(f=n())||u(f)&&!f.v)return void o(s,1,a);if(f.then)return void f.then(v).then(void 0,l);u(a=r())&&(a=a.v)}while(!a||!a.then);a.then(h).then(void 0,l)}function v(n){n?(a=r())&&a.then?a.then(h).then(void 0,l):h(a):o(s,1,a)}function d(){(f=n())?f.then?f.then(v).then(void 0,l):v(f):o(s,1,a)}}(function(){return!!Promise.resolve(e.next()).then(function(r){return t=!(n=r).done})},function(){return!!(t=!1)},function(){self.postMessage(n.value)});if(f&&f.then)return f.then(function(){})}()}catch(n){return e(n)}return f&&f.then?f.then(void 0,e):f}(0,function(t){a=!0,n=t})},function(r,e){function o(n){if(r)throw e;return e}var i=f(function(){var n=function(){if(t&&null!=_iterator.return)return Promise.resolve(_iterator.return()).then(function(){})}();if(n&&n.then)return n.then(function(){})},function(t,r){if(a)throw n;if(t)throw r;return r});return i&&i.then?i.then(o):o()})}var c=function(){if(!r[Symbol.iterator]){var n=self,t=n.postMessage;return Promise.resolve(r).then(function(r){t.call(n,r)})}for(var o,i=function(n,t){var r="undefined"!=typeof Symbol&&n[Symbol.iterator]||n["@@iterator"];if(r)return(r=r.call(n)).next.bind(r);if(Array.isArray(n)||(r=function(n,t){if(n){if("string"==typeof n)return e(n,t);var r=Object.prototype.toString.call(n).slice(8,-1);return"Object"===r&&n.constructor&&(r=n.constructor.name),"Map"===r||"Set"===r?Array.from(n):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?e(n,t):void 0}}(n))){r&&(n=r);var o=0;return function(){return o>=n.length?{done:!0}:{done:!1,value:n[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(r);!(o=i()).done;)self.postMessage(o.value)}();if(c&&c.then)return c.then(function(){})}())}catch(t){return Promise.reject(t)}}},n.useWorker=function(n,e){var o=t.useState(r),i=o[0],u=o[1],f=t.useMemo(n,[n]),a=t.useRef(f);return t.useEffect(function(){a.current=f;var n=function(n){return u(n)};return f.onmessage=function(t){return n({result:t.data})},f.onerror=function(){return n({error:"error"})},f.onmessageerror=function(){return n({error:"messageerror"})},function(){n=function(){return null},f.terminate(),u(r)}},[f]),t.useEffect(function(){a.current.postMessage(e)},[e]),i}}); | ||
//# sourceMappingURL=index.umd.js.map |
@@ -15,3 +15,3 @@ declare type State = { | ||
* | ||
* const createWorker = () => new Worker('./slow_fib.worker', { type: 'module' }); | ||
* const createWorker = () => new Worker(new URL('./slow_fib.worker', import.meta.url)); | ||
* | ||
@@ -18,0 +18,0 @@ * const CalcFib = ({ count }) => { |
{ | ||
"name": "react-hooks-worker", | ||
"description": "React custom hooks for web workers", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"author": "Daishi Kato", | ||
@@ -14,2 +14,11 @@ "repository": { | ||
"types": "./dist/src/index.d.ts", | ||
"exports": { | ||
"./package.json": "./package.json", | ||
".": { | ||
"types": "./dist/src/index.d.ts", | ||
"module": "./dist/index.modern.js", | ||
"import": "./dist/index.modern.mjs", | ||
"default": "./dist/index.umd.js" | ||
} | ||
}, | ||
"sideEffects": false, | ||
@@ -22,2 +31,3 @@ "files": [ | ||
"compile": "microbundle build -f modern,umd", | ||
"postcompile": "cp dist/index.modern.mjs dist/index.modern.js && cp dist/index.modern.mjs.map dist/index.modern.js.map", | ||
"test": "npm run eslint && npm run tsc-test && npm run jest", | ||
@@ -27,3 +37,3 @@ "eslint": "eslint --ext .js,.ts,.tsx --ignore-pattern dist .", | ||
"tsc-test": "tsc --project . --noEmit", | ||
"apidoc": "documentation readme --section API --markdown-toc false --parse-extension ts src/*.ts", | ||
"apidoc": "documentation readme --section API --markdown-toc false --parse-extension ts src/exposeWorker.ts src/useWorker.ts", | ||
"examples:01_minimal": "DIR=01_minimal EXT=js webpack-dev-server", | ||
@@ -44,29 +54,28 @@ "examples:02_typescript": "DIR=02_typescript webpack-dev-server", | ||
"devDependencies": { | ||
"@types/jest": "^26.0.10", | ||
"@types/react": "^16.9.46", | ||
"@types/react-dom": "^16.9.8", | ||
"@types/react-test-renderer": "^16.9.3", | ||
"@typescript-eslint/eslint-plugin": "^3.9.0", | ||
"@typescript-eslint/parser": "^3.9.0", | ||
"documentation": "^13.0.2", | ||
"eslint": "^7.7.0", | ||
"eslint-config-airbnb": "^18.2.0", | ||
"eslint-plugin-import": "^2.22.0", | ||
"eslint-plugin-jsx-a11y": "^6.3.1", | ||
"eslint-plugin-react": "^7.20.6", | ||
"eslint-plugin-react-hooks": "^4.0.8", | ||
"html-webpack-plugin": "^4.3.0", | ||
"jest": "^26.4.0", | ||
"microbundle": "^0.12.3", | ||
"@types/jest": "^27.0.2", | ||
"@types/react": "^17.0.29", | ||
"@types/react-dom": "^17.0.9", | ||
"@types/react-test-renderer": "^17.0.1", | ||
"@typescript-eslint/eslint-plugin": "^5.0.0", | ||
"@typescript-eslint/parser": "^5.0.0", | ||
"documentation": "^13.2.5", | ||
"eslint": "^8.0.0", | ||
"eslint-config-airbnb": "^18.2.1", | ||
"eslint-plugin-import": "^2.25.2", | ||
"eslint-plugin-jsx-a11y": "^6.4.1", | ||
"eslint-plugin-react": "^7.26.1", | ||
"eslint-plugin-react-hooks": "^4.2.0", | ||
"html-webpack-plugin": "^5.3.2", | ||
"jest": "^27.2.5", | ||
"microbundle": "^0.14.0", | ||
"npm-run-all": "^4.1.5", | ||
"react": "^16.13.1", | ||
"react-dom": "^16.13.1", | ||
"react-test-renderer": "^16.13.1", | ||
"ts-jest": "^26.2.0", | ||
"ts-loader": "^8.0.2", | ||
"typescript": "^3.9.7", | ||
"webpack": "^4.44.1", | ||
"webpack-cli": "^3.3.12", | ||
"webpack-dev-server": "^3.11.0", | ||
"worker-plugin": "^5.0.0" | ||
"react-dom": "^17.0.2", | ||
"react-test-renderer": "^17.0.2", | ||
"ts-jest": "^27.0.5", | ||
"ts-loader": "^9.2.6", | ||
"typescript": "^4.4.4", | ||
"webpack": "^5.58.2", | ||
"webpack-cli": "^4.9.0", | ||
"webpack-dev-server": "^4.3.1" | ||
}, | ||
@@ -73,0 +82,0 @@ "peerDependencies": { |
@@ -6,2 +6,3 @@ # react-hooks-worker | ||
[![size](https://img.shields.io/bundlephobia/minzip/react-hooks-worker)](https://bundlephobia.com/result?p=react-hooks-worker) | ||
[![discord](https://img.shields.io/discord/627656437971288081)](https://discord.gg/MrQdmzd) | ||
@@ -22,6 +23,6 @@ React custom hooks for web workers. | ||
- sync function | ||
- async function | ||
- sync generator function | ||
- async generator function | ||
* sync function | ||
* async function | ||
* sync generator function | ||
* async generator function | ||
@@ -53,3 +54,3 @@ ## Install | ||
const createWorker = () => new Worker('./slow_fib.worker', { type: 'module' }); | ||
const createWorker = () => new Worker(new URL('./slow_fib.worker', import.meta.url)); | ||
@@ -69,22 +70,2 @@ const CalcFib = ({ count }) => { | ||
## Bundler support requirements | ||
This library requires a bundler to recognize Web Worker properly. | ||
Not everything is tested, and we appreciate for your help. | ||
(So far, only tested with worker-plugin.) | ||
### Webpack | ||
- [worker-plugin](https://github.com/GoogleChromeLabs/worker-plugin) :white_check_mark: | ||
- [worker-loader](https://github.com/webpack-contrib/worker-loader) | ||
### Parcel | ||
Parcel allow your Web Worker script to be automatically bundled. | ||
### Rollup | ||
- [rollup-plugin-off-main-thread](https://github.com/surma/rollup-plugin-off-main-thread) | ||
- [rollup-plugin-web-worker-loader](https://github.com/darionco/rollup-plugin-web-worker-loader) | ||
## API | ||
@@ -100,10 +81,10 @@ | ||
- A value | ||
- A promise | ||
- An iterable | ||
- An async iterable | ||
* A value | ||
* A promise | ||
* An iterable | ||
* An async iterable | ||
#### Parameters | ||
- `func` **function (data: any): any** | ||
* `func` **function (data: any): any** | ||
@@ -129,4 +110,4 @@ #### Examples | ||
- `createWorker` **function (): [Worker](https://developer.mozilla.org/docs/Web/JavaScript)** | ||
- `input` **any** | ||
* `createWorker` **function (): [Worker](https://developer.mozilla.org/docs/Web/JavaScript)** | ||
* `input` **any** | ||
@@ -138,3 +119,3 @@ #### Examples | ||
const createWorker = () => new Worker('./slow_fib.worker', { type: 'module' }); | ||
const createWorker = () => new Worker(new URL('./slow_fib.worker', import.meta.url)); | ||
@@ -170,3 +151,3 @@ const CalcFib = ({ count }) => { | ||
- [Playing with React Hooks and Web Workers](https://blog.axlight.com/posts/playing-with-react-hooks-and-web-workers/) | ||
- [How I Developed React Hooks for Web Workers](https://blog.axlight.com/posts/how-i-developed-react-hooks-for-web-workers/) | ||
* [Playing with React Hooks and Web Workers](https://blog.axlight.com/posts/playing-with-react-hooks-and-web-workers/) | ||
* [How I Developed React Hooks for Web Workers](https://blog.axlight.com/posts/how-i-developed-react-hooks-for-web-workers/) |
@@ -25,3 +25,3 @@ import { | ||
* | ||
* const createWorker = () => new Worker('./slow_fib.worker', { type: 'module' }); | ||
* const createWorker = () => new Worker(new URL('./slow_fib.worker', import.meta.url)); | ||
* | ||
@@ -28,0 +28,0 @@ * const CalcFib = ({ count }) => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29360
26
16
156
146