react-hooks-worker
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -5,2 +5,6 @@ # Change Log | ||
## [1.0.2] - 2021-11-04 | ||
### Changed | ||
- fix: proper typing for useWorker #34 | ||
## [1.0.1] - 2021-10-13 | ||
@@ -7,0 +11,0 @@ ### Changed |
@@ -1,2 +0,2 @@ | ||
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}; | ||
import{useState as r,useMemo as e,useRef as t,useEffect as n}from"react";const o={};function s(s,a){const[i,l]=r(o),u=e(s,[s]),c=t(u);return n(()=>{c.current=u;let r=r=>l(r);return u.onmessage=e=>r({result:e.data}),u.onerror=()=>r({error:"error"}),u.onmessageerror=()=>r({error:"messageerror"}),()=>{r=()=>null,u.terminate(),l(o)}},[u]),n(()=>{c.current.postMessage(a)},[a]),i}function a(r){function e(r){if(Object(r)!==r)return Promise.reject(new TypeError(r+" is not an object."));var e=r.done;return Promise.resolve(r.value).then(function(r){return{value:r,done:e}})}return a=function(r){this.s=r,this.n=r.next},a.prototype={s:null,n:null,next:function(){return e(this.n.apply(this.s,arguments))},return:function(r){var t=this.s.return;return void 0===t?Promise.resolve({value:r,done:!0}):e(t.apply(this.s,arguments))},throw:function(r){var t=this.s.return;return void 0===t?Promise.reject(r):e(t.apply(this.s,arguments))}},new a(r)}function i(r){self.onmessage=async e=>{const t=r(e.data);if(t[Symbol.asyncIterator]){var n,o=!1,s=!1;try{for(var i,l=function(r){var e,t,n,o=2;for("undefined"!=typeof Symbol&&(t=Symbol.asyncIterator,n=Symbol.iterator);o--;){if(t&&null!=(e=r[t]))return e.call(r);if(n&&null!=(e=r[n]))return new a(e.call(r));t="@@asyncIterator",n="@@iterator"}throw new TypeError("Object is not async iterable")}(t);o=!(i=await l.next()).done;o=!1)self.postMessage(i.value)}catch(r){s=!0,n=r}finally{try{o&&null!=l.return&&await l.return()}finally{if(s)throw n}}}else if(t[Symbol.iterator])for(const r of t)self.postMessage(r);else self.postMessage(await t)}}export{i as exposeWorker,s 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||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}}); | ||
!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 e={};function r(n){function t(n){if(Object(n)!==n)return Promise.reject(new TypeError(n+" is not an object."));var t=n.done;return Promise.resolve(n.value).then(function(n){return{value:n,done:t}})}return r=function(n){this.s=n,this.n=n.next},r.prototype={s:null,n:null,next:function(){return t(this.n.apply(this.s,arguments))},return:function(n){var e=this.s.return;return void 0===e?Promise.resolve({value:n,done:!0}):t(e.apply(this.s,arguments))},throw:function(n){var e=this.s.return;return void 0===e?Promise.reject(n):t(e.apply(this.s,arguments))}},new r(n)}function o(n,t){(null==t||t>n.length)&&(t=n.length);for(var e=0,r=new Array(t);e<t;e++)r[e]=n[e];return r}function i(n,t,e){if(!n.s){if(e instanceof u){if(!e.s)return void(e.o=i.bind(null,n,t));1&t&&(t=e.s),e=e.v}if(e&&e.then)return void e.then(i.bind(null,n,t),i.bind(null,n,2));n.s=t,n.v=e;var r=n.o;r&&r(n)}}var u=/*#__PURE__*/function(){function n(){}return n.prototype.then=function(t,e){var r=new n,o=this.s;if(o){var u=1&o?t:e;if(u){try{i(r,1,u(this.v))}catch(n){i(r,2,n)}return r}return this}return this.o=function(n){try{var o=n.v;1&n.s?i(r,1,t?t(o):o):e?i(r,1,e(o)):i(r,2,o)}catch(n){i(r,2,n)}},r},n}();function f(n){return n instanceof u&&1&n.s}function a(n,t){try{var e=n()}catch(n){return t(!0,n)}return e&&e.then?e.then(t.bind(null,!1),t.bind(null,!0)):t(!1,e)}n.exposeWorker=function(n){self.onmessage=function(t){try{var e=n(t.data);return Promise.resolve(function(){if(e[Symbol.asyncIterator]){var n,t=!1,c=!1;return a(function(){return function(n,o){try{var a=function(){var n,o=function(n){var t,e,o,i=2;for("undefined"!=typeof Symbol&&(e=Symbol.asyncIterator,o=Symbol.iterator);i--;){if(e&&null!=(t=n[e]))return t.call(n);if(o&&null!=(t=n[o]))return new r(t.call(n));e="@@asyncIterator",o="@@iterator"}throw new TypeError("Object is not async iterable")}(e),a=function(n,t,e){for(var r;;){var o=n();if(f(o)&&(o=o.v),!o)return a;if(o.then){r=0;break}var a=e();if(a&&a.then){if(!f(a)){r=1;break}a=a.s}if(t){var c=t();if(c&&c.then&&!f(c)){r=2;break}}}var s=new u,l=i.bind(null,s,2);return(0===r?o.then(v):1===r?a.then(h):c.then(d)).then(void 0,l),s;function h(r){a=r;do{if(t&&(c=t())&&c.then&&!f(c))return void c.then(d).then(void 0,l);if(!(o=n())||f(o)&&!o.v)return void i(s,1,a);if(o.then)return void o.then(v).then(void 0,l);f(a=e())&&(a=a.v)}while(!a||!a.then);a.then(h).then(void 0,l)}function v(n){n?(a=e())&&a.then?a.then(h).then(void 0,l):h(a):i(s,1,a)}function d(){(o=n())?o.then?o.then(v).then(void 0,l):v(o):i(s,1,a)}}(function(){return!!Promise.resolve(o.next()).then(function(e){return t=!(n=e).done})},function(){return!!(t=!1)},function(){self.postMessage(n.value)});if(a&&a.then)return a.then(function(){})}()}catch(n){return o(n)}return a&&a.then?a.then(void 0,o):a}(0,function(t){c=!0,n=t})},function(e,r){function o(n){if(e)throw r;return r}var i=a(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,e){if(c)throw n;if(t)throw e;return e});return i&&i.then?i.then(o):o()})}var s=function(){if(!e[Symbol.iterator]){var n=self,t=n.postMessage;return Promise.resolve(e).then(function(e){t.call(n,e)})}for(var r,i=function(n,t){var e="undefined"!=typeof Symbol&&n[Symbol.iterator]||n["@@iterator"];if(e)return(e=e.call(n)).next.bind(e);if(Array.isArray(n)||(e=function(n,t){if(n){if("string"==typeof n)return o(n,t);var e=Object.prototype.toString.call(n).slice(8,-1);return"Object"===e&&n.constructor&&(e=n.constructor.name),"Map"===e||"Set"===e?Array.from(n):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?o(n,t):void 0}}(n))){e&&(n=e);var r=0;return function(){return r>=n.length?{done:!0}:{done:!1,value:n[r++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(e);!(r=i()).done;)self.postMessage(r.value)}();if(s&&s.then)return s.then(function(){})}())}catch(t){return Promise.reject(t)}}},n.useWorker=function(n,r){var o=t.useState(e),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(e)}},[f]),t.useEffect(function(){a.current.postMessage(r)},[r]),i}}); | ||
//# sourceMappingURL=index.umd.js.map |
@@ -1,3 +0,3 @@ | ||
declare type State = { | ||
result?: unknown; | ||
declare type State<Result> = { | ||
result?: Result; | ||
error?: 'error' | 'messageerror'; | ||
@@ -23,3 +23,3 @@ }; | ||
*/ | ||
export declare function useWorker(createWorker: () => Worker, input: unknown): State; | ||
export declare function useWorker<Input, Result>(createWorker: () => Worker, input: Input): State<Result>; | ||
export {}; |
{ | ||
"name": "react-hooks-worker", | ||
"description": "React custom hooks for web workers", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"author": "Daishi Kato", | ||
@@ -52,9 +52,9 @@ "repository": { | ||
"@types/jest": "^27.0.2", | ||
"@types/react": "^17.0.29", | ||
"@types/react-dom": "^17.0.9", | ||
"@types/react": "^17.0.34", | ||
"@types/react-dom": "^17.0.11", | ||
"@types/react-test-renderer": "^17.0.1", | ||
"@typescript-eslint/eslint-plugin": "^5.0.0", | ||
"@typescript-eslint/parser": "^5.0.0", | ||
"@typescript-eslint/eslint-plugin": "^5.3.0", | ||
"@typescript-eslint/parser": "^5.3.0", | ||
"documentation": "^13.2.5", | ||
"eslint": "^8.0.0", | ||
"eslint": "^8.1.0", | ||
"eslint-config-airbnb": "^18.2.1", | ||
@@ -65,15 +65,15 @@ "eslint-plugin-import": "^2.25.2", | ||
"eslint-plugin-react-hooks": "^4.2.0", | ||
"html-webpack-plugin": "^5.3.2", | ||
"jest": "^27.2.5", | ||
"microbundle": "^0.14.0", | ||
"html-webpack-plugin": "^5.5.0", | ||
"jest": "^27.3.1", | ||
"microbundle": "^0.14.1", | ||
"npm-run-all": "^4.1.5", | ||
"react": "^16.13.1", | ||
"react": "^17.0.2", | ||
"react-dom": "^17.0.2", | ||
"react-test-renderer": "^17.0.2", | ||
"ts-jest": "^27.0.5", | ||
"ts-jest": "^27.0.7", | ||
"ts-loader": "^9.2.6", | ||
"typescript": "^4.4.4", | ||
"webpack": "^5.58.2", | ||
"webpack-cli": "^4.9.0", | ||
"webpack-dev-server": "^4.3.1" | ||
"webpack": "^5.61.0", | ||
"webpack-cli": "^4.9.1", | ||
"webpack-dev-server": "^4.4.0" | ||
}, | ||
@@ -80,0 +80,0 @@ "peerDependencies": { |
@@ -8,8 +8,8 @@ import { | ||
type State = { | ||
result?: unknown; | ||
type State<Result> = { | ||
result?: Result; | ||
error?: 'error' | 'messageerror'; | ||
}; | ||
const initialState: State = {}; | ||
const initialState = {}; | ||
@@ -34,4 +34,4 @@ /** | ||
*/ | ||
export function useWorker(createWorker: () => Worker, input: unknown) { | ||
const [state, setState] = useState<State>(initialState); | ||
export function useWorker<Input, Result>(createWorker: () => Worker, input: Input) { | ||
const [state, setState] = useState<State<Result>>(initialState); | ||
const worker = useMemo(createWorker, [createWorker]); | ||
@@ -41,3 +41,3 @@ const lastWorker = useRef<Worker>(worker); | ||
lastWorker.current = worker; | ||
let setStateSafe = (nextState: State) => setState(nextState); | ||
let setStateSafe = (nextState: State<Result>) => setState(nextState); | ||
worker.onmessage = (e) => setStateSafe({ result: e.data }); | ||
@@ -44,0 +44,0 @@ worker.onerror = () => setStateSafe({ error: 'error' }); |
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
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
31222
162