2022-02-25 18:37:15 +00:00
|
|
|
// Copyright 2021-2022 Signal Messenger, LLC
|
2021-01-29 22:16:48 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-12-22 03:07:45 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { render, unmountComponentAtNode } from 'react-dom';
|
|
|
|
|
2022-11-22 18:43:43 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2022-12-22 03:07:45 +00:00
|
|
|
import * as log from '../logging/log';
|
|
|
|
import { ProgressModal } from '../components/ProgressModal';
|
2022-02-25 18:37:15 +00:00
|
|
|
import { clearTimeoutIfNecessary } from './clearTimeoutIfNecessary';
|
2021-09-17 18:27:53 +00:00
|
|
|
|
2021-01-29 22:16:48 +00:00
|
|
|
export async function longRunningTaskWrapper<T>({
|
|
|
|
name,
|
|
|
|
idForLogging,
|
|
|
|
task,
|
|
|
|
suppressErrorDialog,
|
|
|
|
}: {
|
|
|
|
name: string;
|
|
|
|
idForLogging: string;
|
|
|
|
task: () => Promise<T>;
|
|
|
|
suppressErrorDialog?: boolean;
|
|
|
|
}): Promise<T> {
|
|
|
|
const idLog = `${name}/${idForLogging}`;
|
|
|
|
const ONE_SECOND = 1000;
|
|
|
|
const TWO_SECONDS = 2000;
|
|
|
|
|
2022-12-22 03:07:45 +00:00
|
|
|
let progressNode: HTMLDivElement | undefined;
|
2021-01-29 22:16:48 +00:00
|
|
|
let spinnerStart;
|
|
|
|
let progressTimeout: NodeJS.Timeout | undefined = setTimeout(() => {
|
2022-12-22 03:07:45 +00:00
|
|
|
progressNode = document.createElement('div');
|
2021-01-29 22:16:48 +00:00
|
|
|
|
2022-12-22 03:07:45 +00:00
|
|
|
log.info(`longRunningTaskWrapper/${idLog}: Creating spinner`);
|
|
|
|
render(<ProgressModal i18n={window.i18n} />, progressNode);
|
2021-01-29 22:16:48 +00:00
|
|
|
spinnerStart = Date.now();
|
|
|
|
}, TWO_SECONDS);
|
|
|
|
|
|
|
|
// Note: any task we put here needs to have its own safety valve; this function will
|
|
|
|
// show a spinner until it's done
|
|
|
|
try {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`longRunningTaskWrapper/${idLog}: Starting task`);
|
2021-01-29 22:16:48 +00:00
|
|
|
const result = await task();
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`longRunningTaskWrapper/${idLog}: Task completed successfully`);
|
2021-01-29 22:16:48 +00:00
|
|
|
|
2022-02-25 18:37:15 +00:00
|
|
|
clearTimeoutIfNecessary(progressTimeout);
|
|
|
|
progressTimeout = undefined;
|
2022-12-22 03:07:45 +00:00
|
|
|
if (progressNode) {
|
2021-01-29 22:16:48 +00:00
|
|
|
const now = Date.now();
|
|
|
|
if (spinnerStart && now - spinnerStart < ONE_SECOND) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-01-29 22:16:48 +00:00
|
|
|
`longRunningTaskWrapper/${idLog}: Spinner shown for less than second, showing for another second`
|
|
|
|
);
|
|
|
|
await window.Signal.Util.sleep(ONE_SECOND);
|
|
|
|
}
|
2022-12-22 03:07:45 +00:00
|
|
|
unmountComponentAtNode(progressNode);
|
|
|
|
progressNode = undefined;
|
2021-01-29 22:16:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
} catch (error) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-01-29 22:16:48 +00:00
|
|
|
`longRunningTaskWrapper/${idLog}: Error!`,
|
2022-11-22 18:43:43 +00:00
|
|
|
Errors.toLogFormat(error)
|
2021-01-29 22:16:48 +00:00
|
|
|
);
|
|
|
|
|
2022-02-25 18:37:15 +00:00
|
|
|
clearTimeoutIfNecessary(progressTimeout);
|
|
|
|
progressTimeout = undefined;
|
2022-12-22 03:07:45 +00:00
|
|
|
if (progressNode) {
|
|
|
|
unmountComponentAtNode(progressNode);
|
|
|
|
progressNode = undefined;
|
2021-01-29 22:16:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!suppressErrorDialog) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`longRunningTaskWrapper/${idLog}: Showing error dialog`);
|
2022-12-22 03:07:45 +00:00
|
|
|
window.reduxActions.globalModals.showErrorModal({});
|
2021-01-29 22:16:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|