import { useRef } from "react";

/**
 * Нужно, чтобы избежать состояния гонки
 */
export function useQueue<T>() {
    const queue = useRef<T[]>([]);

    const inProcess = (task: T): boolean => {
        return queue.current.includes(task);
    }

    const addToQueue = (task: T) => {
        queue.current.push(task);
    }

    const removeFromQueue = (task: T) => {
        queue.current = queue.current.filter(t => t !== task);
    }

    return {
        inProcess,
        addToQueue,
        removeFromQueue,
        queue
    }
}