feat: add new rule for floating promise (#2726)

Co-authored-by: Himself65 <himself65@outlook.com>
(cherry picked from commit bedf838fe5)
This commit is contained in:
Peng Xiao
2023-06-08 17:54:25 +08:00
committed by himself65
parent 336ff5220f
commit 9429018221
45 changed files with 221 additions and 134 deletions

View File

@@ -119,7 +119,7 @@ export function createApplicationMenu() {
{
label: 'Open log file',
click: async () => {
revealLogFile();
await revealLogFile();
},
},
{

View File

@@ -62,7 +62,7 @@ test('on applyUpdate (from renderer), will trigger update', async () => {
db.update$.subscribe(onUpdate);
const sub = dbSubjects.externalUpdate.subscribe(onExternalUpdate);
db.applyUpdate(getTestUpdates(), 'renderer');
expect(onUpdate).toHaveBeenCalled(); // not yet updated
expect(onUpdate).toHaveBeenCalled();
sub.unsubscribe();
await db.destroy();
});

View File

@@ -11,6 +11,7 @@ import {
merge,
} from 'rxjs';
import {
concatMap,
distinctUntilChanged,
filter,
ignoreElements,
@@ -126,10 +127,8 @@ function startPollingSecondaryDB(db: WorkspaceSQLiteDB) {
switchMap(secondaryDB => {
return interval(300000).pipe(
startWith(0),
concatMap(() => secondaryDB.pull()),
tap({
next: () => {
secondaryDB.pull();
},
error: err => {
logger.error(`[ensureSQLiteDB] polling secondary db error`, err);
},

View File

@@ -93,6 +93,7 @@ export class SecondaryWorkspaceSQLiteDB extends BaseSQLiteAdapter {
return await fn();
} catch (err) {
logger.error(err);
throw err;
} finally {
this.runCounter--;
if (this.runCounter === 0) {
@@ -115,10 +116,10 @@ export class SecondaryWorkspaceSQLiteDB extends BaseSQLiteAdapter {
}
};
const onSelfUpdate = (update: Uint8Array, origin: YOrigin) => {
const onSelfUpdate = async (update: Uint8Array, origin: YOrigin) => {
// for self update from upstream, we need to push it to external DB
if (origin === 'upstream' && this.db) {
this.addUpdateToUpdateQueue(this.db, update);
await this.addUpdateToUpdateQueue(this.db, update);
}
if (origin === 'self') {
@@ -135,12 +136,18 @@ export class SecondaryWorkspaceSQLiteDB extends BaseSQLiteAdapter {
this.yDoc.off('update', onSelfUpdate);
});
this.run(async () => {
this.run(() => {
// apply all updates from upstream
const upstreamUpdate = this.upstream.getDocAsUpdates();
// to initialize the yDoc, we need to apply all updates from the db
this.applyUpdate(upstreamUpdate, 'upstream');
});
})
.then(() => {
logger.debug('run success');
})
.catch(err => {
logger.error('run error', err);
});
}
applyUpdate = (data: Uint8Array, origin: YOrigin = 'upstream') => {

View File

@@ -79,19 +79,19 @@ export class WorkspaceSQLiteDB extends BaseSQLiteAdapter {
};
override async addBlob(key: string, value: Uint8Array) {
const res = await super.addBlob(key, value);
this.update$.next();
const res = await super.addBlob(key, value);
return res;
}
override async deleteBlob(key: string) {
super.deleteBlob(key);
this.update$.next();
await super.deleteBlob(key);
}
override async addUpdateToSQLite(db: SqliteConnection, data: Uint8Array[]) {
super.addUpdateToSQLite(db, data);
this.update$.next();
await super.addUpdateToSQLite(db, data);
}
}

View File

@@ -320,7 +320,7 @@ export async function moveDBFile(
filePath: newFilePath,
};
} catch (err) {
db?.destroy();
await db?.destroy();
logger.error('[moveDBFile]', err);
return {
error: 'UNKNOWN_ERROR',

View File

@@ -50,7 +50,7 @@ export async function savePDFFileAs(
});
});
shell.openPath(filePath);
await shell.openPath(filePath);
return { filePath };
} catch (err) {
logger.error('savePDFFileAs', err);

View File

@@ -29,7 +29,9 @@ if (!isSingleInstance) {
}
app.on('second-instance', () => {
restoreOrCreateWindow();
restoreOrCreateWindow().catch(e =>
console.error('Failed to restore or create window:', e)
);
});
app.on('open-url', async (_, _url) => {

View File

@@ -28,10 +28,12 @@ export const getExchangeTokenParams = (code: string) => {
};
export function getGoogleOauthCode() {
shell.openExternal(oauthEndpoint);
return new Promise<ReturnType<typeof getExchangeTokenParams>>(
(resolve, reject) => {
shell.openExternal(oauthEndpoint).catch(e => {
logger.error('Failed to open external url', e);
reject(e);
});
const handleOpenUrl = async (_: any, url: string) => {
const mainWindow = BrowserWindow.getAllWindows().find(
w => !w.isDestroyed()

View File

@@ -67,7 +67,9 @@ export const registerUpdater = async () => {
// register events for checkForUpdatesAndNotify
_autoUpdater.on('update-available', info => {
if (allowAutoUpdate) {
_autoUpdater?.downloadUpdate();
_autoUpdater?.downloadUpdate().catch(e => {
logger.error('Failed to download update', e);
});
logger.info('Update available, downloading...', info);
}
updaterSubjects.updateAvailable.next({