Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Revert async event bus

Hakan Shehu6097b6245196
diff --git a/apps/desktop/src/main/mutations/account-update.ts b/apps/desktop/src/main/mutations/account-update.ts
index 2d95638f..12e99003 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 AccountUpdateMutationHandler
throw new Error('Account not found');
}
- await eventBus.publish({
+ 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.ts
index b5ab6c39..57cdcf2b 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 EmailLoginMutationHandler
throw new Error('Failed to create account!');
}
- await eventBus.publish({
+ eventBus.publish({
type: 'account_created',
account,
});
if (data.workspaces.length > 0) {
for (const workspace of data.workspaces) {
- await eventBus.publish({
+ 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.ts
index 7c886744..a7096672 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 EmailRegisterMutationHandler
throw new Error('Failed to create account!');
}
- await eventBus.publish({
+ eventBus.publish({
type: 'account_created',
account,
});
if (data.workspaces.length > 0) {
for (const workspace of data.workspaces) {
- await eventBus.publish({
+ 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.ts
index 0edfb04b..07ebf322 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();
- await eventBus.publish({
+ 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.ts
index b1490408..e4b98473 100644
--- a/apps/desktop/src/main/mutations/mark-node-as-seen.ts
+++ b/apps/desktop/src/main/mutations/mark-node-as-seen.ts
@@ -95,7 +95,7 @@ export class MarkNodeAsSeenMutationHandler
});
if (userNode) {
- await eventBus.publish({
+ eventBus.publish({
type: 'user_node_created',
userId: input.userId,
userNode,
@@ -103,7 +103,7 @@ export class MarkNodeAsSeenMutationHandler
}
if (changeId) {
- await eventBus.publish({
+ 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.ts
index c003656b..6bfbb9af 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();
- await eventBus.publish({
+ eventBus.publish({
type: 'server_created',
server: {
domain: input.domain,
diff --git a/apps/desktop/src/main/mutations/workspace-create.ts b/apps/desktop/src/main/mutations/workspace-create.ts
index fdd216c7..3118513a 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 WorkspaceCreateMutationHandler
throw new Error('Failed to create workspace!');
}
- await eventBus.publish({
+ 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.ts
index 9c1fb10e..9e737056 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 WorkspaceUpdateMutationHandler
throw new Error('Failed to update workspace!');
}
- await eventBus.publish({
+ 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.ts
index fbc64080..67cdb6dc 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
};
}
- await eventBus.publish({
+ 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.ts
index a175ff44..9b13a3ed 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) {
- await eventBus.publish({
+ 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.ts
index a671c46d..fd082b52 100644
--- a/apps/desktop/src/main/services/account-service.ts
+++ b/apps/desktop/src/main/services/account-service.ts
@@ -76,7 +76,7 @@ class AccountService {
return;
}
- await eventBus.publish({
+ eventBus.publish({
type: 'account_updated',
account: mapAccount(updatedAccount),
});
@@ -107,7 +107,7 @@ class AccountService {
return;
}
- await eventBus.publish({
+ eventBus.publish({
type: 'workspace_created',
workspace: mapWorkspace(createdWorkspace),
});
@@ -130,7 +130,7 @@ class AccountService {
return;
}
- await eventBus.publish({
+ eventBus.publish({
type: 'workspace_updated',
workspace: mapWorkspace(updatedWorkspace),
});
@@ -174,7 +174,7 @@ class AccountService {
return false;
}
- await eventBus.publish({
+ eventBus.publish({
type: 'account_deleted',
account: mapAccount(deletedAccount),
});
@@ -251,7 +251,7 @@ class AccountService {
fs.rmSync(workspaceDir, { recursive: true });
}
- await eventBus.publish({
+ eventBus.publish({
type: 'workspace_deleted',
workspace: mapWorkspace(deletedWorkspace),
});
diff --git a/apps/desktop/src/main/services/file-service.ts b/apps/desktop/src/main/services/file-service.ts
index c46888d2..a38b51de 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();
- await eventBus.publish({
+ eventBus.publish({
type: 'download_deleted',
userId: credentials.userId,
download: {
@@ -249,7 +249,7 @@ class FileService {
.where('node_id', '=', download.node_id)
.execute();
- await eventBus.publish({
+ eventBus.publish({
type: 'download_updated',
userId: credentials.userId,
download: {
@@ -291,7 +291,7 @@ class FileService {
.where('node_id', '=', download.node_id)
.execute();
- await eventBus.publish({
+ eventBus.publish({
type: 'download_updated',
userId: credentials.userId,
download: {
@@ -311,7 +311,7 @@ class FileService {
.where('node_id', '=', download.node_id)
.execute();
- await eventBus.publish({
+ 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.ts
index 2ca8c127..1405a02e 100644
--- a/apps/desktop/src/main/services/node-service.ts
+++ b/apps/desktop/src/main/services/node-service.ts
@@ -156,7 +156,7 @@ class NodeService {
});
for (const createdNode of createdNodes) {
- await eventBus.publish({
+ eventBus.publish({
type: 'node_created',
userId,
node: createdNode,
@@ -164,7 +164,7 @@ class NodeService {
}
for (const createdUpload of createdUploads) {
- await eventBus.publish({
+ eventBus.publish({
type: 'upload_created',
userId,
upload: createdUpload,
@@ -172,7 +172,7 @@ class NodeService {
}
for (const createdDownload of createdDownloads) {
- await eventBus.publish({
+ eventBus.publish({
type: 'download_created',
userId,
download: createdDownload,
@@ -180,7 +180,7 @@ class NodeService {
}
for (const createdChangeId of createdChangeIds) {
- await eventBus.publish({
+ eventBus.publish({
type: 'change_created',
userId,
changeId: createdChangeId,
@@ -305,7 +305,7 @@ class NodeService {
});
if (result) {
- await eventBus.publish({
+ eventBus.publish({
type: 'node_updated',
userId,
node,
@@ -313,7 +313,7 @@ class NodeService {
}
if (changeId) {
- await eventBus.publish({
+ eventBus.publish({
type: 'change_created',
userId,
changeId,
@@ -382,14 +382,14 @@ class NodeService {
}
});
- await eventBus.publish({
+ eventBus.publish({
type: 'node_deleted',
userId,
node: node,
});
if (changeId) {
- await eventBus.publish({
+ eventBus.publish({
type: 'change_created',
userId,
changeId,
@@ -444,7 +444,7 @@ class NodeService {
.executeTakeFirst();
if (result) {
- await eventBus.publish({
+ eventBus.publish({
type: 'node_created',
userId: userId,
node: mapNode(result),
@@ -477,7 +477,7 @@ class NodeService {
.executeTakeFirst();
if (updatedNode) {
- await eventBus.publish({
+ eventBus.publish({
type: 'node_updated',
userId: userId,
node: mapNode(updatedNode),
@@ -501,7 +501,7 @@ class NodeService {
.executeTakeFirst();
if (deletedNode) {
- await eventBus.publish({
+ eventBus.publish({
type: 'node_deleted',
userId,
node: mapNode(deletedNode),
@@ -543,7 +543,7 @@ class NodeService {
.executeTakeFirst();
if (createdUserNode) {
- await eventBus.publish({
+ eventBus.publish({
type: 'user_node_created',
userId: userId,
userNode: {
diff --git a/apps/desktop/src/main/services/query-service.ts b/apps/desktop/src/main/services/query-service.ts
index c5793a79..3ddc3eb8 100644
--- a/apps/desktop/src/main/services/query-service.ts
+++ b/apps/desktop/src/main/services/query-service.ts
@@ -10,8 +10,8 @@ class QueryService {
new Map();
constructor() {
- eventBus.subscribe(async (event: Event) => {
- await this.checkForQueryChanges(event);
+ eventBus.subscribe((event: Event) => {
+ this.checkForQueryChanges(event);
});
}
@@ -58,6 +58,7 @@ class QueryService {
query.input,
query.result
);
+
if (
changeCheckResult.hasChanges &&
changeCheckResult.result &&
diff --git a/apps/desktop/src/main/services/server-service.ts b/apps/desktop/src/main/services/server-service.ts
index fa6379a9..9978fb49 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) {
- await eventBus.publish({
+ eventBus.publish({
type: 'server_availability_changed',
server,
isAvailable,
@@ -74,7 +74,7 @@ class ServerService {
.executeTakeFirst();
if (updatedServer) {
- await eventBus.publish({
+ 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.ts
index 9669f5ac..74a550e5 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(async (event) => {
+ eventBus.subscribe((event) => {
if (event.type === 'server_availability_changed' && event.isAvailable) {
this.checkConnections();
} else if (
diff --git a/apps/desktop/src/shared/lib/event-bus.ts b/apps/desktop/src/shared/lib/event-bus.ts
index b9301bcb..962f63d7 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 | Promise<void>;
+ callback: (event: Event) => void;
}
export interface EventBus {
@@ -11,20 +11,16 @@ 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> = new Map();
- private eventQueue: EventPromise[] = [];
- private isProcessing = false;
+ private subscriptions: Map<string, Subscription>;
private id = 0;
- public subscribe(callback: (event: Event) => void | Promise<void>): string {
- const id = (this.id++).toString();
+ public constructor() {
+ this.subscriptions = new Map<string, Subscription>();
+ }
+
+ public subscribe(callback: (event: Event) => void): string {
+ const id = (this.id++).toLocaleString();
this.subscriptions.set(id, {
callback,
id,
@@ -33,34 +29,16 @@ export class EventBusService {
}
public unsubscribe(subscriptionId: string) {
+ if (!this.subscriptions.has(subscriptionId)) return;
+
this.subscriptions.delete(subscriptionId);
}
- public publish(event: Event): Promise<void> {
- return new Promise<void>((resolve, reject) => {
- this.eventQueue.push({ event, resolve, reject });
- if (!this.isProcessing) {
- this.processQueue();
- }
+ public publish(event: Event) {
+ this.subscriptions.forEach((subscription) => {
+ subscription.callback(event);
});
}
-
- 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();