/******/ // The require scope
/******/ var __webpack_require__ = {};
/******/ 
/************************************************************************/
/******/ /* webpack/runtime/define property getters */
/******/ (() => {
/******/ 	// define getter functions for harmony exports
/******/ 	__webpack_require__.d = (exports, definition) => {
/******/ 		for(var key in definition) {
/******/ 			if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ 				Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ 			}
/******/ 		}
/******/ 	};
/******/ })();
/******/ 
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ (() => {
/******/ 	__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ })();
/******/ 
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ 	// define __esModule on exports
/******/ 	__webpack_require__.r = (exports) => {
/******/ 		if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ 			Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ 		}
/******/ 		Object.defineProperty(exports, '__esModule', { value: true });
/******/ 	};
/******/ })();
/******/ 
/************************************************************************/
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   connectWorker: () => (/* binding */ connectWorker)
/* harmony export */ });
/**
 * Establishes a specific connection protocol between the callee (worker) and external
 * caller (which created a worker via `new Worker(...)` constructor).
 *
 * The protocol assumes the worker exposes an RPC-like interface via a `class` where
 * every public method returns a `Promise`. This interface is transparently mapped
 * from the caller to the worker via messages.
 *
 * **Example**:
 * ```ts
 * // calc-worker.ts
 * class Calculator {
 *     constructor(precision: number) { ... }
 *     add(a: number, b: number): Promise<number> { ... }
 * }
 *
 * connectWorker(Calculator);
 *
 * // component.ts
 * const calcWorker = defineWorker(() => new Worker('./calc-worker.js'), [0.1]);
 * ...
 * function Component() {
 *     const calc = useWorker(calcWorker);
 *     ...
 *     const result = await calc.add(2, 3);
 * }
 * ```
 */
function connectWorker(factory) {
    let handler;
    onmessage = async (e) => {
        const message = e.data;
        if (message.type === 'call') {
            let response;
            try {
                if (handler) {
                    const result = await handler[message.method](...message.args);
                    response = {
                        type: 'success',
                        id: message.id,
                        result,
                    };
                }
                else {
                    if (message.method !== 'constructor') {
                        throw new Error('Cannot call worker method without initializing it first');
                    }
                    handler = new factory(...message.args);
                    response = {
                        type: 'success',
                        id: message.id,
                        result: undefined,
                    };
                }
            }
            catch (err) {
                response = {
                    type: 'error',
                    id: message.id,
                    error: err,
                };
            }
            postMessage(response);
        }
    };
}

var __webpack_exports__connectWorker = __webpack_exports__.connectWorker;
export { __webpack_exports__connectWorker as connectWorker };

//# sourceMappingURL=worker-protocol.js.map