saleor-dashboard/src/containers/BackgroundTasks/BackgroundTasksProvider.tsx

98 lines
2.5 KiB
TypeScript
Raw Normal View History

import { IMessageContext } from "@saleor/components/messages";
import useNotifier from "@saleor/hooks/useNotifier";
import ApolloClient from "apollo-client";
2020-06-19 10:42:29 +00:00
import React from "react";
import { useApolloClient } from "react-apollo";
import { IntlShape, useIntl } from "react-intl";
2020-06-19 10:42:29 +00:00
import BackgroundTasksContext from "./context";
import { handleTask, queueCustom } from "./tasks";
2020-06-21 15:31:33 +00:00
import { QueuedTask, Task, TaskData, TaskStatus } from "./types";
2020-06-19 10:42:29 +00:00
export const backgroundTasksRefreshTime = 15 * 1000;
2020-07-06 15:06:01 +00:00
// TODO: Remove underscores when working on #575 or similar PR
export function useBackgroundTasks(
_apolloClient: ApolloClient<any>,
_notify: IMessageContext,
_intl: IntlShape
) {
2020-06-19 10:42:29 +00:00
const idCounter = React.useRef(0);
const tasks = React.useRef<QueuedTask[]>([]);
React.useEffect(() => {
const intervalId = setInterval(() => {
2020-06-21 15:31:33 +00:00
const queue = async () => {
try {
await Promise.all(
tasks.current.map(async task => {
if (task.status === TaskStatus.PENDING) {
let status: TaskStatus;
2020-06-21 15:31:33 +00:00
try {
status = await handleTask(task);
} catch (error) {
throw error;
}
if (status !== TaskStatus.PENDING) {
const taskIndex = tasks.current.findIndex(
t => t.id === task.id
);
tasks.current[taskIndex].status = status;
}
2020-06-21 15:31:33 +00:00
}
})
);
} catch (error) {
throw error;
}
};
queue();
2020-06-19 10:42:29 +00:00
}, backgroundTasksRefreshTime);
return () => clearInterval(intervalId);
});
function cancel(id: number) {
tasks.current = tasks.current.filter(task => task.id !== id);
}
function queue(type: Task, data?: TaskData) {
idCounter.current += 1;
switch (type) {
case Task.CUSTOM:
queueCustom(idCounter.current, tasks, data);
break;
2020-06-19 10:42:29 +00:00
}
return idCounter.current;
}
return {
cancel,
queue
};
}
const BackgroundTasksProvider: React.FC = ({ children }) => {
const apolloClient = useApolloClient();
const notify = useNotifier();
const intl = useIntl();
const { cancel, queue } = useBackgroundTasks(apolloClient, notify, intl);
2020-06-19 10:42:29 +00:00
return (
<BackgroundTasksContext.Provider
value={{
cancel,
queue
}}
>
{children}
</BackgroundTasksContext.Provider>
);
};
BackgroundTasksProvider.displayName = "BackgroundTasksProvider";
export default BackgroundTasksProvider;