// All material copyright ESRI, All Rights Reserved, unless otherwise specified.
// See http://js.arcgis.com/3.23/esri/copyright.txt for details.
//>>built
define("esri/layers/support/ParallelSnapshot","dojo/_base/declare dojo/_base/lang dojo/_base/array dojo/has dojo/Deferred ../../kernel".split(" "),function(g,h,e,k,l,m){g=g(null,{declaredClass:"esri.layers.support.ParallelSnapshot",layer:null,mode:null,queryTask:null,batchSize:5,_query:null,_fetchDfd:null,_startPage:null,_lastPage:null,_maxPage:null,_requests:null,constructor:function(a){h.mixin(this,a);this._requests={};this._maxPage=Math.ceil(this.mode.maxFeatures/this.layer.maxRecordCount)},destroy:function(){this.cancel()},
fetch:function(a){var b=new l;this.cancel(!0);this._query=a;this._fetchDfd=b;this._sendRequests();return b.promise},cancel:function(a){this._cancelPendingRequests(a);this._reset()},_reset:function(){this._startPage=this._lastPage=null;this._requests={}},_sendRequests:function(a){var b=this._query,c=this.queryTask,d=this._requests,e=this.layer.maxRecordCount,f;a=null==a?1:a;f=a+this.batchSize-1;f>this._maxPage&&(f=this._maxPage);if(!(a>f))for(this._startPage=a,this._lastPage=f;a<=f;a++)b.start=(a-
1)*e,b.num=e,d[a]=c.execute(b),d[a].then(h.hitch(this,this._handleSuccess,a)).otherwise(h.hitch(this,this._handleError,a))},_evalNextBatch:function(a){a===this._lastPage&&this._sendRequests(this._lastPage+1)},_handleSuccess:function(a,b){b.exceededTransferLimit||this._cancelLaterRequests(a,!0);var c=this.mode._checkMaxLimit(b.features);this._fetchDfd.progress({features:c.features,isError:!1});c.maxLimitReached?((c=c.featuresDiscarded||this._hasRejectedRequests())||(c=!!this._getResult(this._getLastSuccessfulRequest()).exceededTransferLimit),
this._resolveFetch(c)):(b.exceededTransferLimit&&this._evalNextBatch(a),this._evalResolution())},_handleError:function(a,b){var c=this._requests;c&&c[a]&&(1===a?this._rejectFetch(b):(this._evalNextBatch(a),this._fetchDfd.progress({error:b,isError:!0}),this._evalResolution()))},_evalResolution:function(){this._hasPendingRequests()||this._resolveFetch(this._hasRejectedRequests())},_resolveFetch:function(a){this.cancel(!0);this._fetchDfd.resolve({hasPartialFeatures:a})},_rejectFetch:function(a){this.cancel(!0);
this._fetchDfd.reject(a)},_getResult:function(a){return a&&a.dfd.results[0]},_hasPendingRequests:function(){return!!this._getPendingRequests().length},_getPendingRequests:function(){var a=this._requests,b,c=[];for(b in a){var d=a[b];d.isFulfilled()||c.push({pageId:Number(b),dfd:d})}return c},_getLastSuccessfulRequest:function(){var a=this._getResolvedRequests(),b=-Infinity,c,a=e.filter(a,function(a){return!!this._getResult(a).features.length||1===a.pageId},this);e.forEach(a,function(a){a.pageId>b&&
(b=a.pageId,c=a)});return c},_getResolvedRequests:function(){var a=this._requests,b,c=[];for(b in a){var d=a[b];d.isResolved()&&c.push({pageId:Number(b),dfd:d})}return c},_hasRejectedRequests:function(){var a=this._getRejectedRequests(),b=this._getLastSuccessfulRequest(),c=this._getResult(b);c&&!c.exceededTransferLimit&&(a=e.filter(a,function(a){return a.pageId<=b.pageId}));return!!a.length},_getRejectedRequests:function(){var a=this._requests,b,c=[];for(b in a){var d=a[b];d.isRejected()&&c.push({pageId:Number(b),
dfd:d})}return c},_cancelPendingRequests:function(a){this._cancelRequests(this._getPendingRequests(),a)},_cancelLaterRequests:function(a,b){var c=e.filter(this._getPendingRequests(),function(b){return b.pageId>a});this._cancelRequests(c,b)},_cancelRequests:function(a,b){var c=this.mode,d=this._requests;e.forEach(a,function(a){b&&delete d[a.pageId];c._cancelPendingRequest(a.dfd)})}});k("extend-esri")&&h.setObject("layers.support.ParallelSnapshot",g,m);return g});