diff --git a/lib/hooks/actions-queue.js b/lib/hooks/actions-queue.js index 6d49f1bcd..0e327dd70 100644 --- a/lib/hooks/actions-queue.js +++ b/lib/hooks/actions-queue.js @@ -1,44 +1,45 @@ // @flow import * as React from 'react'; type MessageQueueHook = { +enqueue: (items: $ReadOnlyArray) => void, }; function useActionsQueue( performAction: (item: T) => mixed | Promise, + canProcess: boolean = true, ): MessageQueueHook { const [queue, setQueue] = React.useState<$ReadOnlyArray>([]); const isProcessing = React.useRef(false); const process = React.useCallback( async (action: T) => { isProcessing.current = true; try { await performAction(action); } finally { isProcessing.current = false; setQueue(currentQueue => currentQueue.slice(1)); } }, [performAction], ); const enqueue = React.useCallback( (items: $ReadOnlyArray) => setQueue(prevQueue => [...prevQueue, ...items]), [], ); React.useEffect(() => { - if (isProcessing.current || queue.length === 0) { + if (isProcessing.current || queue.length === 0 || !canProcess) { return; } void process(queue[0]); - }, [process, queue]); + }, [process, queue, canProcess]); return { enqueue }; } export { useActionsQueue };