class t{constructor(t,e){this._map=new Map,this._elements=[],this._cursor=null,this._filter=null,this._abortable=t,this._onChange=e}getId(t){throw new Error("Abstract method: please implement me")}matchFilter(t){throw new Error("Abstract method: please implement me")}fetch(t,e){throw new Error("Abstract method: please implement me")}reset(){this._cursor=null,this._map.clear(),this._elements=[],this._onChange?.()}setFilter(t){this._filter=t,this.reset()}delete(t){let e=-1;return this._map.delete(t)&&(this._elements=this._elements.filter(((s,h)=>{const r=this.getId(s);return r===t&&(e=h),r!==t})),this._total--,this._onChange?.()),e}update(t){if(null==t||0===t.length)return!1;const e=[],s=new Map;return t.forEach((t=>{if(null==this._filter||this.matchFilter(t)){const h=this.getId(t);this._map.has(h)?s.set(h,t):e.push(t),this._map.set(h,t)}})),(e.length>0||s.size>0)&&(this._elements=[...e,...this._elements.map((t=>{const e=this.getId(t);return s.has(e)?{...s.get(e)}:t}))],this._total+=e.length,this._onChange?.(),!0)}getElement(t){return this._map.get(t)}get elements(){return this._elements}get total(){return this._total}hasMore(){return 0!==this._cursor}async loadMore(t=1e3){if(this.hasMore()){const e=await this._abortable.run((e=>this.fetch(t,e)));e.elements.forEach((t=>{this._map.set(this.getId(t),t)})),this._elements=Array.from(this._map.values()),this._cursor=e.cursor,this._total=e.total,this._onChange?.()}}}export{t as KvScanner};
//# sourceMappingURL=kv-scanner.js.map
