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
Make event bus async
Hakan Shehu2fe78cf13c4e
diff --git a/apps/desktop/src/main/mutations/account-update.ts b/apps/desktop/src/main/mutations/account-update.tsindex 12e99003..2d95638f 100644--- a/apps/desktop/src/main/mutations/account-update.ts+++ b/apps/desktop/src/main/mutations/account-update.ts@@ -60,7 +60,7 @@ export class AccountUpdateMutationHandlerthrow new Error('Account not found');}- eventBus.publish({+ await eventBus.publish({type: 'account_updated',account: {id: updatedAccount.id,diff --git a/apps/desktop/src/main/mutations/email-login.ts b/apps/desktop/src/main/mutations/email-login.tsindex 57cdcf2b..b5ab6c39 100644--- a/apps/desktop/src/main/mutations/email-login.ts+++ b/apps/desktop/src/main/mutations/email-login.ts@@ -95,14 +95,14 @@ export class EmailLoginMutationHandlerthrow new Error('Failed to create account!');}- eventBus.publish({+ await eventBus.publish({type: 'account_created',account,});if (data.workspaces.length > 0) {for (const workspace of data.workspaces) {- eventBus.publish({+ await eventBus.publish({type: 'workspace_created',workspace: {id: workspace.id,diff --git a/apps/desktop/src/main/mutations/email-register.ts b/apps/desktop/src/main/mutations/email-register.tsindex a7096672..7c886744 100644--- a/apps/desktop/src/main/mutations/email-register.ts+++ b/apps/desktop/src/main/mutations/email-register.ts@@ -96,14 +96,14 @@ export class EmailRegisterMutationHandlerthrow new Error('Failed to create account!');}- eventBus.publish({+ await eventBus.publish({type: 'account_created',account,});if (data.workspaces.length > 0) {for (const workspace of data.workspaces) {- eventBus.publish({+ await eventBus.publish({type: 'workspace_created',workspace: {id: workspace.id,diff --git a/apps/desktop/src/main/mutations/file-download.ts b/apps/desktop/src/main/mutations/file-download.tsindex 07ebf322..0edfb04b 100644--- a/apps/desktop/src/main/mutations/file-download.ts+++ b/apps/desktop/src/main/mutations/file-download.ts@@ -51,7 +51,7 @@ export class FileDownloadMutationHandler}).execute();- eventBus.publish({+ await eventBus.publish({type: 'download_created',userId: input.userId,download: {diff --git a/apps/desktop/src/main/mutations/mark-node-as-seen.ts b/apps/desktop/src/main/mutations/mark-node-as-seen.tsindex 415a3801..66504411 100644--- a/apps/desktop/src/main/mutations/mark-node-as-seen.ts+++ b/apps/desktop/src/main/mutations/mark-node-as-seen.ts@@ -75,7 +75,7 @@ export class MarkNodeAsSeenMutationHandler});if (userNode) {- eventBus.publish({+ await eventBus.publish({type: 'user_node_created',userId: input.userId,userNode,@@ -83,7 +83,7 @@ export class MarkNodeAsSeenMutationHandler}if (changeId) {- eventBus.publish({+ await eventBus.publish({type: 'change_created',userId: input.userId,changeId,diff --git a/apps/desktop/src/main/mutations/server-create.ts b/apps/desktop/src/main/mutations/server-create.tsindex 6bfbb9af..c003656b 100644--- a/apps/desktop/src/main/mutations/server-create.ts+++ b/apps/desktop/src/main/mutations/server-create.ts@@ -33,7 +33,7 @@ export class ServerCreateMutationHandler}).execute();- eventBus.publish({+ await eventBus.publish({type: 'server_created',server: {domain: input.domain,diff --git a/apps/desktop/src/main/mutations/server-node-delete.ts b/apps/desktop/src/main/mutations/server-node-delete.tsindex 77181b2e..f61c3e2d 100644--- a/apps/desktop/src/main/mutations/server-node-delete.ts+++ b/apps/desktop/src/main/mutations/server-node-delete.ts@@ -48,7 +48,7 @@ export class ServerNodeDeleteMutationHandler});if (deletedNode) {- eventBus.publish({+ await eventBus.publish({type: 'node_deleted',userId,node: mapNode(deletedNode),diff --git a/apps/desktop/src/main/mutations/server-node-sync.ts b/apps/desktop/src/main/mutations/server-node-sync.tsindex 921ec1ce..d208676e 100644--- a/apps/desktop/src/main/mutations/server-node-sync.ts+++ b/apps/desktop/src/main/mutations/server-node-sync.ts@@ -75,7 +75,7 @@ export class ServerNodeSyncMutationHandlerworkspaceId: input.workspaceId,});- eventBus.publish({+ await eventBus.publish({type: 'node_created',userId: userId,node: mapNode(result),@@ -118,7 +118,7 @@ export class ServerNodeSyncMutationHandlerworkspaceId: input.workspaceId,});- eventBus.publish({+ await eventBus.publish({type: 'node_updated',userId: userId,node: mapNode(updatedNode),diff --git a/apps/desktop/src/main/mutations/server-user-node-sync.ts b/apps/desktop/src/main/mutations/server-user-node-sync.tsindex 91ba6ab5..6cae340d 100644--- a/apps/desktop/src/main/mutations/server-user-node-sync.ts+++ b/apps/desktop/src/main/mutations/server-user-node-sync.ts@@ -68,7 +68,7 @@ export class ServerUserNodeSyncMutationHandler});if (createdUserNode) {- eventBus.publish({+ await eventBus.publish({type: 'user_node_created',userId: userId,userNode: {diff --git a/apps/desktop/src/main/mutations/workspace-create.ts b/apps/desktop/src/main/mutations/workspace-create.tsindex 3118513a..fdd216c7 100644--- a/apps/desktop/src/main/mutations/workspace-create.ts+++ b/apps/desktop/src/main/mutations/workspace-create.ts@@ -67,7 +67,7 @@ export class WorkspaceCreateMutationHandlerthrow new Error('Failed to create workspace!');}- eventBus.publish({+ await eventBus.publish({type: 'workspace_created',workspace: {id: createdWorkspace.workspace_id,diff --git a/apps/desktop/src/main/mutations/workspace-update.ts b/apps/desktop/src/main/mutations/workspace-update.tsindex 9e737056..9c1fb10e 100644--- a/apps/desktop/src/main/mutations/workspace-update.ts+++ b/apps/desktop/src/main/mutations/workspace-update.ts@@ -69,7 +69,7 @@ export class WorkspaceUpdateMutationHandlerthrow new Error('Failed to update workspace!');}- eventBus.publish({+ await eventBus.publish({type: 'workspace_updated',workspace: {id: updatedWorkspace.workspace_id,diff --git a/apps/desktop/src/main/mutations/workspace-user-role-update.ts b/apps/desktop/src/main/mutations/workspace-user-role-update.tsindex 67cdb6dc..fbc64080 100644--- a/apps/desktop/src/main/mutations/workspace-user-role-update.ts+++ b/apps/desktop/src/main/mutations/workspace-user-role-update.ts@@ -88,7 +88,7 @@ export class WorkspaceUserRoleUpdateMutationHandler};}- eventBus.publish({+ await eventBus.publish({type: 'node_updated',userId: input.userId,node: mapNode(updatedUser),diff --git a/apps/desktop/src/main/mutations/workspace-users-invite.ts b/apps/desktop/src/main/mutations/workspace-users-invite.tsindex 9b13a3ed..a175ff44 100644--- a/apps/desktop/src/main/mutations/workspace-users-invite.ts+++ b/apps/desktop/src/main/mutations/workspace-users-invite.ts@@ -99,7 +99,7 @@ export class WorkspaceUsersInviteMutationHandler}for (const createdNode of createdNodes) {- eventBus.publish({+ await eventBus.publish({type: 'node_created',userId: input.userId,node: mapNode(createdNode),diff --git a/apps/desktop/src/main/services/account-service.ts b/apps/desktop/src/main/services/account-service.tsindex 61e9a331..722a7463 100644--- a/apps/desktop/src/main/services/account-service.ts+++ b/apps/desktop/src/main/services/account-service.ts@@ -73,7 +73,7 @@ class AccountService {return;}- eventBus.publish({+ await eventBus.publish({type: 'account_updated',account: mapAccount(updatedAccount),});@@ -104,7 +104,7 @@ class AccountService {return;}- eventBus.publish({+ await eventBus.publish({type: 'workspace_created',workspace: mapWorkspace(createdWorkspace),});@@ -127,7 +127,7 @@ class AccountService {return;}- eventBus.publish({+ await eventBus.publish({type: 'workspace_updated',workspace: mapWorkspace(updatedWorkspace),});@@ -171,7 +171,7 @@ class AccountService {return false;}- eventBus.publish({+ await eventBus.publish({type: 'account_deleted',account: mapAccount(deletedAccount),});diff --git a/apps/desktop/src/main/services/file-service.ts b/apps/desktop/src/main/services/file-service.tsindex a38b51de..c46888d2 100644--- a/apps/desktop/src/main/services/file-service.ts+++ b/apps/desktop/src/main/services/file-service.ts@@ -219,7 +219,7 @@ class FileService {.where('node_id', '=', download.node_id).execute();- eventBus.publish({+ await eventBus.publish({type: 'download_deleted',userId: credentials.userId,download: {@@ -249,7 +249,7 @@ class FileService {.where('node_id', '=', download.node_id).execute();- eventBus.publish({+ await eventBus.publish({type: 'download_updated',userId: credentials.userId,download: {@@ -291,7 +291,7 @@ class FileService {.where('node_id', '=', download.node_id).execute();- eventBus.publish({+ await eventBus.publish({type: 'download_updated',userId: credentials.userId,download: {@@ -311,7 +311,7 @@ class FileService {.where('node_id', '=', download.node_id).execute();- eventBus.publish({+ await eventBus.publish({type: 'download_updated',userId: credentials.userId,download: {diff --git a/apps/desktop/src/main/services/node-service.ts b/apps/desktop/src/main/services/node-service.tsindex 34aa94e5..1b8fc3ff 100644--- a/apps/desktop/src/main/services/node-service.ts+++ b/apps/desktop/src/main/services/node-service.ts@@ -158,7 +158,7 @@ class NodeService {});for (const createdNode of createdNodes) {- eventBus.publish({+ await eventBus.publish({type: 'node_created',userId,node: createdNode,@@ -166,7 +166,7 @@ class NodeService {}for (const createdUpload of createdUploads) {- eventBus.publish({+ await eventBus.publish({type: 'upload_created',userId,upload: createdUpload,@@ -174,7 +174,7 @@ class NodeService {}for (const createdDownload of createdDownloads) {- eventBus.publish({+ await eventBus.publish({type: 'download_created',userId,download: createdDownload,@@ -182,7 +182,7 @@ class NodeService {}for (const createdChangeId of createdChangeIds) {- eventBus.publish({+ await eventBus.publish({type: 'change_created',userId,changeId: createdChangeId,@@ -315,7 +315,7 @@ class NodeService {});if (result) {- eventBus.publish({+ await eventBus.publish({type: 'node_updated',userId,node,@@ -323,7 +323,7 @@ class NodeService {}if (changeId) {- eventBus.publish({+ await eventBus.publish({type: 'change_created',userId,changeId,@@ -400,14 +400,14 @@ class NodeService {}});- eventBus.publish({+ await eventBus.publish({type: 'node_deleted',userId,node: node,});if (changeId) {- eventBus.publish({+ await eventBus.publish({type: 'change_created',userId,changeId,diff --git a/apps/desktop/src/main/services/query-service.ts b/apps/desktop/src/main/services/query-service.tsindex 5fefb85d..8867d0fc 100644--- a/apps/desktop/src/main/services/query-service.ts+++ b/apps/desktop/src/main/services/query-service.ts@@ -9,8 +9,8 @@ class QueryService {new Map();constructor() {- eventBus.subscribe((event: Event) => {- this.checkForQueryChanges(event);+ eventBus.subscribe(async (event: Event) => {+ await this.checkForQueryChanges(event);});}diff --git a/apps/desktop/src/main/services/server-service.ts b/apps/desktop/src/main/services/server-service.tsindex 9978fb49..fa6379a9 100644--- a/apps/desktop/src/main/services/server-service.ts+++ b/apps/desktop/src/main/services/server-service.ts@@ -48,7 +48,7 @@ class ServerService {const wasAvailable = existingState?.isAvailable ?? false;const isAvailable = newState.isAvailable;if (wasAvailable !== isAvailable) {- eventBus.publish({+ await eventBus.publish({type: 'server_availability_changed',server,isAvailable,@@ -74,7 +74,7 @@ class ServerService {.executeTakeFirst();if (updatedServer) {- eventBus.publish({+ await eventBus.publish({type: 'server_updated',server: mapServer(updatedServer),});diff --git a/apps/desktop/src/main/services/socket-service.ts b/apps/desktop/src/main/services/socket-service.tsindex 74a550e5..9669f5ac 100644--- a/apps/desktop/src/main/services/socket-service.ts+++ b/apps/desktop/src/main/services/socket-service.ts@@ -8,7 +8,7 @@ class SocketService {private readonly sockets: Map<string, SocketConnection> = new Map();constructor() {- eventBus.subscribe((event) => {+ eventBus.subscribe(async (event) => {if (event.type === 'server_availability_changed' && event.isAvailable) {this.checkConnections();} else if (diff --git a/apps/desktop/src/main/services/workspace-service.ts b/apps/desktop/src/main/services/workspace-service.tsindex b231b4b1..f83a7d88 100644--- a/apps/desktop/src/main/services/workspace-service.ts+++ b/apps/desktop/src/main/services/workspace-service.ts@@ -45,7 +45,7 @@ class WorkspaceService {fs.rmSync(workspaceDir, { recursive: true });}- eventBus.publish({+ await eventBus.publish({type: 'workspace_deleted',workspace: mapWorkspace(deletedWorkspace),});diff --git a/apps/desktop/src/renderer/components/accounts/email-login.tsx b/apps/desktop/src/renderer/components/accounts/email-login.tsxindex 784bb7e9..84f91819 100644--- a/apps/desktop/src/renderer/components/accounts/email-login.tsx+++ b/apps/desktop/src/renderer/components/accounts/email-login.tsx@@ -47,7 +47,9 @@ export const EmailLogin = ({ server }: EmailLoginProps) => {},onSuccess(output) {if (output.success) {- navigate(`/${output.account.id}`);+ const userId =+ output.workspaces.length > 0 ? output.workspaces[0].id : '';+ navigate(`/${output.account.id}/${userId}`);} else {toast({title: 'Failed to login',diff --git a/apps/desktop/src/renderer/components/accounts/email-register.tsx b/apps/desktop/src/renderer/components/accounts/email-register.tsxindex a33896d8..2a1d41b7 100644--- a/apps/desktop/src/renderer/components/accounts/email-register.tsx+++ b/apps/desktop/src/renderer/components/accounts/email-register.tsx@@ -50,7 +50,9 @@ export const EmailRegister = ({ server }: EmailRegisterProps) => {},onSuccess(output) {if (output.success) {- navigate(`/${output.account.id}`);+ const userId =+ output.workspaces.length > 0 ? output.workspaces[0].id : '';+ navigate(`/${output.account.id}/${userId}`);} else {toast({title: 'Failed to register',diff --git a/apps/desktop/src/shared/lib/event-bus.ts b/apps/desktop/src/shared/lib/event-bus.tsindex 962f63d7..b9301bcb 100644--- a/apps/desktop/src/shared/lib/event-bus.ts+++ b/apps/desktop/src/shared/lib/event-bus.ts@@ -2,7 +2,7 @@ import { Event } from '@/shared/types/events';export interface Subscription {id: string;- callback: (event: Event) => void;+ callback: (event: Event) => void | Promise<void>;}export interface EventBus {@@ -11,16 +11,20 @@ export interface EventBus {publish(event: Event): void;}+interface EventPromise {+ event: Event;+ resolve: () => void;+ reject: (error: any) => void;+}+export class EventBusService {- private subscriptions: Map<string, Subscription>;+ private subscriptions: Map<string, Subscription> = new Map();+ private eventQueue: EventPromise[] = [];+ private isProcessing = false;private id = 0;- public constructor() {- this.subscriptions = new Map<string, Subscription>();- }-- public subscribe(callback: (event: Event) => void): string {- const id = (this.id++).toLocaleString();+ public subscribe(callback: (event: Event) => void | Promise<void>): string {+ const id = (this.id++).toString();this.subscriptions.set(id, {callback,id,@@ -29,16 +33,34 @@ export class EventBusService {}public unsubscribe(subscriptionId: string) {- if (!this.subscriptions.has(subscriptionId)) return;-this.subscriptions.delete(subscriptionId);}- public publish(event: Event) {- this.subscriptions.forEach((subscription) => {- subscription.callback(event);+ public publish(event: Event): Promise<void> {+ return new Promise<void>((resolve, reject) => {+ this.eventQueue.push({ event, resolve, reject });+ if (!this.isProcessing) {+ this.processQueue();+ }});}++ private async processQueue(): Promise<void> {+ this.isProcessing = true;+ while (this.eventQueue.length > 0) {+ const { event, resolve, reject } = this.eventQueue.shift()!;+ try {+ // Process the event by calling all subscribers+ for (const subscription of this.subscriptions.values()) {+ await subscription.callback(event);+ }+ resolve();+ } catch (error) {+ reject(error);+ }+ }+ this.isProcessing = false;+ }}export const eventBus = new EventBusService();