Skip to content

Commit

Permalink
Added missing cache
Browse files Browse the repository at this point in the history
  • Loading branch information
peacefulotter committed Feb 3, 2024
1 parent 6c00b3c commit 2d0ab0d
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions discojs/discojs-web/src/dataset/data_loader/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
export class Deferred<T> {
promise: Promise<T> = new Promise<T>(() => {})

Check failure on line 2 in discojs/discojs-web/src/dataset/data_loader/cache.ts

View workflow job for this annotation

GitHub Actions / lint-lib-web

Expected indentation of 2 spaces but found 4
resolve: (value: T | PromiseLike<T>) => void = () => {}

Check failure on line 3 in discojs/discojs-web/src/dataset/data_loader/cache.ts

View workflow job for this annotation

GitHub Actions / lint-lib-web

Expected indentation of 2 spaces but found 4
reject: (reason?: any) => void = () => {}

Check failure on line 4 in discojs/discojs-web/src/dataset/data_loader/cache.ts

View workflow job for this annotation

GitHub Actions / lint-lib-web

Expected indentation of 2 spaces but found 4

constructor() {

Check failure on line 6 in discojs/discojs-web/src/dataset/data_loader/cache.ts

View workflow job for this annotation

GitHub Actions / lint-lib-web

Expected indentation of 2 spaces but found 4

Check failure on line 6 in discojs/discojs-web/src/dataset/data_loader/cache.ts

View workflow job for this annotation

GitHub Actions / lint-lib-web

Missing space before function parentheses
this.reset()

Check failure on line 7 in discojs/discojs-web/src/dataset/data_loader/cache.ts

View workflow job for this annotation

GitHub Actions / lint-lib-web

Expected indentation of 4 spaces but found 8
}

Check failure on line 8 in discojs/discojs-web/src/dataset/data_loader/cache.ts

View workflow job for this annotation

GitHub Actions / lint-lib-web

Expected indentation of 2 spaces but found 4

reset() {

Check failure on line 10 in discojs/discojs-web/src/dataset/data_loader/cache.ts

View workflow job for this annotation

GitHub Actions / lint-lib-web

Expected indentation of 2 spaces but found 4

Check failure on line 10 in discojs/discojs-web/src/dataset/data_loader/cache.ts

View workflow job for this annotation

GitHub Actions / lint-lib-web

Missing return type on function

Check failure on line 10 in discojs/discojs-web/src/dataset/data_loader/cache.ts

View workflow job for this annotation

GitHub Actions / lint-lib-web

Missing space before function parentheses
this.promise = new Promise<T>((resolve, reject) => {
this.resolve = resolve
this.reject = reject
})
}
}

export class Cache<E> {
position: number = 0
private readonly cache: Deferred<E>[]

private constructor(
readonly length: number,
private readonly request: (
pos: number,
init?: boolean
) => void | Promise<void>
) {
this.cache = Array.from({ length }, () => new Deferred<E>())
}

// pre-loads the cache with the first n requests
static async init<E>(
length: number,
request: (pos: number, init?: boolean) => void | Promise<void>,
initializer: (c: Cache<E>) => void
): Promise<Cache<E>> {
const cache = new Cache<E>(length, request)
initializer(cache)
for (let pos = 0; pos < length; pos++) {
cache.request(pos, true)
}
return cache
}

put(pos: number, elt: E): void {
const promise = this.cache[pos] as Deferred<E>
promise.resolve(elt)
}

async next(): Promise<E> {
const eltOrDeffered = this.cache[this.position]
const elt = await eltOrDeffered.promise
const pos = this.position
this.cache[pos] = new Deferred<E>()
this.request(pos)
this.position = (pos + 1) % this.length
return elt
}
}

0 comments on commit 2d0ab0d

Please sign in to comment.