22581b2f创建于 2025年12月16日历史提交
/*
 * -------------------------------------------------------------------------
 * This file is part of the MindStudio project.
 * Copyright (c) 2025 Huawei Technologies Co.,Ltd.
 *
 * MindStudio is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *
 *          http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 * -------------------------------------------------------------------------
 */
type Callback<T extends unknown[], K> = (...args: T) => Promise<K>;
export const customDebounce = function<T extends unknown[], K>(callback: Callback<T, K>): Callback<T, K> {
    let waitingTask: ReturnType<Callback<T, K>> | null = null;
    let curTask: Callback<T, K> | null = null;
    const taskQueue: Array<(value: K) => void> = [];
    return (...args) => {
        if (waitingTask === null) {
            waitingTask = callback(...args);
            return waitingTask;
        }
        return new Promise((resolve, reject) => {
            curTask = callback;
            taskQueue.push(resolve);
            waitingTask?.then((res) => {
                if (resolve !== taskQueue[taskQueue.length - 1]) {
                    const index = taskQueue.indexOf(resolve);
                    resolve(res);
                    taskQueue.splice(0, index + 1);
                    return;
                }
                waitingTask = curTask?.(...args) ?? null;
                waitingTask?.then((finalRes) => {
                    const index = taskQueue.indexOf(resolve);
                    resolve(finalRes);
                    taskQueue.splice(0, index + 1);
                });
            })
                .catch(e => reject(e));
        });
    };
};