????

Your IP : 216.73.216.216


Current Path : C:/inetpub/vhost/binhdinhinvest.gdtvietnam.com/www/esri/core/workers/
Upload File :
Current File : C:/inetpub/vhost/binhdinhinvest.gdtvietnam.com/www/esri/core/workers/RemoteClient.js

// COPYRIGHT © 2020 Esri
//
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
//
// This material is licensed for use under the Esri Master License
// Agreement (MLA), and is bound by the terms of that agreement.
// You may redistribute and use this code without modification,
// provided you adhere to the terms of the MLA and include this
// copyright notice.
//
// See use restrictions at http://www.esri.com/legal/pdfs/mla_e204_e300/english
//
// For additional information, contact:
// Environmental Systems Research Institute, Inc.
// Attn: Contracts and Legal Services Department
// 380 New York Street
// Redlands, California, USA 92373
// USA
//
// email: contracts@esri.com
//
// See http://js.arcgis.com/4.16/esri/copyright.txt for details.

define(["require","exports","../Error","../events","../promiseUtils","./registry","./utils","../../views/support/Scheduler"],(function(e,t,s,o,n,r,i,a){var p=i.MessageType.CLOSE,h=i.MessageType.ABORT,l=i.MessageType.INVOKE,c=i.MessageType.RESPONSE,u=i.MessageType.OPEN_PORT,_=i.MessageType.ON,f=function(){function e(e){this._timer=null,this._cancelledJobIds=new Set,this._invokeMessages=[],this._invoke=e,this._timer=null,this._process=this._process.bind(this)}return e.prototype.push=function(e){e.type===i.MessageType.ABORT?this._cancelledJobIds.add(e.jobId):(this._invokeMessages.push(e),null===this._timer&&(this._timer=setTimeout(this._process,0)))},e.prototype.clear=function(){this._invokeMessages.length=0,this._cancelledJobIds.clear(),this._timer=null},e.prototype._process=function(){this._timer=null;for(var e=0,t=this._invokeMessages;e<t.length;e++){var s=t[e];this._cancelledJobIds.has(s.jobId)||this._invoke(s)}this._cancelledJobIds.clear(),this._invokeMessages.length=0},e}();return function(){function e(e,t){var s=this;this._port=e,this._outJobs=new Map,this._inJobs=new Map,this._invokeQueue=new f((function(e){return s._onInvokeMessage(e)})),this._responseQueue=[],this._client=t.client,this._onMessage=this._onMessage.bind(this),this._channel=t.channel,t.scheduler&&(this._frameTask=t.scheduler.registerTask(a.Task.REMOTE_CLIENT,(function(e){return s._update(e)}),(function(){return s._responseQueue.length>0}))),this._port.addEventListener("message",this._onMessage),this._port.start()}return e.connect=function(t){var s,o=new MessageChannel;s="function"==typeof t?new t:"default"in t&&"function"==typeof t.default?new t.default:t;var n=new e(o.port1,{channel:o,client:s});return"object"==typeof s&&"remoteClient"in s&&(s.remoteClient=n),e.clients.set(n,s),o.port2},e.loadWorker=function(e){var t=r.registry[e];return t?t():n.resolve(null)},e.prototype.close=function(){this._post({type:p}),this._close()},e.prototype.isBusy=function(){return this._outJobs.size>0},e.prototype.invoke=function(e,t,o){var r=this,a=o&&o.signal,p=o&&o.transferList;if(!this._port)return n.reject(new s("remote-client:port-closed","Can't invoke(), port is closed"));var c=i.newJobId();return n.create((function(s,o){n.onAbortOrThrow(a,(function(){r._outJobs.get(c)&&(r._outJobs.delete(c),r._post({type:h,jobId:c}),o(n.createAbortError()))}));var i={resolve:s,reject:o};r._outJobs.set(c,i),r._post({type:l,jobId:c,methodName:e,abortable:null!=a},t,p)}))},e.prototype.on=function(e,t){var s=new MessageChannel;function o(e){t(e.data)}return this._port.postMessage({type:i.MessageType.ON,eventType:e,port:s.port2},[s.port2]),s.port1.addEventListener("message",o),s.port1.start(),{remove:function(){s.port1.postMessage({type:i.MessageType.CLOSE}),s.port1.close(),s.port1.removeEventListener("message",o)}}},e.prototype.openPort=function(){var e=new MessageChannel;return this._post({type:u,port:e.port2}),e.port1},e.prototype._close=function(){this._channel&&(this._channel=null),this._port.removeEventListener("message",this._onMessage),this._port.close(),this._outJobs.forEach((function(e){return e.reject(n.createAbortError("Abort job: worker closing"))})),this._inJobs.clear(),this._outJobs.clear(),this._invokeQueue.clear(),this._port=this._client=null,this._frameTask&&this._frameTask.remove(),this._frameTask=null,this._responseQueue=null},e.prototype._onMessage=function(e){var t=i.receiveMessage(e);if(t)switch(t.type){case c:this._onResponseMessage(t);break;case l:this._invokeQueue.push(t);break;case h:this._onAbortMessage(t);break;case p:this._onCloseMessage();break;case u:this._onOpenPortMessage(t);break;case _:this._onOnMessage(t)}},e.prototype._onAbortMessage=function(e){var t=this._inJobs,s=e.jobId,o=t.get(s);this._invokeQueue.push(e),o&&(o.controller&&o.controller.abort(),t.delete(s))},e.prototype._onCloseMessage=function(){var t=this._client;this._close(),t&&"destroy"in t&&e.clients.get(this)===t&&t.destroy(),e.clients.delete(this),t&&t.remoteClient&&(t.remoteClient=null)},e.prototype._onInvokeMessage=function(e){var t,s=this,o=e.methodName,r=e.jobId,a=e.data,p=e.abortable?n.createAbortController():null,h=this._inJobs,l=this._client,u=l[o];try{if(!u&&o&&-1!==o.indexOf("."))for(var _=o.split("."),f=0;f<_.length-1;f++)u=(l=l[_[f]])[_[f+1]];if("function"!=typeof u)throw new TypeError(o+" is not a function");t=u.call(l,a,{client:this,signal:p?p.signal:null})}catch(e){return void this._post({type:c,jobId:r,error:i.toInvokeError(e)})}n.isPromiseLike(t)?(h.set(r,{controller:p,promise:t}),t.then((function(e){h.has(r)&&(h.delete(r),s._post({type:c,jobId:r},e))}),(function(e){h.has(r)&&(h.delete(r),n.isAbortError(e)||s._post({type:c,jobId:r,error:i.toInvokeError(e||{message:"Error encountered at method "+o})}))}))):this._post({type:c,jobId:r},t)},e.prototype._onOpenPortMessage=function(t){new e(t.port,{client:this._client})},e.prototype._onOnMessage=function(e){var t=e.port,s=this._client.on(e.eventType,(function(e){t.postMessage(e)})),n=o.on(e.port,"message",(function(e){i.receiveMessage(e).type===i.MessageType.CLOSE&&(n.remove(),s.remove(),t.close())}))},e.prototype._onResponseMessage=function(e){this._frameTask?this._responseQueue.push(e):this._handleResponse(e)},e.prototype._update=function(e){for(;!e.done&&this._responseQueue.length>0;)this._handleResponse(this._responseQueue.shift()),e.madeProgress()},e.prototype._handleResponse=function(e){var t=e.jobId,o=e.error,n=e.data,r=this._outJobs;if(r.has(t)){var i=r.get(t);r.delete(t),o?i.reject(s.fromJSON(JSON.parse(o))):i.resolve(n)}},e.prototype._post=function(e,t,s){return i.postMessage(this._port,e,t,s)},e.clients=new Map,e}()}));