Last active
August 24, 2023 08:33
-
-
Save zerkalica/9c01fc871f8b4fc5f947ad5399088dbe to your computer and use it in GitHub Desktop.
mol-range2-gql
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
/** | |
return $gd_rad_transport_query.make({ | |
query: $mol_const(query), | |
assert: $mol_data_string, | |
}) | |
*/ | |
class $gd_rad_transport_query<Item> extends $mol_object { | |
query() { | |
return '' | |
} | |
limit() { | |
return 100 | |
} | |
assert(raw: Item) { | |
return raw as Item | |
} | |
@ $mol_mem | |
array_asserter() { | |
return $mol_data_array(raw => this.assert(raw)) | |
} | |
@ $mol_mem_key | |
fetch_data(offset: number) { | |
const raw = this.$.$gd_rad_transport.response({ | |
path: `${this.query()}&limit=${this.limit()}&offset=${offset}`, | |
}).json() as any[] | |
return this.array_asserter()(raw) | |
} | |
count() { | |
return Number.POSITIVE_INFINITY as number | undefined | |
} | |
@ $mol_mem | |
fetch_count() { | |
const res = this.$.$gd_rad_transport.response({ | |
method: 'HEAD', | |
headers: { | |
'Range-Unit': 'items', | |
Prefer: 'count-exact', | |
}, | |
path: `${this.query()}`, | |
}) | |
const headers = res.headers() | |
const range_str = headers.get('Content-Range') | |
const count = Number(range_str?.replace(/\/(\d+)$/, '$1')) | |
if (isNaN(count)) return 0 | |
return count | |
} | |
@ $mol_mem | |
range() { | |
return this.$.$mol_range2( | |
index => this.fetch_data(Math.floor(index / this.limit()) * this.limit())[index % this.limit()], | |
() => this.count() ?? this.fetch_count() | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment