/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import has from"../has.js";import{isAbortError as t}from"../promiseUtils.js";const r="worker:port-closed",e={HANDSHAKE:0,OPEN:1,OPENED:2,RESPONSE:3,INVOKE:4,ABORT:5,CLOSE:6,OPEN_PORT:7,ON:8};let n=0;function s(){return n++}function i(t){return t&&"object"==typeof t&&("result"in t||"transferList"in t)}function o(t){return t?"string"==typeof t?JSON.stringify({name:"message",message:t}):t.toJSON?JSON.stringify(t):JSON.stringify({name:t.name,message:t.message,details:t.details||{stack:t.stack}}):null}function a(t,r,n,s){if(r.type===e.OPEN_PORT)return void t.postMessage(r,[r.port]);if(r.type!==e.INVOKE&&r.type!==e.RESPONSE)return void t.postMessage(r);let o;i(n)?(o=u(n.transferList),r.data=n.result):(o=u(s),r.data=n),o?t.postMessage(r,o):t.postMessage(r)}function f(t){if(!t)return null;const r=t.data;return r?"string"==typeof r?JSON.parse(r):r:null}function u(t){if(!t?.length)return null;if(has("esri-workers-arraybuffer-transfer"))return t;const r=t.filter(t=>!c(t));return r.length?r:null}function c(t){return t instanceof ArrayBuffer||"ArrayBuffer"===t?.constructor?.name}async function l(e){try{return await e}catch(n){const e=n?.name===r;if(!(t(n)||e))throw n;return}}export{e as MessageType,l as ignoreConnectionErrors,i as isTransferableResult,s as newJobId,r as portClosedErrorName,a as postMessage,f as receiveMessage,o as toInvokeError};
