/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../chunks/_rollupPluginBabelHelpers","../arrayUtils","../handleUtils","../Logger","../maybe","../promiseUtils","./workers"],(function(e,t,i,r,s,n,o,h){"use strict";const a=s.getLogger("esri.core.workers.WorkerHandle");let l=function(){function e(e,t,i,r,s={}){this._mainMethod=t,this._transferLists=i,this._listeners=[],this._promise=h.open(e,{...s,schedule:r}).then((e=>{if(void 0===this._thread){this._thread=e,this._promise=null,s.hasInitialize&&this.broadcast({},"initialize");for(const e of this._listeners)this._connectListener(e)}else e.close()})),this._promise.catch((t=>a.error(`Failed to initialize ${e} worker: ${t}`)))}var s=e.prototype;return s.on=function(e,t){const s={removed:!1,eventName:e,callback:t,threadHandle:null};return this._listeners.push(s),this._connectListener(s),r.makeHandle((()=>{s.removed=!0,i.remove(this._listeners,s),this._thread&&n.isSome(s.threadHandle)&&s.threadHandle.remove()}))},s.destroy=function(){this._thread&&(this._thread.close(),this._thread=null),this._promise=null},s.invoke=function(e,t){return this.invokeMethod(this._mainMethod,e,t)},s.invokeMethod=function(e,t,i){if(this._thread){const r=this._transferLists[e],s=r?r(t):[];return this._thread.invoke(e,t,{transferList:s,signal:i})}return this._promise?this._promise.then((()=>(o.throwIfAborted(i),this.invokeMethod(e,t,i)))):Promise.reject(null)},s.broadcast=function(e,t){return this._thread?Promise.all(this._thread.broadcast(t,e)).then((()=>{})):this._promise?this._promise.then((()=>this.broadcast(e,t))):Promise.reject()},s._connectListener=function(e){this._thread&&this._thread.on(e.eventName,e.callback).then((t=>{e.removed||(e.threadHandle=t)}))},t._createClass(e,[{key:"promise",get:function(){return this._promise}}]),e}();e.WorkerHandle=l,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
