Refactor to remove "Async" function name suffix

Remove convention where Async suffix is added to functions that returns
a Promise. It was a habit from C#, but is not widely used in JavaScript
/ TypeScript world, also bloats the code. The code is more consistent
with third party dependencies/frameworks without the suffix.
This commit is contained in:
undergroundwires
2021-10-30 12:35:20 +01:00
parent 799fb091b8
commit 82c43ba2e3
35 changed files with 189 additions and 187 deletions

View File

@@ -4,8 +4,8 @@ import { ProgressInfo } from 'electron-builder';
import { UpdateProgressBar } from './UpdateProgressBar';
import log from 'electron-log';
export async function handleAutoUpdateAsync() {
if (await askDownloadAndInstallAsync() === DownloadDialogResult.NotNow) {
export async function handleAutoUpdate() {
if (await askDownloadAndInstall() === DownloadDialogResult.NotNow) {
return;
}
startHandlingUpdateProgress();
@@ -29,12 +29,12 @@ function startHandlingUpdateProgress() {
autoUpdater.on('update-downloaded', async (info: UpdateInfo) => {
log.info('@update-downloaded@\n', info);
progressBar.close();
await handleUpdateDownloadedAsync();
await handleUpdateDownloaded();
});
}
async function handleUpdateDownloadedAsync() {
if (await askRestartAndInstallAsync() === InstallDialogResult.NotNow) {
async function handleUpdateDownloaded() {
if (await askRestartAndInstall() === InstallDialogResult.NotNow) {
return;
}
setTimeout(() => autoUpdater.quitAndInstall(), 1);
@@ -44,7 +44,7 @@ enum DownloadDialogResult {
Install = 0,
NotNow = 1,
}
async function askDownloadAndInstallAsync(): Promise<DownloadDialogResult> {
async function askDownloadAndInstall(): Promise<DownloadDialogResult> {
const updateDialogResult = await dialog.showMessageBox({
type: 'question',
buttons: ['Install', 'Not now' ],
@@ -61,7 +61,7 @@ enum InstallDialogResult {
InstallAndRestart = 0,
NotNow = 1,
}
async function askRestartAndInstallAsync(): Promise<InstallDialogResult> {
async function askRestartAndInstall(): Promise<InstallDialogResult> {
const installDialogResult = await dialog.showMessageBox({
type: 'question',
buttons: ['Install and restart', 'Later'],

View File

@@ -12,8 +12,8 @@ export function requiresManualUpdate(): boolean {
return process.platform === 'darwin';
}
export async function handleManualUpdateAsync(info: UpdateInfo) {
const result = await askForVisitingWebsiteForManualUpdateAsync();
export async function handleManualUpdate(info: UpdateInfo) {
const result = await askForVisitingWebsiteForManualUpdate();
if (result === ManualDownloadDialogResult.NoAction) {
return;
}
@@ -26,7 +26,7 @@ export async function handleManualUpdateAsync(info: UpdateInfo) {
if (result === ManualDownloadDialogResult.VisitReleasesPage) {
await shell.openExternal(project.releaseUrl);
} else if (result === ManualDownloadDialogResult.UpdateNow) {
await downloadAsync(info, project);
await download(info, project);
}
}
@@ -35,7 +35,7 @@ enum ManualDownloadDialogResult {
UpdateNow = 1,
VisitReleasesPage = 2,
}
async function askForVisitingWebsiteForManualUpdateAsync(): Promise<ManualDownloadDialogResult> {
async function askForVisitingWebsiteForManualUpdate(): Promise<ManualDownloadDialogResult> {
const visitPageResult = await dialog.showMessageBox({
type: 'info',
buttons: [
@@ -54,7 +54,7 @@ async function askForVisitingWebsiteForManualUpdateAsync(): Promise<ManualDownlo
return visitPageResult.response;
}
async function downloadAsync(info: UpdateInfo, project: ProjectInformation) {
async function download(info: UpdateInfo, project: ProjectInformation) {
log.info('Downloading update manually');
const progressBar = new UpdateProgressBar();
progressBar.showIndeterminateState();
@@ -69,7 +69,7 @@ async function downloadAsync(info: UpdateInfo, project: ProjectInformation) {
await fs.promises.mkdir(parentFolder, { recursive: true });
}
const dmgFileUrl = project.getDownloadUrl(OperatingSystem.macOS);
await downloadFileWithProgressAsync(dmgFileUrl, filePath,
await downloadFileWithProgress(dmgFileUrl, filePath,
(percentage) => { progressBar.showPercentage(percentage); });
await shell.openPath(filePath);
progressBar.close();
@@ -81,7 +81,7 @@ async function downloadAsync(info: UpdateInfo, project: ProjectInformation) {
type ProgressCallback = (progress: number) => void;
async function downloadFileWithProgressAsync(
async function downloadFileWithProgress(
url: string, filePath: string, progressHandler: ProgressCallback) {
// We don't download through autoUpdater as it cannot download DMG but requires distributing ZIP
log.info(`Fetching ${url}`);
@@ -100,10 +100,10 @@ async function downloadFileWithProgressAsync(
if (!reader) {
throw new Error('No response body');
}
await streamWithProgressAsync(contentLength, reader, writer, progressHandler);
await streamWithProgress(contentLength, reader, writer, progressHandler);
}
async function streamWithProgressAsync(
async function streamWithProgress(
totalLength: number,
readStream: NodeJS.ReadableStream,
writeStream: fs.WriteStream,

View File

@@ -1,10 +1,10 @@
import { autoUpdater, UpdateInfo } from 'electron-updater';
import log from 'electron-log';
import { handleManualUpdateAsync, requiresManualUpdate } from './ManualUpdater';
import { handleAutoUpdateAsync } from './AutoUpdater';
import { handleManualUpdate, requiresManualUpdate } from './ManualUpdater';
import { handleAutoUpdate } from './AutoUpdater';
interface IUpdater {
checkForUpdatesAsync(): Promise<void>;
checkForUpdates(): Promise<void>;
}
export function setupAutoUpdater(): IUpdater {
@@ -21,20 +21,20 @@ export function setupAutoUpdater(): IUpdater {
return;
}
isAlreadyHandled = true;
await handleAvailableUpdateAsync(info);
await handleAvailableUpdate(info);
});
return {
checkForUpdatesAsync: async () => {
checkForUpdates: async () => {
// autoUpdater.emit('update-available'); // For testing
await autoUpdater.checkForUpdates();
},
};
}
async function handleAvailableUpdateAsync(info: UpdateInfo) {
async function handleAvailableUpdate(info: UpdateInfo) {
if (requiresManualUpdate()) {
await handleManualUpdateAsync(info);
await handleManualUpdate(info);
return;
}
await handleAutoUpdateAsync();
await handleAutoUpdate();
}

View File

@@ -124,7 +124,7 @@ function loadApplication(window: BrowserWindow) {
// Load the index.html when not in development
loadUrlWithNodeWorkaround(win, 'app://./index.html');
const updater = setupAutoUpdater();
updater.checkForUpdatesAsync();
updater.checkForUpdates();
}
}