UNPKG

quadstore

Version:

Quadstore is a LevelDB-backed RDF graph database / triplestore for JavaScript runtimes (browsers, Node.js, Deno, Bun, ...) that implements the RDF/JS interfaces and supports SPARQL queries and querying across named graphs.

78 lines 2.15 kB
import { AsyncIterator } from 'asynciterator'; export class LevelIterator extends AsyncIterator { #source; #sourceEnded; #mapper; #bufsize; #nextbuf; #currbuf; #curridx; #loading; constructor(source, mapper, maxBufferSize = 256) { super(); this.#source = source; this.#sourceEnded = false; this.#mapper = mapper; this.#bufsize = maxBufferSize; this.#currbuf = null; this.#nextbuf = null; this.#curridx = 0; this.#loading = false; this.readable = false; queueMicrotask(this.#loadNextBuffer); } read() { let item = null; if (!this.#currbuf) { this.#currbuf = this.#nextbuf; this.#nextbuf = null; this.#curridx = 0; this.#loadNextBuffer(); } if (this.#currbuf) { if (this.#curridx < this.#currbuf.length) { item = this.#mapper(this.#currbuf[this.#curridx++]); } if (this.#curridx === this.#currbuf.length) { this.#currbuf = null; } } if (item === null) { this.readable = false; if (this.#sourceEnded) { this.close(); } } return item; } #loadNextBuffer = () => { if (!this.#loading && !this.#nextbuf) { this.#loading = true; this.#source.nextv(this.#bufsize) .then(this.#onNextBuffer) .catch(this.#onNextBufferError); } }; #onNextBuffer = (entries) => { this.#loading = false; if (entries.length) { this.readable = true; this.#nextbuf = entries; } else { this.#nextbuf = null; this.#sourceEnded = true; if (!this.#currbuf) { this.close(); } } }; #onNextBufferError = (err) => { this.#loading = false; this.#sourceEnded = true; if (!this.#currbuf) { this.close(); } }; } //# sourceMappingURL=leveliterator.js.map