Open-source and local-first Slack and Notion alternative that puts you in control of your data
TypeScript chatchat-applicationcrdteditorelectronknowledge-baselocal-firstnotionnotion-alternativerealtime-collaborationself-hostedslacksqliteteam-collaborationwikiyjs
Improve synchronizer cleanups in desktop
Hakan Shehu062bae979705
diff --git a/apps/desktop/src/main/data/database-service.ts b/apps/desktop/src/main/data/database-service.tsindex b865c4fe..a12c20fc 100644--- a/apps/desktop/src/main/data/database-service.ts+++ b/apps/desktop/src/main/data/database-service.ts@@ -83,7 +83,14 @@ class DatabaseService {const workspaceDatabase = this.workspaceDatabases.get(userId);if (workspaceDatabase) {- workspaceDatabase.destroy();+ try {+ workspaceDatabase.destroy();+ } catch (error) {+ this.debug(+ `Failed to destroy workspace database for user: ${userId}`,+ error+ );+ }}this.workspaceDatabases.delete(userId);diff --git a/apps/desktop/src/main/scheduler.ts b/apps/desktop/src/main/scheduler.tsindex 57946fcc..7b5a6bde 100644--- a/apps/desktop/src/main/scheduler.ts+++ b/apps/desktop/src/main/scheduler.ts@@ -295,10 +295,6 @@ class Scheduler {}if (this.isWorkspaceJob(state, userId)) {- if (state.timeout) {- clearTimeout(state.timeout);- }-this.deleteJob(state);}}diff --git a/apps/desktop/src/main/services/account-service.ts b/apps/desktop/src/main/services/account-service.tsindex 70bed0b2..9c47da1f 100644--- a/apps/desktop/src/main/services/account-service.ts+++ b/apps/desktop/src/main/services/account-service.ts@@ -336,15 +336,13 @@ class AccountService {if (!deletedWorkspace) {this.debug(`Failed to delete workspace ${userId}`);return false;- } else {- this.debug(`Deleted workspace ${userId}`);}await databaseService.removeWorkspaceDatabase(userId);const workspaceDir = getWorkspaceDirectoryPath(userId);let deleted = false;- for (let attempt = 1; attempt <= 5; attempt++) {+ for (let attempt = 1; attempt <= 10; attempt++) {try {if (fs.existsSync(workspaceDir)) {fs.rmSync(workspaceDir, { recursive: true, force: true });@@ -356,17 +354,17 @@ class AccountService {`Failed to delete workspace directory ${workspaceDir} on attempt ${attempt}`,error);- if (attempt === 5) {+ if (attempt === 10) {break;}await new Promise((resolve) => setTimeout(resolve, 1000));}}- if (!deleted) {- this.debug(`Failed to delete workspace directory ${workspaceDir}`);- } else {+ if (deleted) {this.debug(`Deleted workspace directory ${workspaceDir}`);+ } else {+ this.debug(`Failed to delete workspace directory ${workspaceDir}`);}eventBus.publish({diff --git a/apps/desktop/src/main/services/sync-service.ts b/apps/desktop/src/main/services/sync-service.tsindex 128b1030..09b1b47a 100644--- a/apps/desktop/src/main/services/sync-service.ts+++ b/apps/desktop/src/main/services/sync-service.ts@@ -236,6 +236,8 @@ class SyncService {rootId: string,workspaceDatabase: Kysely<WorkspaceDatabaseSchema>) {+ this.debug('Initializing root node synchronizers', rootId);+await this.initSynchronizer(userId, accountId, workspaceDatabase, {type: 'entry_transactions',rootId,@@ -283,6 +285,8 @@ class SyncService {}private async removeRootNodeSynchronizers(userId: string, rootId: string) {+ this.debug('Removing root node synchronizers', rootId);+const keys = Array.from(this.synchronizers.keys());for (const key of keys) {@@ -301,6 +305,8 @@ class SyncService {await synchronizer.delete();this.synchronizers.delete(key);++ this.debug('Removed root node synchronizer', userId, synchronizer.input);}}@@ -316,6 +322,12 @@ class SyncService {if (synchronizer.userId === userId) {await synchronizer.delete();this.synchronizers.delete(key);++ this.debug(+ 'Removed workspace synchronizer',+ userId,+ synchronizer.input+ );}}}@@ -332,6 +344,12 @@ class SyncService {if (synchronizer.accountId === accountId) {await synchronizer.delete();this.synchronizers.delete(key);++ this.debug(+ 'Removed account synchronizer',+ accountId,+ synchronizer.input+ );}}}diff --git a/apps/desktop/src/main/synchronizers/base.ts b/apps/desktop/src/main/synchronizers/base.tsindex c347ca10..ed9fa79a 100644--- a/apps/desktop/src/main/synchronizers/base.ts+++ b/apps/desktop/src/main/synchronizers/base.ts@@ -97,7 +97,7 @@ export abstract class BaseSynchronizer<TInput extends SynchronizerInput> {};const sent = socketService.sendMessage(this.accountId, message);- if (!sent) {+ if (sent) {this.status = 'waiting';}}@@ -130,10 +130,14 @@ export abstract class BaseSynchronizer<TInput extends SynchronizerInput> {}public async delete() {- await this.database- .deleteFrom('cursors')- .where('key', '=', this.cursorKey)- .execute();+ try {+ await this.database+ .deleteFrom('cursors')+ .where('key', '=', this.cursorKey)+ .execute();+ } catch {+ this.debug('Failed to delete cursor', this.cursorKey);+ }}private generateId(userId: string, input: TInput) {