All files / src pull-queue.ts

100% Statements 28/28
100% Branches 10/10
100% Functions 4/4
100% Lines 28/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 351x       1x 471x 471x   471x 15x 15x   471x 61x 7x 7x 7x 7x 54x 54x 54x 59x 54x 54x 54x 61x   471x 54x 54x 7x 7x 54x 471x  
import { Notice } from "obsidian";
import { NOTICE } from "./types";
import type { QueuedPull } from "./types";
 
export class PullQueue {
    private pulling = false;
    private queue: QueuedPull[] = [];
 
    get isPulling(): boolean {
        return this.pulling;
    }
 
    async enqueue(run: () => Promise<void>, modelName: string): Promise<void> {
        if (this.pulling) {
            this.queue.push({ run, modelName });
            new Notice(`${NOTICE.PULL_QUEUED}: ${modelName}`);
            return;
        }
        this.pulling = true;
        try {
            await run();
        } finally {
            this.pulling = false;
            await this.runNext();
        }
    }
 
    private async runNext(): Promise<void> {
        const next = this.queue.shift();
        if (next) {
            return this.enqueue(next.run, next.modelName);
        }
    }
}