worker-factory
Advanced tools
Comparing version 2.5.6 to 3.0.0
import { IBrokerEvent, IReceiver, IWorkerDefinition } from '../interfaces'; | ||
import { TWorkerImplementation } from '../types'; | ||
export declare const createMessageHandler: <T extends IWorkerDefinition>(receiver: IReceiver, workerImplementation: TWorkerImplementation<T>, isSupportingTransferables: Promise<boolean>) => ({ data: { id, method, params } }: IBrokerEvent<T>) => Promise<void>; | ||
export declare const createMessageHandler: <T extends IWorkerDefinition>(receiver: IReceiver, workerImplementation: TWorkerImplementation<T>) => ({ data: { id, method, params } }: IBrokerEvent<T>) => Promise<void>; | ||
//# sourceMappingURL=/build/es2015/helpers/create-message-handler.d.ts.map |
import * as tslib_1 from "tslib"; | ||
import { renderMethodNotFoundError, renderMissingResponseError, renderUnexpectedResultError } from './error-renderers'; | ||
export const createMessageHandler = (receiver, workerImplementation, isSupportingTransferables) => { | ||
export const createMessageHandler = (receiver, workerImplementation) => { | ||
return ({ data: { id, method, params } }) => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
@@ -25,3 +25,3 @@ const messageHandler = workerImplementation[method]; | ||
const { result, transferables = [] } = synchronousResponse; | ||
receiver.postMessage({ id, result }, (yield isSupportingTransferables) ? transferables : []); | ||
receiver.postMessage({ id, result }, transferables); | ||
} | ||
@@ -28,0 +28,0 @@ } |
import { IDefaultWorkerDefinition, IReceiver, IWorkerDefinition } from '../interfaces'; | ||
import { TDestroyWorkerFunction, TWorkerImplementation } from '../types'; | ||
export declare const extendWorkerImplementation: <T extends IWorkerDefinition>(createWorker: (receiver: IReceiver, workerImplementation: TWorkerImplementation<T>) => TDestroyWorkerFunction, partialWorkerImplementation: TWorkerImplementation<T>) => TWorkerImplementation<T & IDefaultWorkerDefinition>; | ||
export declare const extendWorkerImplementation: <T extends IWorkerDefinition>(createWorker: (receiver: IReceiver, workerImplementation: TWorkerImplementation<T>) => TDestroyWorkerFunction, partialWorkerImplementation: TWorkerImplementation<T>, isSupportedFunction: () => boolean | Promise<boolean>) => TWorkerImplementation<T & IDefaultWorkerDefinition>; | ||
//# sourceMappingURL=/build/es2015/helpers/extend-worker-implementation.d.ts.map |
@@ -0,5 +1,7 @@ | ||
import * as tslib_1 from "tslib"; | ||
import { generateUniqueNumber } from 'fast-unique-numbers'; | ||
import { renderUnknownPortIdError } from './error-renderers'; | ||
import { isSupportingTransferables } from './is-supporting-transferables'; | ||
const DESTROY_WORKER_FUNCTIONS = new Map(); | ||
export const extendWorkerImplementation = (createWorker, partialWorkerImplementation) => { | ||
export const extendWorkerImplementation = (createWorker, partialWorkerImplementation, isSupportedFunction) => { | ||
return Object.assign({}, partialWorkerImplementation, { connect: ({ port }) => { | ||
@@ -22,4 +24,14 @@ port.start(); | ||
return { result: null }; | ||
} }); | ||
}, | ||
// @todo It should be okay to define a function without a parameter if it doesn't need one. | ||
isSupported: (_) => tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const isSelfSupported = yield isSupportingTransferables(); | ||
if (isSelfSupported) { | ||
const result = isSupportedFunction(); | ||
const synchronousResult = (result instanceof Promise) ? yield result : result; | ||
return { result: synchronousResult }; | ||
} | ||
return { result: false }; | ||
}) }); | ||
}; | ||
//# sourceMappingURL=/build/es2015/helpers/extend-worker-implementation.js.map |
@@ -1,2 +0,2 @@ | ||
export declare const isSupportingTransferables: Promise<boolean>; | ||
export declare const isSupportingTransferables: () => Promise<boolean>; | ||
//# sourceMappingURL=/build/es2015/helpers/is-supporting-transferables.d.ts.map |
// Bug #1: Safari does currently not support to use transferables. | ||
export const isSupportingTransferables = new Promise((resolve) => { | ||
export const isSupportingTransferables = () => new Promise((resolve) => { | ||
const arrayBuffer = new ArrayBuffer(0); | ||
@@ -4,0 +4,0 @@ const { port1, port2 } = new MessageChannel(); |
@@ -19,3 +19,8 @@ import { IWorkerDefinition } from './worker-definition'; | ||
}; | ||
isSupported: { | ||
response: { | ||
result: boolean; | ||
}; | ||
}; | ||
} | ||
//# sourceMappingURL=/build/es2015/interfaces/default-worker-definition.d.ts.map |
@@ -0,1 +1,2 @@ | ||
import { isSupportingTransferables } from './helpers/is-supporting-transferables'; | ||
import { IReceiver, IWorkerDefinition } from './interfaces'; | ||
@@ -5,3 +6,4 @@ import { TDestroyWorkerFunction, TWorkerImplementation } from './types'; | ||
export * from './types'; | ||
export declare const createWorker: <T extends IWorkerDefinition>(receiver: IReceiver, workerImplementation: TWorkerImplementation<T>) => TDestroyWorkerFunction; | ||
export declare const createWorker: <T extends IWorkerDefinition>(receiver: IReceiver, workerImplementation: TWorkerImplementation<T>, isSupportedFunction?: () => boolean | Promise<boolean>) => TDestroyWorkerFunction; | ||
export { isSupportingTransferables as isSupported }; | ||
//# sourceMappingURL=/build/es2015/module.d.ts.map |
import { createMessageHandler } from './helpers/create-message-handler'; | ||
import { extendWorkerImplementation } from './helpers/extend-worker-implementation'; | ||
import { isSupportingTransferables } from './helpers/is-supporting-transferables'; | ||
export const createWorker = (receiver, workerImplementation) => { | ||
const fullWorkerImplementation = extendWorkerImplementation(createWorker, workerImplementation); | ||
const messageHandler = createMessageHandler(receiver, fullWorkerImplementation, isSupportingTransferables); | ||
export const createWorker = (receiver, workerImplementation, isSupportedFunction = () => true) => { | ||
const fullWorkerImplementation = extendWorkerImplementation(createWorker, workerImplementation, isSupportedFunction); | ||
const messageHandler = createMessageHandler(receiver, fullWorkerImplementation); | ||
receiver.addEventListener('message', messageHandler); | ||
return () => receiver.removeEventListener('message', messageHandler); | ||
}; | ||
export { isSupportingTransferables as isSupported }; | ||
//# sourceMappingURL=/build/es2015/module.js.map |
@@ -32,3 +32,3 @@ (function (global, factory) { | ||
var _this = undefined; | ||
var createMessageHandler = function createMessageHandler(receiver, workerImplementation, isSupportingTransferables) { | ||
var createMessageHandler = function createMessageHandler(receiver, workerImplementation) { | ||
return function (_ref) { | ||
@@ -107,3 +107,3 @@ var _ref$data = _ref.data, | ||
case 18: | ||
_context.next = 34; | ||
_context.next = 24; | ||
break; | ||
@@ -123,36 +123,15 @@ | ||
result = synchronousResponse.result, _synchronousResponse$ = synchronousResponse.transferables, transferables = _synchronousResponse$ === void 0 ? [] : _synchronousResponse$; | ||
_context.t1 = receiver; | ||
_context.t2 = { | ||
receiver.postMessage({ | ||
id: id, | ||
result: result | ||
}; | ||
_context.next = 27; | ||
return isSupportingTransferables; | ||
}, transferables); | ||
case 27: | ||
if (!_context.sent) { | ||
_context.next = 31; | ||
break; | ||
} | ||
_context.t3 = transferables; | ||
_context.next = 32; | ||
case 24: | ||
_context.next = 30; | ||
break; | ||
case 31: | ||
_context.t3 = []; | ||
case 32: | ||
_context.t4 = _context.t3; | ||
_context.t1.postMessage.call(_context.t1, _context.t2, _context.t4); | ||
case 34: | ||
_context.next = 40; | ||
break; | ||
case 36: | ||
_context.prev = 36; | ||
_context.t5 = _context["catch"](1); | ||
message = _context.t5.message, _err$status = _context.t5.status, status = _err$status === void 0 ? -32603 : _err$status; | ||
case 26: | ||
_context.prev = 26; | ||
_context.t1 = _context["catch"](1); | ||
message = _context.t1.message, _err$status = _context.t1.status, status = _err$status === void 0 ? -32603 : _err$status; | ||
receiver.postMessage({ | ||
@@ -166,3 +145,3 @@ error: { | ||
case 40: | ||
case 30: | ||
case "end": | ||
@@ -172,3 +151,3 @@ return _context.stop(); | ||
} | ||
}, _callee, this, [[1, 36]]); | ||
}, _callee, this, [[1, 26]]); | ||
})); | ||
@@ -178,4 +157,23 @@ }; | ||
// Bug #1: Safari does currently not support to use transferables. | ||
var isSupportingTransferables = function isSupportingTransferables() { | ||
return new Promise(function (resolve) { | ||
var arrayBuffer = new ArrayBuffer(0); | ||
var _ref = new MessageChannel(), | ||
port1 = _ref.port1, | ||
port2 = _ref.port2; | ||
port1.onmessage = function (_ref2) { | ||
var data = _ref2.data; | ||
return resolve(data !== null); | ||
}; | ||
port2.postMessage(arrayBuffer, [arrayBuffer]); | ||
}); | ||
}; | ||
var _this$1 = undefined; | ||
var DESTROY_WORKER_FUNCTIONS = new Map(); | ||
var extendWorkerImplementation = function extendWorkerImplementation(createWorker, partialWorkerImplementation) { | ||
var extendWorkerImplementation = function extendWorkerImplementation(createWorker, partialWorkerImplementation, isSupportedFunction) { | ||
return Object.assign({}, partialWorkerImplementation, { | ||
@@ -210,25 +208,70 @@ connect: function connect(_ref) { | ||
}; | ||
} | ||
}); | ||
}; | ||
}, | ||
// @todo It should be okay to define a function without a parameter if it doesn't need one. | ||
isSupported: function isSupported(_) { | ||
return tslib_1.__awaiter(_this$1, void 0, void 0, | ||
/*#__PURE__*/ | ||
_regeneratorRuntime.mark(function _callee() { | ||
var isSelfSupported, result, synchronousResult; | ||
return _regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.next = 2; | ||
return isSupportingTransferables(); | ||
// Bug #1: Safari does currently not support to use transferables. | ||
var isSupportingTransferables = new Promise(function (resolve) { | ||
var arrayBuffer = new ArrayBuffer(0); | ||
case 2: | ||
isSelfSupported = _context.sent; | ||
var _ref = new MessageChannel(), | ||
port1 = _ref.port1, | ||
port2 = _ref.port2; | ||
if (!isSelfSupported) { | ||
_context.next = 14; | ||
break; | ||
} | ||
port1.onmessage = function (_ref2) { | ||
var data = _ref2.data; | ||
return resolve(data !== null); | ||
}; | ||
result = isSupportedFunction(); | ||
port2.postMessage(arrayBuffer, [arrayBuffer]); | ||
}); | ||
if (!(result instanceof Promise)) { | ||
_context.next = 11; | ||
break; | ||
} | ||
_context.next = 8; | ||
return result; | ||
case 8: | ||
_context.t0 = _context.sent; | ||
_context.next = 12; | ||
break; | ||
case 11: | ||
_context.t0 = result; | ||
case 12: | ||
synchronousResult = _context.t0; | ||
return _context.abrupt("return", { | ||
result: synchronousResult | ||
}); | ||
case 14: | ||
return _context.abrupt("return", { | ||
result: false | ||
}); | ||
case 15: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, this); | ||
})); | ||
} | ||
}); | ||
}; | ||
var createWorker = function createWorker(receiver, workerImplementation) { | ||
var fullWorkerImplementation = extendWorkerImplementation(createWorker, workerImplementation); | ||
var messageHandler = createMessageHandler(receiver, fullWorkerImplementation, isSupportingTransferables); | ||
var isSupportedFunction = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : function () { | ||
return true; | ||
}; | ||
var fullWorkerImplementation = extendWorkerImplementation(createWorker, workerImplementation, isSupportedFunction); | ||
var messageHandler = createMessageHandler(receiver, fullWorkerImplementation); | ||
receiver.addEventListener('message', messageHandler); | ||
@@ -241,2 +284,3 @@ return function () { | ||
exports.createWorker = createWorker; | ||
exports.isSupported = isSupportingTransferables; | ||
@@ -243,0 +287,0 @@ Object.defineProperty(exports, '__esModule', { value: true }); |
@@ -1,1 +0,1 @@ | ||
!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=8)}([function(e,t,n){!function(e,t,n,r){"use strict";t=t&&t.hasOwnProperty("default")?t.default:t,n=n&&n.hasOwnProperty("default")?n.default:n,r=r&&r.hasOwnProperty("default")?r.default:r;var o=function(e,t){return void 0===t?e:t.reduce(function(e,t){if("capitalize"===t){var o=e.charAt(0).toUpperCase(),i=e.slice(1);return"".concat(o).concat(i)}return"dashify"===t?n(e):"prependIndefiniteArticle"===t?"".concat(r(e)," ").concat(e):e},e)},i=function(e,n){for(var r=/\${([^.}]+)((\.[^(]+\(\))*)}/g,i=[],a=r.exec(e);null!==a;){var u={modifiers:[],name:a[1]};if(void 0!==a[3])for(var c=/\.[^(]+\(\)/g,s=c.exec(a[2]);null!==s;)u.modifiers.push(s[0].slice(1,-2)),s=c.exec(a[2]);i.push(u),a=r.exec(e)}var d=i.reduce(function(e,r){return e.map(function(e){return"string"==typeof e?e.split(function(e){var t=e.name+e.modifiers.map(function(e){return"\\.".concat(e,"\\(\\)")}).join("");return new RegExp("\\$\\{".concat(t,"}"),"g")}(r)).reduce(function(e,i,a){return 0===a?[i]:r.name in n?t(e).concat([o(n[r.name],r.modifiers),i]):t(e).concat([function(e){return o(e[r.name],r.modifiers)},i])},[]):[e]}).reduce(function(e,n){return t(e).concat(t(n))},[])},[e]);return function(e){return d.reduce(function(n,r){return"string"==typeof r?t(n).concat([r]):t(n).concat([r(e)])},[]).join("")}};e.compile=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=void 0===e.code?void 0:i(e.code,t),r=void 0===e.message?void 0:i(e.message,t);return function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},o=arguments.length>1?arguments[1]:void 0,i=void 0===o&&(t instanceof Error||void 0!==t.code&&"Exception"===t.code.slice(-9))?{cause:t,missingParameters:{}}:{cause:o,missingParameters:t},a=i.cause,u=i.missingParameters,c=void 0===r?new Error:new Error(r(u));return null!==a&&(c.cause=a),void 0!==n&&(c.code=n(u)),void 0!==e.status&&(c.status=e.status),c}},Object.defineProperty(e,"__esModule",{value:!0})}(t,n(2),n(6),n(7))},function(e,t,n){!function(e){"use strict";var t=new WeakMap,n=Number.MAX_SAFE_INTEGER||9007199254740991,r=function(e,n){return t.set(e,n),n},o=function(e){var o=t.get(e),i=void 0===o?e.size:o>2147483648?0:o+1;if(!e.has(i))return r(e,i);if(e.size<1073741824){for(;e.has(i);)i=Math.floor(2147483648*Math.random());return r(e,i)}if(e.size>n)throw new Error("Congratulations, you created a collection of unique numbers which uses all available integers!");for(;e.has(i);)i=Math.floor(Math.random()*n);return r(e,i)};e.addUniqueNumber=function(e){var t=o(e);return e.add(t),t},e.generateUniqueNumber=o,Object.defineProperty(e,"__esModule",{value:!0})}(t)},function(e,t,n){var r=n(3),o=n(4),i=n(5);e.exports=function(e){return r(e)||o(e)||i()}},function(e,t){e.exports=function(e){if(Array.isArray(e)){for(var t=0,n=new Array(e.length);t<e.length;t++)n[t]=e[t];return n}}},function(e,t){e.exports=function(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}},function(e,t){e.exports=function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}},function(e,t,n){"use strict";e.exports=function(e,t){if("string"!=typeof e)throw new TypeError("expected a string");return e.trim().replace(/([a-z])([A-Z])/g,"$1-$2").replace(/\W/g,e=>/[À-ž]/.test(e)?e:"-").replace(/^-+|-+$/g,"").replace(/-{2,}/g,e=>t&&t.condense?"-":e).toLowerCase()}},function(e,t){var n=function(e){var t,n,r=/\w+/.exec(e);if(!r)return"an";var o=(n=r[0]).toLowerCase(),i=["honest","hour","hono"];for(t in i)if(0==o.indexOf(i[t]))return"an";if(1==o.length)return"aedhilmnorsx".indexOf(o)>=0?"an":"a";if(n.match(/(?!FJO|[HLMNS]Y.|RY[EO]|SQU|(F[LR]?|[HL]|MN?|N|RH?|S[CHKLMNPTVW]?|X(YL)?)[AEIOU])[FHLMNRSX][A-Z]/))return"an";var a=[/^e[uw]/,/^onc?e\b/,/^uni([^nmd]|mo)/,/^u[bcfhjkqrst][aeiou]/];for(t=0;t<a.length;t++)if(o.match(a[t]))return"a";return n.match(/^U[NK][AIEO]/)?"a":n==n.toUpperCase()?"aedhilmnorsx".indexOf(o[0])>=0?"an":"a":"aeiou".indexOf(o[0])>=0?"an":o.match(/^y(b[lor]|cl[ea]|fere|gg|p[ios]|rou|tt)/)?"an":"a"};void 0!==e&&void 0!==e.exports?e.exports=n:window.indefiniteArticle=n},function(e,t,n){"use strict";n.r(t);var r=n(0);const o=-32603,i=-32602,a=-32601,u=Object(r.compile)({message:'The requested method called "${method}" is not supported.',status:a}),c=Object(r.compile)({message:'The handler of the method called "${method}" returned no required result.',status:o}),s=Object(r.compile)({message:'The handler of the method called "${method}" returned an unexpected result.',status:o}),d=Object(r.compile)({message:'The specified parameter called "portId" with the given value "${portId}" does not identify a port connected to this worker.',status:i}),f=(e,t,n)=>({data:{id:r,method:o,params:i}})=>(function(e,t,n,r){return new(n||(n=Promise))(function(o,i){function a(e){try{c(r.next(e))}catch(e){i(e)}}function u(e){try{c(r.throw(e))}catch(e){i(e)}}function c(e){e.done?o(e.value):new n(function(t){t(e.value)}).then(a,u)}c((r=r.apply(e,t||[])).next())})})(void 0,void 0,void 0,function*(){const a=t[o];try{if(void 0===a)throw u({method:o});const t=a(i);if(void 0===t)throw c({method:o});const d=t instanceof Promise?yield t:t;if(null===r){if(void 0!==d.result)throw s({method:o})}else{if(void 0===d.result)throw s({method:o});const t=d.result,i=d.transferables,a=void 0===i?[]:i;e.postMessage({id:r,result:t},(yield n)?a:[])}}catch(t){const n=t.message,o=t.status,i=void 0===o?-32603:o;e.postMessage({error:{code:i,message:n},id:r})}});var l=n(1);const p=new Map,m=new Promise(e=>{const t=new ArrayBuffer(0),n=new MessageChannel,r=n.port1,o=n.port2;r.onmessage=(({data:t})=>e(null!==t)),o.postMessage(t,[t])});n.d(t,"createWorker",function(){return h});const h=(e,t)=>{const n=((e,t)=>Object.assign({},t,{connect:({port:n})=>{n.start();const r=e(n,t),o=Object(l.generateUniqueNumber)(p);return p.set(o,()=>{r(),n.close(),p.delete(o)}),{result:o}},disconnect:({portId:e})=>{const t=p.get(e);if(void 0===t)throw d({portId:e.toString()});return t(),{result:null}}}))(h,t),r=f(e,n,m);return e.addEventListener("message",r),()=>e.removeEventListener("message",r)}}]); | ||
!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=8)}([function(e,t,n){!function(e,t,n,r){"use strict";t=t&&t.hasOwnProperty("default")?t.default:t,n=n&&n.hasOwnProperty("default")?n.default:n,r=r&&r.hasOwnProperty("default")?r.default:r;var o=function(e,t){return void 0===t?e:t.reduce(function(e,t){if("capitalize"===t){var o=e.charAt(0).toUpperCase(),i=e.slice(1);return"".concat(o).concat(i)}return"dashify"===t?n(e):"prependIndefiniteArticle"===t?"".concat(r(e)," ").concat(e):e},e)},i=function(e,n){for(var r=/\${([^.}]+)((\.[^(]+\(\))*)}/g,i=[],a=r.exec(e);null!==a;){var u={modifiers:[],name:a[1]};if(void 0!==a[3])for(var c=/\.[^(]+\(\)/g,s=c.exec(a[2]);null!==s;)u.modifiers.push(s[0].slice(1,-2)),s=c.exec(a[2]);i.push(u),a=r.exec(e)}var d=i.reduce(function(e,r){return e.map(function(e){return"string"==typeof e?e.split(function(e){var t=e.name+e.modifiers.map(function(e){return"\\.".concat(e,"\\(\\)")}).join("");return new RegExp("\\$\\{".concat(t,"}"),"g")}(r)).reduce(function(e,i,a){return 0===a?[i]:r.name in n?t(e).concat([o(n[r.name],r.modifiers),i]):t(e).concat([function(e){return o(e[r.name],r.modifiers)},i])},[]):[e]}).reduce(function(e,n){return t(e).concat(t(n))},[])},[e]);return function(e){return d.reduce(function(n,r){return"string"==typeof r?t(n).concat([r]):t(n).concat([r(e)])},[]).join("")}};e.compile=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=void 0===e.code?void 0:i(e.code,t),r=void 0===e.message?void 0:i(e.message,t);return function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},o=arguments.length>1?arguments[1]:void 0,i=void 0===o&&(t instanceof Error||void 0!==t.code&&"Exception"===t.code.slice(-9))?{cause:t,missingParameters:{}}:{cause:o,missingParameters:t},a=i.cause,u=i.missingParameters,c=void 0===r?new Error:new Error(r(u));return null!==a&&(c.cause=a),void 0!==n&&(c.code=n(u)),void 0!==e.status&&(c.status=e.status),c}},Object.defineProperty(e,"__esModule",{value:!0})}(t,n(2),n(6),n(7))},function(e,t,n){!function(e){"use strict";var t=new WeakMap,n=Number.MAX_SAFE_INTEGER||9007199254740991,r=function(e,n){return t.set(e,n),n},o=function(e){var o=t.get(e),i=void 0===o?e.size:o>2147483648?0:o+1;if(!e.has(i))return r(e,i);if(e.size<1073741824){for(;e.has(i);)i=Math.floor(2147483648*Math.random());return r(e,i)}if(e.size>n)throw new Error("Congratulations, you created a collection of unique numbers which uses all available integers!");for(;e.has(i);)i=Math.floor(Math.random()*n);return r(e,i)};e.addUniqueNumber=function(e){var t=o(e);return e.add(t),t},e.generateUniqueNumber=o,Object.defineProperty(e,"__esModule",{value:!0})}(t)},function(e,t,n){var r=n(3),o=n(4),i=n(5);e.exports=function(e){return r(e)||o(e)||i()}},function(e,t){e.exports=function(e){if(Array.isArray(e)){for(var t=0,n=new Array(e.length);t<e.length;t++)n[t]=e[t];return n}}},function(e,t){e.exports=function(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}},function(e,t){e.exports=function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}},function(e,t,n){"use strict";e.exports=function(e,t){if("string"!=typeof e)throw new TypeError("expected a string");return e.trim().replace(/([a-z])([A-Z])/g,"$1-$2").replace(/\W/g,e=>/[À-ž]/.test(e)?e:"-").replace(/^-+|-+$/g,"").replace(/-{2,}/g,e=>t&&t.condense?"-":e).toLowerCase()}},function(e,t){var n=function(e){var t,n,r=/\w+/.exec(e);if(!r)return"an";var o=(n=r[0]).toLowerCase(),i=["honest","hour","hono"];for(t in i)if(0==o.indexOf(i[t]))return"an";if(1==o.length)return"aedhilmnorsx".indexOf(o)>=0?"an":"a";if(n.match(/(?!FJO|[HLMNS]Y.|RY[EO]|SQU|(F[LR]?|[HL]|MN?|N|RH?|S[CHKLMNPTVW]?|X(YL)?)[AEIOU])[FHLMNRSX][A-Z]/))return"an";var a=[/^e[uw]/,/^onc?e\b/,/^uni([^nmd]|mo)/,/^u[bcfhjkqrst][aeiou]/];for(t=0;t<a.length;t++)if(o.match(a[t]))return"a";return n.match(/^U[NK][AIEO]/)?"a":n==n.toUpperCase()?"aedhilmnorsx".indexOf(o[0])>=0?"an":"a":"aeiou".indexOf(o[0])>=0?"an":o.match(/^y(b[lor]|cl[ea]|fere|gg|p[ios]|rou|tt)/)?"an":"a"};void 0!==e&&void 0!==e.exports?e.exports=n:window.indefiniteArticle=n},function(e,t,n){"use strict";n.r(t);function r(e,t,n,r){return new(n||(n=Promise))(function(o,i){function a(e){try{c(r.next(e))}catch(e){i(e)}}function u(e){try{c(r.throw(e))}catch(e){i(e)}}function c(e){e.done?o(e.value):new n(function(t){t(e.value)}).then(a,u)}c((r=r.apply(e,t||[])).next())})}var o=n(0);const i=-32603,a=-32602,u=-32601,c=Object(o.compile)({message:'The requested method called "${method}" is not supported.',status:u}),s=Object(o.compile)({message:'The handler of the method called "${method}" returned no required result.',status:i}),d=Object(o.compile)({message:'The handler of the method called "${method}" returned an unexpected result.',status:i}),f=Object(o.compile)({message:'The specified parameter called "portId" with the given value "${portId}" does not identify a port connected to this worker.',status:a});var l=n(1);const p=()=>new Promise(e=>{const t=new ArrayBuffer(0),n=new MessageChannel,r=n.port1,o=n.port2;r.onmessage=(({data:t})=>e(null!==t)),o.postMessage(t,[t])}),m=new Map;n.d(t,"createWorker",function(){return h}),n.d(t,"isSupported",function(){return p});const h=(e,t,n=(()=>!0))=>{const o=((e,t,n)=>Object.assign({},t,{connect:({port:n})=>{n.start();const r=e(n,t),o=Object(l.generateUniqueNumber)(m);return m.set(o,()=>{r(),n.close(),m.delete(o)}),{result:o}},disconnect:({portId:e})=>{const t=m.get(e);if(void 0===t)throw f({portId:e.toString()});return t(),{result:null}},isSupported:e=>r(void 0,void 0,void 0,function*(){if(yield p()){const e=n();return{result:e instanceof Promise?yield e:e}}return{result:!1}})}))(h,t,n),i=((e,t)=>({data:{id:n,method:o,params:i}})=>r(void 0,void 0,void 0,function*(){const r=t[o];try{if(void 0===r)throw c({method:o});const t=r(i);if(void 0===t)throw s({method:o});const a=t instanceof Promise?yield t:t;if(null===n){if(void 0!==a.result)throw d({method:o})}else{if(void 0===a.result)throw d({method:o});const t=a.result,r=a.transferables,i=void 0===r?[]:r;e.postMessage({id:n,result:t},i)}}catch(t){const r=t.message,o=t.status,i=void 0===o?-32603:o;e.postMessage({error:{code:i,message:r},id:n})}}))(e,o);return e.addEventListener("message",i),()=>e.removeEventListener("message",i)}}]); |
var _this = this; | ||
import * as tslib_1 from "tslib"; | ||
import { renderMethodNotFoundError, renderMissingResponseError, renderUnexpectedResultError } from './error-renderers'; | ||
export var createMessageHandler = function (receiver, workerImplementation, isSupportingTransferables) { | ||
export var createMessageHandler = function (receiver, workerImplementation) { | ||
return function (_a) { | ||
var _b = _a.data, id = _b.id, method = _b.method, params = _b.params; | ||
return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var messageHandler, response, synchronousResponse, _c, _d, result, _e, transferables, _f, _g, _h, err_1, _j, message, _k, status; | ||
return tslib_1.__generator(this, function (_l) { | ||
switch (_l.label) { | ||
var messageHandler, response, synchronousResponse, _c, _d, result, _e, transferables, err_1, _f, message, _g, status; | ||
return tslib_1.__generator(this, function (_h) { | ||
switch (_h.label) { | ||
case 0: | ||
messageHandler = workerImplementation[method]; | ||
_l.label = 1; | ||
_h.label = 1; | ||
case 1: | ||
_l.trys.push([1, 8, , 9]); | ||
_h.trys.push([1, 5, , 6]); | ||
if (messageHandler === undefined) { | ||
@@ -26,32 +26,28 @@ throw renderMethodNotFoundError({ method: method }); | ||
case 2: | ||
_c = _l.sent(); | ||
_c = _h.sent(); | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
_c = response; | ||
_l.label = 4; | ||
_h.label = 4; | ||
case 4: | ||
synchronousResponse = _c; | ||
if (!(id === null)) return [3 /*break*/, 5]; | ||
if (synchronousResponse.result !== undefined) { | ||
throw renderUnexpectedResultError({ method: method }); | ||
if (id === null) { | ||
if (synchronousResponse.result !== undefined) { | ||
throw renderUnexpectedResultError({ method: method }); | ||
} | ||
} | ||
return [3 /*break*/, 7]; | ||
else { | ||
if (synchronousResponse.result === undefined) { | ||
throw renderUnexpectedResultError({ method: method }); | ||
} | ||
_d = synchronousResponse, result = _d.result, _e = _d.transferables, transferables = _e === void 0 ? [] : _e; | ||
receiver.postMessage({ id: id, result: result }, transferables); | ||
} | ||
return [3 /*break*/, 6]; | ||
case 5: | ||
if (synchronousResponse.result === undefined) { | ||
throw renderUnexpectedResultError({ method: method }); | ||
} | ||
_d = synchronousResponse, result = _d.result, _e = _d.transferables, transferables = _e === void 0 ? [] : _e; | ||
_g = (_f = receiver).postMessage; | ||
_h = [{ id: id, result: result }]; | ||
return [4 /*yield*/, isSupportingTransferables]; | ||
case 6: | ||
_g.apply(_f, _h.concat([(_l.sent()) ? transferables : []])); | ||
_l.label = 7; | ||
case 7: return [3 /*break*/, 9]; | ||
case 8: | ||
err_1 = _l.sent(); | ||
_j = err_1, message = _j.message, _k = _j.status, status = _k === void 0 ? -32603 : _k; | ||
err_1 = _h.sent(); | ||
_f = err_1, message = _f.message, _g = _f.status, status = _g === void 0 ? -32603 : _g; | ||
receiver.postMessage({ error: { code: status, message: message }, id: id }); | ||
return [3 /*break*/, 9]; | ||
case 9: return [2 /*return*/]; | ||
return [3 /*break*/, 6]; | ||
case 6: return [2 /*return*/]; | ||
} | ||
@@ -58,0 +54,0 @@ }); |
@@ -0,6 +1,8 @@ | ||
var _this = this; | ||
import * as tslib_1 from "tslib"; | ||
import { generateUniqueNumber } from 'fast-unique-numbers'; | ||
import { renderUnknownPortIdError } from './error-renderers'; | ||
import { isSupportingTransferables } from './is-supporting-transferables'; | ||
var DESTROY_WORKER_FUNCTIONS = new Map(); | ||
export var extendWorkerImplementation = function (createWorker, partialWorkerImplementation) { | ||
export var extendWorkerImplementation = function (createWorker, partialWorkerImplementation, isSupportedFunction) { | ||
return tslib_1.__assign({}, partialWorkerImplementation, { connect: function (_a) { | ||
@@ -25,4 +27,29 @@ var port = _a.port; | ||
return { result: null }; | ||
} }); | ||
}, | ||
// @todo It should be okay to define a function without a parameter if it doesn't need one. | ||
isSupported: function (_) { return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var isSelfSupported, result, synchronousResult, _a; | ||
return tslib_1.__generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: return [4 /*yield*/, isSupportingTransferables()]; | ||
case 1: | ||
isSelfSupported = _b.sent(); | ||
if (!isSelfSupported) return [3 /*break*/, 5]; | ||
result = isSupportedFunction(); | ||
if (!(result instanceof Promise)) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, result]; | ||
case 2: | ||
_a = _b.sent(); | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
_a = result; | ||
_b.label = 4; | ||
case 4: | ||
synchronousResult = _a; | ||
return [2 /*return*/, { result: synchronousResult }]; | ||
case 5: return [2 /*return*/, { result: false }]; | ||
} | ||
}); | ||
}); } }); | ||
}; | ||
//# sourceMappingURL=/build/es2015/helpers/extend-worker-implementation.js.map |
// Bug #1: Safari does currently not support to use transferables. | ||
export var isSupportingTransferables = new Promise(function (resolve) { | ||
export var isSupportingTransferables = function () { return new Promise(function (resolve) { | ||
var arrayBuffer = new ArrayBuffer(0); | ||
@@ -10,3 +10,3 @@ var _a = new MessageChannel(), port1 = _a.port1, port2 = _a.port2; | ||
port2.postMessage(arrayBuffer, [arrayBuffer]); | ||
}); | ||
}); }; | ||
//# sourceMappingURL=/build/es2015/helpers/is-supporting-transferables.js.map |
import { createMessageHandler } from './helpers/create-message-handler'; | ||
import { extendWorkerImplementation } from './helpers/extend-worker-implementation'; | ||
import { isSupportingTransferables } from './helpers/is-supporting-transferables'; | ||
export var createWorker = function (receiver, workerImplementation) { | ||
var fullWorkerImplementation = extendWorkerImplementation(createWorker, workerImplementation); | ||
var messageHandler = createMessageHandler(receiver, fullWorkerImplementation, isSupportingTransferables); | ||
export var createWorker = function (receiver, workerImplementation, isSupportedFunction) { | ||
if (isSupportedFunction === void 0) { isSupportedFunction = function () { return true; }; } | ||
var fullWorkerImplementation = extendWorkerImplementation(createWorker, workerImplementation, isSupportedFunction); | ||
var messageHandler = createMessageHandler(receiver, fullWorkerImplementation); | ||
receiver.addEventListener('message', messageHandler); | ||
return function () { return receiver.removeEventListener('message', messageHandler); }; | ||
}; | ||
export { isSupportingTransferables as isSupported }; | ||
//# sourceMappingURL=/build/es2015/module.js.map |
@@ -19,5 +19,5 @@ { | ||
"devDependencies": { | ||
"@babel/core": "^7.0.1", | ||
"@babel/core": "^7.1.0", | ||
"@babel/plugin-external-helpers": "^7.0.0", | ||
"@babel/plugin-transform-runtime": "^7.0.0", | ||
"@babel/plugin-transform-runtime": "^7.1.0", | ||
"@babel/preset-env": "^7.0.0", | ||
@@ -30,3 +30,3 @@ "@commitlint/cli": "^7.1.2", | ||
"cz-conventional-changelog": "^2.1.0", | ||
"eslint": "^5.5.0", | ||
"eslint": "^5.6.0", | ||
"eslint-config-holy-grail": "^31.0.1", | ||
@@ -50,16 +50,16 @@ "grunt": "^1.0.3", | ||
"karma-sinon-chai": "^2.0.2", | ||
"karma-webpack": "^3.0.4", | ||
"karma-webpack": "^3.0.5", | ||
"load-grunt-config": "^0.19.2", | ||
"mocha": "^5.2.0", | ||
"rollup": "^0.65.2", | ||
"rollup": "^0.66.2", | ||
"rollup-plugin-babel": "^4.0.3", | ||
"sinon": "^6.3.1", | ||
"sinon": "^6.3.4", | ||
"sinon-chai": "^3.2.0", | ||
"ts-loader": "^5.1.0", | ||
"tsconfig-holy-grail": "^6.0.4", | ||
"ts-loader": "^5.1.1", | ||
"tsconfig-holy-grail": "^6.0.6", | ||
"tslint": "^5.11.0", | ||
"tslint-config-holy-grail": "^35.1.0", | ||
"tslint-config-holy-grail": "^35.1.1", | ||
"typescript": "^3.0.3", | ||
"uglifyjs-webpack-plugin": "^1.3.0", | ||
"webpack": "^4.18.0" | ||
"webpack": "^4.19.1" | ||
}, | ||
@@ -88,3 +88,3 @@ "files": [ | ||
"types": "build/es2015/module.d.ts", | ||
"version": "2.5.6" | ||
"version": "3.0.0" | ||
} |
@@ -8,4 +8,3 @@ import { IAugmentedError } from 'compilerr'; | ||
receiver: IReceiver, | ||
workerImplementation: TWorkerImplementation<T>, | ||
isSupportingTransferables: Promise<boolean> | ||
workerImplementation: TWorkerImplementation<T> | ||
) => { | ||
@@ -39,3 +38,3 @@ return async ({ data: { id, method, params } }: IBrokerEvent<T>) => { | ||
receiver.postMessage({ id, result }, (await isSupportingTransferables) ? transferables : [ ]); | ||
receiver.postMessage({ id, result }, transferables); | ||
} | ||
@@ -42,0 +41,0 @@ } catch (err) { |
@@ -5,2 +5,3 @@ import { generateUniqueNumber } from 'fast-unique-numbers'; | ||
import { renderUnknownPortIdError } from './error-renderers'; | ||
import { isSupportingTransferables } from './is-supporting-transferables'; | ||
@@ -11,3 +12,4 @@ const DESTROY_WORKER_FUNCTIONS: Map<number, TDestroyWorkerFunction> = new Map(); | ||
createWorker: (receiver: IReceiver, workerImplementation: TWorkerImplementation<T>) => TDestroyWorkerFunction, | ||
partialWorkerImplementation: TWorkerImplementation<T> | ||
partialWorkerImplementation: TWorkerImplementation<T>, | ||
isSupportedFunction: () => boolean | Promise<boolean> | ||
): TWorkerImplementation<T & IDefaultWorkerDefinition> => { | ||
@@ -41,4 +43,17 @@ return <TWorkerImplementation<T & IDefaultWorkerDefinition>> { | ||
return { result: null }; | ||
}, | ||
// @todo It should be okay to define a function without a parameter if it doesn't need one. | ||
isSupported: async (_) => { | ||
const isSelfSupported = await isSupportingTransferables(); | ||
if (isSelfSupported) { | ||
const result = isSupportedFunction(); | ||
const synchronousResult = (result instanceof Promise) ? await result : result; | ||
return { result: synchronousResult }; | ||
} | ||
return { result: false }; | ||
} | ||
}; | ||
}; |
// Bug #1: Safari does currently not support to use transferables. | ||
export const isSupportingTransferables = new Promise<boolean>((resolve) => { | ||
export const isSupportingTransferables = () => new Promise<boolean>((resolve) => { | ||
const arrayBuffer = new ArrayBuffer(0); | ||
@@ -5,0 +5,0 @@ const { port1, port2 } = new MessageChannel(); |
@@ -37,2 +37,12 @@ import { IWorkerDefinition } from './worker-definition'; | ||
isSupported: { | ||
response: { | ||
result: boolean; | ||
}; | ||
}; | ||
} |
@@ -12,6 +12,7 @@ import { createMessageHandler } from './helpers/create-message-handler'; | ||
receiver: IReceiver, | ||
workerImplementation: TWorkerImplementation<T> | ||
workerImplementation: TWorkerImplementation<T>, | ||
isSupportedFunction: () => boolean | Promise<boolean> = () => true | ||
): TDestroyWorkerFunction => { | ||
const fullWorkerImplementation = extendWorkerImplementation<T>(createWorker, workerImplementation); | ||
const messageHandler = createMessageHandler(receiver, fullWorkerImplementation, isSupportingTransferables); | ||
const fullWorkerImplementation = extendWorkerImplementation<T>(createWorker, workerImplementation, isSupportedFunction); | ||
const messageHandler = createMessageHandler(receiver, fullWorkerImplementation); | ||
@@ -22,1 +23,3 @@ receiver.addEventListener('message', messageHandler); | ||
}; | ||
export { isSupportingTransferables as isSupported }; |
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
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
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
Sorry, the diff of this file is not supported yet
84030
914