UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 3.55 kB
import{unwrapOrThrow as t}from"../../../core/maybe.js";class e{constructor(t,e){this._lastId=-1,this._progress=e,this._parent=t}reset(){this._lastId=-1}nextBatch(e){if(null!==this._parent._mainSetInUse)return this._parent._mainSetInUse.then((t=>this.nextBatch(e)),(t=>this.nextBatch(e)));const n={returnpromise:null,hasset:!1},s=[];return n.returnpromise=new Promise(((a,i)=>{this._parent._getSet(this._progress).then((r=>{const h=t(r._known,"known");let _=h.length-1;if("GETPAGES"===h[h.length-1]&&(_-=1),this._lastId+e>_&&h.length>0&&"GETPAGES"===h[h.length-1])return void this._parent._expandPagedSet(r,this._parent._maxQueryRate(),0,0,this._progress).then((t=>{n.hasset=!0,this._parent._mainSetInUse=null,this.nextBatch(e).then(a,i)}),(t=>{n.hasset=!0,this._parent._mainSetInUse=null,i(t)}));const l=t(r._candidates,"candidates");if(_>=this._lastId+e||0===l.length){for(let t=0;t<e;t++){const e=t+this._lastId+1;if(e>=h.length)break;s[t]=h[e]}return this._lastId+=s.length,0===s.length&&(n.hasset=!0,this._parent._mainSetInUse=null,a([])),void this._parent._getFeatureBatch(s,this._progress).then((t=>{n.hasset=!0,this._parent._mainSetInUse=null,a(t)}),(t=>{n.hasset=!0,this._parent._mainSetInUse=null,i(t)}))}this._parent._refineSetBlock(r,this._parent._maxProcessingRate(),this._progress).then((()=>{n.hasset=!0,this._parent._mainSetInUse=null,this.nextBatch(e).then(a,i)}),(t=>{n.hasset=!0,this._parent._mainSetInUse=null,i(t)}))}),(t=>{n.hasset=!0,this._parent._mainSetInUse=null,i(t)}))})),!1===n.hasset&&(this._parent._mainSetInUse=n.returnpromise,n.hasset=!0),n.returnpromise}next(){if(null!==this._parent._mainSetInUse)return this._parent._mainSetInUse.then((t=>this.next()),(t=>this.next()));const e={returnpromise:null,hasset:!1};return e.returnpromise=new Promise(((n,s)=>{this._parent._getSet(this._progress).then((a=>{const i=t(a._known,"known");if(this._lastId<i.length-1)"GETPAGES"===i[this._lastId+1]?this._parent._expandPagedSet(a,this._parent._maxQueryRate(),0,0,this._progress).then((t=>(e.hasset=!0,this._parent._mainSetInUse=null,this.next()))).then(n,s):(this._lastId+=1,this._parent._getFeature(a,i[this._lastId],this._progress).then((t=>{e.hasset=!0,this._parent._mainSetInUse=null,n(t)}),(t=>{e.hasset=!0,this._parent._mainSetInUse=null,s(t)})));else{t(a._candidates,"candidates").length>0?this._parent._refineSetBlock(a,this._parent._maxProcessingRate(),this._progress).then((()=>{e.hasset=!0,this._parent._mainSetInUse=null,this.next().then(n,s)}),(t=>{e.hasset=!0,this._parent._mainSetInUse=null,s(t)})):(e.hasset=!0,this._parent._mainSetInUse=null,n(null))}}),(t=>{e.hasset=!0,this._parent._mainSetInUse=null,s(t)}))})),!1===e.hasset&&(this._parent._mainSetInUse=e.returnpromise,e.hasset=!0),e.returnpromise}async count(){if(-1!==this._parent._totalCount)return this._parent._totalCount;const t=await this._parent._getSet(this._progress),e=await this._refineAllSets(t);return this._parent._totalCount=e._known.length,this._parent._totalCount}async _refineAllSets(t){if(t._known.length>0&&"GETPAGES"===t._known[t._known.length-1])return await this._parent._expandPagedSet(t,this._parent._maxQueryRate(),0,1,this._progress),this._refineAllSets(t);if(t._candidates.length>0){if("GETPAGES"===t._known[t._candidates.length-1])return await this._parent._expandPagedSet(t,this._parent._maxQueryRate(),0,2,this._progress),this._refineAllSets(t);const e=await this._parent._refineSetBlock(t,this._parent._maxProcessingRate(),this._progress);return e._candidates.length>0?this._refineAllSets(e):e}return t}}export{e as default};