UNPKG

react-hooks-worker

Version:
3 lines (2 loc) 4.86 kB
!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;const 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,t){self.onmessage=function(e){try{var c=n(e.data);return Promise.resolve(function(){if(c&&c[Symbol.asyncIterator]){var n,e=!1,s=!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")}(c),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(t){return e=!(n=t).done})},function(){return!!(e=!1)},function(){self.postMessage(n.value,null==t?void 0:t())});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){s=!0,n=t})},function(t,r){function o(n){if(t)throw r;return r}var i=a(function(){var n=function(){if(e&&null!=_iterator.return)return Promise.resolve(_iterator.return()).then(function(){})}();if(n&&n.then)return n.then(function(){})},function(t,e){if(s)throw n;if(t)throw e;return e});return i&&i.then?i.then(o):o()})}var l=function(){if(!c||!c[Symbol.iterator]){var n=self,e=n.postMessage;return Promise.resolve(c).then(function(r){e.call(n,r,null==t?void 0:t())})}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.")}(c);!(r=i()).done;)self.postMessage(r.value,null==t?void 0:t())}();if(l&&l.then)return l.then(function(){})}())}catch(e){return Promise.reject(e)}}},n.useWorker=function(n,r,o){var i=t.useState(e),u=i[0],f=i[1],a=t.useMemo(n,[n]),c=t.useRef(a);return t.useEffect(function(){c.current=a;var n=function(n){return f(n)};return a.onmessage=function(t){return n({result:t.data})},a.onerror=function(){return n({error:"error"})},a.onmessageerror=function(){return n({error:"messageerror"})},function(){n=function(){return null},a.terminate(),f(e)}},[a]),t.useEffect(function(){c.current.postMessage(r,null==o?void 0:o())},[r,o]),u}}); //# sourceMappingURL=index.umd.js.map