Clean up email reading logic

This commit is contained in:
2024-12-30 22:37:16 -05:00
parent 063809e446
commit 9f750bc2eb
4 changed files with 125 additions and 221 deletions

View File

@@ -1,139 +0,0 @@
import {type ChunkCallback, Iterable, type MaybeIterationItem} from "./Iterable.ts";
import {collect, type Collection} from "./Collection.ts";
import {type Either, isLeft, isRight, left, type Maybe, right, unright} from "../types.ts";
export class AsyncGeneratorIterable<T> extends Iterable<T> {
private sourceIndex: number = -1
private cacheStartIndex: number = 0
private cache: T[] = []
private source?: AsyncGenerator<T, unknown, unknown>
constructor(
private sourceFactory: () => AsyncGenerator<T, unknown, unknown>,
private maxCacheSize: number = 100,
) {
super()
}
private computeCacheIndex(realIndex: number): Either<null, number> {
let i = realIndex - this.cacheStartIndex
console.log('agi cci', { i, realIndex, cSI: this.cacheStartIndex, cl: this.cache.length})
if ( i >= this.cache.length ) {
return left(null)
}
return right(i)
}
private async advanceIndexInCache(realIndex: number): Promise<void> {
if ( isRight(this.computeCacheIndex(realIndex)) ) {
return
}
console.log('aIIC needs advance')
if ( realIndex < this.cacheStartIndex ) {
this.source = undefined
}
if ( !this.source ) {
this.source = this.sourceFactory()
this.sourceIndex = -1
}
for await ( const item of this.source ) {
console.log('aIIC source item', item, this.sourceIndex, realIndex)
this.sourceIndex += 1
this.cache.push(item)
if ( this.cache.length >= this.maxCacheSize ) {
this.cache.shift()
this.cacheStartIndex += 1
}
if ( this.sourceIndex >= realIndex ) {
console.log('aIIC break')
break
}
}
}
async at(i: number): Promise<Maybe<T>> {
await this.advanceIndexInCache(i)
const cacheIndex = this.computeCacheIndex(i)
console.log('agi at', { i, cacheIndex })
if ( isRight(cacheIndex) ) {
return this.cache[unright(cacheIndex)]
}
return undefined
}
clone(): AsyncGeneratorIterable<T> {
return new AsyncGeneratorIterable(this.sourceFactory, this.maxCacheSize)
}
count(): Promise<number> {
throw new Error('cannot count!')
}
async range(start: number, end: number): Promise<Collection<T>> {
const c: Collection<T> = collect()
for ( let i = start; i <= end; i += 1 ) {
const item = await this.at(i)
if ( !item ) {
break
}
c.push(item)
}
return c
}
async all(): Promise<Collection<T>> {
const c: Collection<T> = collect()
let i = -1
while ( true ) {
i += 1
const item = await this.at(i)
if ( !item ) {
break
}
c.push(item)
}
return c
}
async next(): Promise<MaybeIterationItem<T>> {
const value = await this.at(this.index)
if ( !value ) {
return { done: true }
}
this.index += 1
return { done: false, value }
}
async seek(index: number): Promise<void> {
if ( index < 0 ) {
throw new TypeError('Cannot seek to negative index.')
}
await this.advanceIndexInCache(index)
const cacheIndex = this.computeCacheIndex(index)
if ( isLeft(cacheIndex) ) {
throw new TypeError('Cannot seek past last item.')
}
this.index = index
}
async peek(): Promise<Maybe<T>> {
return this.at(this.index + 1)
}
}

View File

@@ -48,7 +48,7 @@ export abstract class Iterable<T> {
* @return Promise<Collection>
*/
public async all(): Promise<Collection<T>> {
return this.range(0, (await this.count()) + 1)
return this.range(0, (await this.count()) - 1)
}
/**