init; collecting some of my commonly used utils

This commit is contained in:
2024-04-01 17:31:33 -05:00
commit a09a7d7337
13 changed files with 322 additions and 0 deletions

40
pkg/queue.ts Normal file
View File

@@ -0,0 +1,40 @@
export default class Queue<TData> extends Array<TData> {
constructor(...items: TData[]) {
super(...items);
}
public get isEmpty() {
return this.length == 0;
}
public get isNotEmpty() {
return this.length > 0;
}
public enqueue(item: TData) {
this.push(item);
return this;
}
public dequeue() {
return this.shift();
}
public async* walk({ interval = 1000, chunkSize = 1 }) {
while (this.isNotEmpty) {
const item = this.getChunk(chunkSize);
await new Promise(resolve => setTimeout(resolve, interval));
yield item;
}
}
private getChunk(length: number) {
const chunk = [];
while (chunk.length < length && this.isNotEmpty) {
chunk.push(this.dequeue());
}
return chunk;
}
}