Last active
September 16, 2023 13:41
-
-
Save zerkalica/12268658470d30025bdb8f201741554a to your computer and use it in GitHub Desktop.
chunked range
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace $ { | |
export class $gd_kit_range<Item> extends $mol_object { | |
@ $mol_mem | |
protected server_count(next?: number | null): number { | |
throw new Error('implement') | |
} | |
@ $mol_mem_key | |
protected server_chunk(offset: number, next?: null): readonly Item[] { | |
throw new Error('implement') | |
} | |
limit() { | |
return 100 | |
} | |
@ $mol_mem | |
protected temp(next?: null | readonly string[]) { | |
return next | |
} | |
@ $mol_action | |
temp_push(...ids: readonly string[]) { | |
this.temp([ ...this.temp() ?? [], ...ids ]) | |
this.temp_cut() | |
} | |
temp_chunk_min() { | |
return 1 | |
} | |
temp_cut() { | |
const temp = this.temp() | |
if (! temp) return | |
const limit = this.limit() | |
const delete_chunks = Math.ceil(temp.length / limit) - this.temp_chunk_min() | |
if (delete_chunks <= 0) return | |
const delete_count = delete_chunks * limit | |
this.server_count(this.server_count() + delete_count) | |
this.temp(temp.slice(0, delete_count)) | |
} | |
protected server_item(index: number, reset?: null) { | |
const limit = this.limit() | |
const offset = Math.floor(index / limit) * limit | |
const chunk_index = index % limit | |
const chunk = this.server_chunk(offset, reset) | |
const id = chunk[chunk_index] | |
return id | |
} | |
protected item(index: number, reset?: null) { | |
const count = this.server_count() | |
const temp = this.temp() | |
if (index < count) return this.server_item(index, reset) | |
return temp?.[index - count]! | |
} | |
protected count() { | |
return this.server_count() + ( this.temp()?.length ?? 0) | |
} | |
@ $mol_mem | |
range() { | |
return this.$.$mol_range2(index => this.item(index), () => this.count()) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment