Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

1.3 KB
Raw Blame
// 单击判定与数据预取并行,取消后不再应用异步结果。
export class ParentDocClick {
    private pending: {row: HTMLElement, timer: ReturnType<typeof setTimeout>, waiting: boolean} | undefined;

    public cancel() {
        if (this.pending) {
            clearTimeout(this.pending.timer);
            this.pending = undefined;
        }
    }

    public click(row: HTMLElement, prepare: () => Promise<() => void>, open: () => void) {
        if (this.pending?.row === row && this.pending.waiting) {
            this.cancel();
            open();
            return;
        }
        this.cancel();
        const pending = {row, timer: undefined as ReturnType<typeof setTimeout>, waiting: true};
        this.pending = pending;
        let apply: () => void;
        const finish = () => {
            if (this.pending !== pending || pending.waiting || !apply) { return; }
            this.pending = undefined;
            if (row.isConnected) { apply(); }
        };
        pending.timer = setTimeout(() => {
            pending.waiting = false;
            finish();
        }, 300);
        prepare().then(result => {
            apply = result;
            finish();
        }).catch(error => {
            if (this.pending === pending) { this.cancel(); }
            console.warn("Failed to prepare document children", error);
        });
    }
}