Skip to content

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

Refactor sockets

Hakan Shehu1f2059a6ab9e
diff --git a/desktop/src/main.ts b/desktop/src/main.ts
index 4cb2e97c..30d6bf37 100644
--- a/desktop/src/main.ts
+++ b/desktop/src/main.ts
@@ -5,6 +5,7 @@ import { MutationInput, MutationMap } from '@/types/mutations';
import { QueryInput, QueryMap } from '@/types/queries';
import { mediator } from '@/main/mediator';
import { databaseContext } from '@/main/data/database-context';
+import { socketManager } from '@/main/sockets/socket-manager';
let subscriptionId: string | null = null;
@@ -15,6 +16,7 @@ if (require('electron-squirrel-startup')) {
const createWindow = async () => {
await databaseContext.init();
+ socketManager.init();
// Create the browser window.
const mainWindow = new BrowserWindow({
@@ -75,18 +77,28 @@ ipcMain.handle(
_: unknown,
input: T,
): Promise<MutationMap[T['type']]['output']> => {
- return mediator.handleMutation(input);
+ return mediator.executeMutation(input);
},
);
ipcMain.handle(
'execute-query',
+ async <T extends QueryInput>(
+ _: unknown,
+ input: T,
+ ): Promise<QueryMap[T['type']]['output']> => {
+ return mediator.executeQuery(input);
+ },
+);
+
+ipcMain.handle(
+ 'execute-query-and-subscribe',
async <T extends QueryInput>(
_: unknown,
id: string,
input: T,
): Promise<QueryMap[T['type']]['output']> => {
- return mediator.handleQuery(id, input);
+ return mediator.executeQueryAndSubscribe(id, input);
},
);
diff --git a/desktop/src/main/account-manager.ts b/desktop/src/main/account-manager.ts
deleted file mode 100644
index dd7361a4..00000000
--- a/desktop/src/main/account-manager.ts
+++ /dev/null
@@ -1,137 +0,0 @@
-// import * as fs from 'node:fs';
-// import Axios, { AxiosInstance } from 'axios';
-// import { Account } from '@/types/accounts';
-// import { Workspace } from '@/types/workspaces';
-// import { WorkspaceManager } from '@/main/workspace-manager';
-// import { SocketManager } from '@/main/socket-manager';
-// import { ServerMutation } from '@/types/mutations';
-// import { Kysely } from 'kysely';
-// import { AppDatabaseSchema } from '@/main/schemas/app';
-// import { Server } from '@/types/servers';
-// import { buildApiBaseUrl } from '@/lib/servers';
-
-// export class AccountManager {
-// public readonly account: Account;
-// private readonly accountPath: string;
-// private readonly socket: SocketManager;
-// private readonly axios: AxiosInstance;
-// private readonly workspaces: Map<string, WorkspaceManager>;
-// private readonly database: Kysely<AppDatabaseSchema>;
-
-// constructor(
-// server: Server,
-// account: Account,
-// appPath: string,
-// workspaces: Workspace[],
-// database: Kysely<AppDatabaseSchema>,
-// ) {
-// this.account = account;
-// this.database = database;
-// this.socket = new SocketManager(server, account);
-// this.axios = Axios.create({
-// baseURL: buildApiBaseUrl(server),
-// headers: {
-// Authorization: `Bearer ${account.token}`,
-// DeviceId: account.deviceId,
-// },
-// });
-
-// this.workspaces = new Map<string, WorkspaceManager>();
-// this.accountPath = `${appPath}/${account.id}`;
-// if (!fs.existsSync(this.accountPath)) {
-// fs.mkdirSync(this.accountPath);
-// }
-
-// for (const workspace of workspaces) {
-// this.workspaces.set(
-// workspace.id,
-// new WorkspaceManager(workspace, this.axios, this.accountPath),
-// );
-// }
-
-// this.socket.on('mutation', async (mutation) => {
-// try {
-// const serverMutation = mutation as ServerMutation;
-// const workspace = this.workspaces.get(serverMutation.workspaceId);
-// if (!workspace) {
-// return;
-// }
-
-// const executed = await workspace.executeServerMutation(serverMutation);
-// if (executed) {
-// this.socket.send({
-// type: 'mutation_ack',
-// payload: {
-// id: serverMutation.id,
-// workspaceId: serverMutation.workspaceId,
-// },
-// });
-// }
-// } catch (error) {
-// console.log(error);
-// }
-// });
-// }
-
-// public async init(): Promise<void> {
-// for (const workspace of this.workspaces.values()) {
-// await workspace.init();
-// }
-// }
-
-// public getWorkspace(workspaceId: string): WorkspaceManager | undefined {
-// return this.workspaces.get(workspaceId);
-// }
-
-// public async addWorkspace(workspace: Workspace): Promise<void> {
-// const workspaceManager = new WorkspaceManager(
-// workspace,
-// this.axios,
-// this.accountPath,
-// );
-
-// await workspaceManager.init();
-// this.workspaces.set(workspace.id, workspaceManager);
-// }
-
-// public async logout(): Promise<boolean> {
-// const { status } = await this.axios.delete(
-// `v1/accounts/logout/${this.account.deviceId}`,
-// );
-
-// if (status !== 200) {
-// return false;
-// }
-
-// if (fs.existsSync(this.accountPath)) {
-// fs.rm(this.accountPath, { recursive: true, force: true }, (err) => {
-// if (err) {
-// console.error('Error deleting account directory: ', err);
-// }
-// });
-// }
-
-// if (this.socket) {
-// this.socket.close();
-// }
-
-// return true;
-// }
-
-// public async executeEventLoop(): Promise<void> {
-// this.socket.checkConnection();
-// for (const workspace of this.workspaces.values()) {
-// if (!workspace.isSynced()) {
-// const synced = await workspace.sync();
-// if (synced) {
-// await this.database
-// .updateTable('workspaces')
-// .set({ synced: 1 })
-// .where('id', '=', workspace.getWorkspace().id)
-// .execute();
-// }
-// }
-// await workspace.sendMutations();
-// }
-// }
-// }
diff --git a/desktop/src/main/app-manager.ts b/desktop/src/main/app-manager.ts
deleted file mode 100644
index 82f3ec34..00000000
--- a/desktop/src/main/app-manager.ts
+++ /dev/null
@@ -1,383 +0,0 @@
-// import { app } from 'electron';
-// import {
-// CompiledQuery,
-// Kysely,
-// Migration,
-// Migrator,
-// QueryResult,
-// SqliteDialect,
-// } from 'kysely';
-// import { AccountManager } from '@/main/account-manager';
-// import { AppDatabaseSchema } from '@/main/schemas/app';
-// import { appDatabaseMigrations } from '@/main/migrations/app';
-// import { Account } from '@/types/accounts';
-// import {
-// buildSqlite,
-// extractTablesFromSql,
-// resultHasChanged,
-// } from '@/main/utils';
-// import { Workspace, WorkspaceRole } from '@/types/workspaces';
-// import { SubscribedQueryContext, SubscribedQueryResult } from '@/types/queries';
-// import { eventBus } from '@/lib/event-bus';
-// import { isEqual } from 'lodash';
-// import { Server } from '@/types/servers';
-
-// const EVENT_LOOP_INTERVAL = 1000;
-
-// class AppManager {
-// private readonly servers: Map<string, Server>;
-// private readonly accounts: Map<string, AccountManager>;
-// private readonly appPath: string;
-// private readonly database: Kysely<AppDatabaseSchema>;
-// private readonly subscribers: Map<string, SubscribedQueryResult<unknown>>;
-// private initPromise: Promise<void> | null = null;
-
-// constructor() {
-// this.servers = new Map<string, Server>();
-// this.accounts = new Map<string, AccountManager>();
-// this.appPath = app.getPath('userData');
-// this.subscribers = new Map();
-
-// const dialect = new SqliteDialect({
-// database: buildSqlite(`${this.appPath}/app.db`),
-// });
-
-// this.database = new Kysely<AppDatabaseSchema>({
-// dialect,
-// });
-
-// this.executeEventLoop = this.executeEventLoop.bind(this);
-// }
-
-// public async getAccount(
-// accountId: string,
-// ): Promise<AccountManager | undefined> {
-// await this.waitForInit();
-// return this.accounts.get(accountId);
-// }
-
-// public async init(): Promise<void> {
-// if (!this.initPromise) {
-// this.initPromise = this.executeInit();
-// }
-
-// await this.initPromise;
-// }
-
-// public async executeQuery<T>(
-// query: CompiledQuery<T>,
-// ): Promise<QueryResult<T>> {
-// await this.waitForInit();
-// const result = await this.database.executeQuery(query);
-
-// //only mutations should have side effects
-// if (result.numAffectedRows > 0) {
-// throw new Error('Query should not have any side effects');
-// }
-
-// return result;
-// }
-
-// public async executeQueryAndSubscribe<T>(
-// context: SubscribedQueryContext<T>,
-// ): Promise<QueryResult<T>> {
-// await this.waitForInit();
-// const result = await this.database.executeQuery(context.query);
-
-// // only mutations should have side effects
-// if (result.numAffectedRows > 0) {
-// throw new Error('Query should not have any side effects');
-// }
-
-// const queryId = context.key.join('|') + context.page;
-// const selectedTables = extractTablesFromSql(context.query.sql);
-// const subscriberData: SubscribedQueryResult<T> = {
-// context,
-// tables: selectedTables,
-// result: result,
-// };
-// this.subscribers.set(queryId, subscriberData);
-
-// return result;
-// }
-
-// public async executeMutation(
-// mutation: CompiledQuery | CompiledQuery[],
-// ): Promise<void> {
-// const affectedTables = new Set<string>();
-// let numAffectedRows = 0n;
-
-// if (Array.isArray(mutation)) {
-// // Execute multiple queries as a transaction
-// await this.database.transaction().execute(async (trx) => {
-// for (const query of mutation) {
-// const result = await trx.executeQuery(query);
-// if (result.numAffectedRows > 0n) {
-// numAffectedRows += result.numAffectedRows;
-// extractTablesFromSql(query.sql).forEach((table) =>
-// affectedTables.add(table),
-// );
-// }
-// }
-// });
-// } else {
-// // Execute single query
-// const result = await this.database.executeQuery(mutation);
-// numAffectedRows = result.numAffectedRows;
-// if (numAffectedRows > 0n) {
-// extractTablesFromSql(mutation.sql).forEach((table) =>
-// affectedTables.add(table),
-// );
-// }
-// }
-
-// if (numAffectedRows === 0n) {
-// return;
-// }
-
-// if (affectedTables.size === 0) {
-// return;
-// }
-
-// if (affectedTables.has('servers')) {
-// const servers = await this.getServers();
-// for (const server of servers) {
-// this.servers.set(server.domain, server);
-// }
-// }
-
-// if (affectedTables.has('accounts')) {
-// const accounts = await this.getAccounts();
-// for (const account of accounts) {
-// if (this.accounts.has(account.id)) {
-// continue;
-// }
-
-// const server = this.servers.get(account.server);
-// if (!server) {
-// continue;
-// }
-
-// const accountManager = new AccountManager(
-// server,
-// account,
-// this.appPath,
-// [],
-// this.database,
-// );
-
-// await accountManager.init();
-// this.accounts.set(account.id, accountManager);
-// }
-// }
-
-// if (affectedTables.has('workspaces')) {
-// const workspaces = await this.getWorkspaces();
-// for (const workspace of workspaces) {
-// const accountManager = this.accounts.get(workspace.accountId);
-// if (!accountManager) {
-// continue;
-// }
-
-// const workspaceManager = accountManager.getWorkspace(workspace.id);
-// if (!workspaceManager) {
-// await accountManager.addWorkspace(workspace);
-// }
-// }
-// }
-
-// for (const subscriber of this.subscribers.values()) {
-// const hasAffectedTables = subscriber.tables.some((table) =>
-// affectedTables.has(table),
-// );
-
-// if (!hasAffectedTables) {
-// continue;
-// }
-
-// const newResult = await this.database.executeQuery(
-// subscriber.context.query,
-// );
-
-// if (resultHasChanged(subscriber.result, newResult)) {
-// subscriber.result = newResult;
-// eventBus.publish({
-// event: 'app_query_updated',
-// payload: {
-// key: subscriber.context.key,
-// page: subscriber.context.page,
-// result: newResult,
-// },
-// });
-// }
-// }
-// }
-
-// public unsubscribeQuery(queryKey: string[]): void {
-// var queryIds = [...this.subscribers.keys()];
-// for (const queryId of queryIds) {
-// const subscriberData = this.subscribers.get(queryId);
-// if (isEqual(subscriberData.context.key, queryKey)) {
-// this.subscribers.delete(queryId);
-// }
-// }
-// }
-
-// public async logout(accountId: string): Promise<void> {
-// await this.waitForInit();
-// const accountManager = this.accounts.get(accountId);
-// if (!accountManager) {
-// throw new Error(`Account not found: ${accountId}`);
-// }
-
-// accountManager.account.status = 'logout';
-// const markAccountLoggedOutQuery = this.database
-// .updateTable('accounts')
-// .set({
-// status: 'logout',
-// })
-// .where('id', '=', accountId)
-// .compile();
-
-// await this.executeMutation(markAccountLoggedOutQuery);
-// }
-
-// private async waitForInit(): Promise<void> {
-// if (!this.initPromise) {
-// this.initPromise = this.executeInit();
-// }
-
-// await this.initPromise;
-// }
-
-// private async executeInit(): Promise<void> {
-// await this.migrate();
-
-// const servers = await this.getServers();
-// for (const server of servers) {
-// this.servers.set(server.domain, server);
-// }
-
-// const accounts = await this.getAccounts();
-// const workspaces = await this.getWorkspaces();
-// for (const account of accounts) {
-// const accountWorkspaces = workspaces.filter(
-// (workspace) => workspace.accountId === account.id,
-// );
-
-// const server = this.servers.get(account.server);
-// if (!server) {
-// continue;
-// }
-
-// const accountManager = new AccountManager(
-// server,
-// account,
-// this.appPath,
-// accountWorkspaces,
-// this.database,
-// );
-// await accountManager.init();
-
-// this.accounts.set(account.id, accountManager);
-// }
-
-// setTimeout(this.executeEventLoop, EVENT_LOOP_INTERVAL);
-// }
-
-// private async executeEventLoop(): Promise<void> {
-// try {
-// for (const accountManager of this.accounts.values()) {
-// if (accountManager.account.status === 'logout') {
-// const loggedOut = await accountManager.logout();
-// if (loggedOut) {
-// await this.database
-// .deleteFrom('accounts')
-// .where('id', '=', accountManager.account.id)
-// .execute();
-
-// this.accounts.delete(accountManager.account.id);
-// }
-// } else {
-// await accountManager.executeEventLoop();
-// }
-// }
-// } catch (error) {
-// // console.error('Error in event loop:', error);
-// }
-// setTimeout(this.executeEventLoop, EVENT_LOOP_INTERVAL);
-// }
-
-// private async migrate(): Promise<void> {
-// const migrator = new Migrator({
-// db: this.database,
-// provider: {
-// getMigrations(): Promise<Record<string, Migration>> {
-// return Promise.resolve(appDatabaseMigrations);
-// },
-// },
-// });
-
-// await migrator.migrateToLatest();
-// }
-
-// private async getServers(): Promise<Server[]> {
-// const servers = await this.database
-// .selectFrom('servers')
-// .selectAll()
-// .execute();
-
-// return servers.map((server) => ({
-// domain: server.domain,
-// name: server.name,
-// avatar: server.avatar,
-// attributes: JSON.parse(server.attributes),
-// version: server.version,
-// createdAt: new Date(server.created_at),
-// lastSyncedAt: server.last_synced_at
-// ? new Date(server.last_synced_at)
-// : null,
-// }));
-// }
-
-// private async getAccounts(): Promise<Account[]> {
-// const accounts = await this.database
-// .selectFrom('accounts')
-// .selectAll()
-// .execute();
-
-// return accounts.map((account) => ({
-// id: account.id,
-// name: account.name,
-// email: account.email,
-// avatar: account.avatar,
-// token: account.token,
-// deviceId: account.device_id,
-// server: account.server,
-// status: account.status,
-// }));
-// }
-
-// private async getWorkspaces(): Promise<Workspace[]> {
-// const workspaces = await this.database
-// .selectFrom('workspaces')
-// .selectAll()
-// .execute();
-
-// return workspaces.map(
-// (workspace): Workspace => ({
-// id: workspace.id,
-// accountId: workspace.account_id,
-// name: workspace.name,
-// description: workspace.description,
-// avatar: workspace.avatar,
-// versionId: workspace.version_id,
-// role: workspace.role as WorkspaceRole,
-// userId: workspace.user_id,
-// synced: workspace.synced === 1,
-// }),
-// );
-// }
-// }
-
-// export const appManager = new AppManager();
diff --git a/desktop/src/main/mediator.ts b/desktop/src/main/mediator.ts
index b670ce2f..c257f889 100644
--- a/desktop/src/main/mediator.ts
+++ b/desktop/src/main/mediator.ts
@@ -18,7 +18,7 @@ class Mediator {
private readonly subscribedQueries: Map<string, SubscribedQuery<QueryInput>> =
new Map();
- public async handleMutation<T extends MutationInput>(
+ public async executeMutation<T extends MutationInput>(
input: T,
): Promise<MutationMap[T['type']]['output']> {
const handler = mutationHandlerMap[input.type] as MutationHandler<T>;
@@ -31,7 +31,15 @@ class Mediator {
return result.output;
}
- public async handleQuery<T extends QueryInput>(
+ public async executeQuery<T extends QueryInput>(
+ input: T,
+ ): Promise<QueryMap[T['type']]['output']> {
+ const handler = queryHandlerMap[input.type] as QueryHandler<T>;
+ const result = await handler.handleQuery(input);
+ return result.output;
+ }
+
+ public async executeQueryAndSubscribe<T extends QueryInput>(
id: string,
input: T,
): Promise<QueryMap[T['type']]['output']> {
diff --git a/desktop/src/main/socket-manager.ts b/desktop/src/main/sockets/socket-connection.ts
similarity index 98%
rename from desktop/src/main/socket-manager.ts
rename to desktop/src/main/sockets/socket-connection.ts
index 4050a235..b110c516 100644
--- a/desktop/src/main/socket-manager.ts
+++ b/desktop/src/main/sockets/socket-connection.ts
@@ -9,7 +9,7 @@ export type SocketMessage = {
payload: any;
};
-export class SocketManager {
+export class SocketConnection {
private readonly server: Server;
private readonly account: Account;
private socket: WebSocket | null;
diff --git a/desktop/src/main/sockets/socket-manager.ts b/desktop/src/main/sockets/socket-manager.ts
new file mode 100644
index 00000000..f9b3245a
--- /dev/null
+++ b/desktop/src/main/sockets/socket-manager.ts
@@ -0,0 +1,75 @@
+import { SocketConnection } from '@/main/sockets/socket-connection';
+import { mediator } from '@/main/mediator';
+
+const EVENT_LOOP_INTERVAL = 5000;
+
+class SocketManager {
+ private readonly accounts: Map<string, SocketConnection> = new Map();
+ private initiated: boolean = false;
+
+ constructor() {
+ this.executeEventLoop = this.executeEventLoop.bind(this);
+ }
+
+ public init() {
+ if (this.initiated) {
+ return;
+ }
+
+ // for the first time we execute event loop almost instantly
+ setTimeout(this.executeEventLoop, 10);
+ this.initiated = true;
+ }
+
+ private async executeEventLoop() {
+ await this.checkAccounts();
+ this.checkConnections();
+
+ setTimeout(this.executeEventLoop, EVENT_LOOP_INTERVAL);
+ }
+
+ private async checkAccounts() {
+ const servers = await mediator.executeQuery({
+ type: 'server_list',
+ });
+
+ const accounts = await mediator.executeQuery({
+ type: 'account_list',
+ });
+
+ // Update accounts map
+ for (const account of accounts) {
+ const server = servers.find((s) => s.domain === account.server);
+ if (!server) {
+ this.accounts.delete(account.id);
+ continue;
+ }
+
+ if (this.accounts.has(account.id)) {
+ continue;
+ }
+
+ const connection = new SocketConnection(server, account);
+ connection.init();
+
+ this.accounts.set(account.id, connection);
+ }
+
+ // Remove logged out or missing accounts
+ for (const [accountId, connection] of this.accounts.entries()) {
+ const account = accounts.find((acc) => acc.id === accountId);
+ if (!account) {
+ connection.close();
+ this.accounts.delete(accountId);
+ }
+ }
+ }
+
+ private checkConnections() {
+ for (const connection of this.accounts.values()) {
+ connection.checkConnection();
+ }
+ }
+}
+
+export const socketManager = new SocketManager();
diff --git a/desktop/src/main/workspace-manager.ts b/desktop/src/main/workspace-manager.ts
deleted file mode 100644
index c9002d8c..00000000
--- a/desktop/src/main/workspace-manager.ts
+++ /dev/null
@@ -1,597 +0,0 @@
-import * as fs from 'node:fs';
-import {
- CompiledQuery,
- Kysely,
- Migration,
- Migrator,
- QueryResult,
- SqliteDialect,
-} from 'kysely';
-import { Workspace, WorkspaceSyncData } from '@/types/workspaces';
-import {
- SelectNodeCollaborator,
- WorkspaceDatabaseSchema,
-} from '@/main/data/workspace/schema';
-import { workspaceDatabaseMigrations } from '@/main/data/workspace/migrations';
-import {
- buildSqlite,
- extractTablesFromSql,
- resultHasChanged,
-} from '@/main/data/utils';
-import {
- ServerExecuteMutationsResponse,
- ServerMutation,
-} from '@/types/mutations';
-import { eventBus } from '@/lib/event-bus';
-import { AxiosInstance, isAxiosError } from 'axios';
-import { debounce, isEqual } from 'lodash';
-import {
- ServerNode,
- ServerNodeCollaborator,
- ServerNodeReaction,
-} from '@/types/nodes';
-import { SelectNode } from '@/main/data/workspace/schema';
-import { BackoffCalculator } from '@/lib/backoff-calculator';
-import * as Y from 'yjs';
-import { fromUint8Array, toUint8Array } from 'js-base64';
-
-export class WorkspaceManager {
- private readonly workspace: Workspace;
- private readonly axios: AxiosInstance;
- private readonly database: Kysely<WorkspaceDatabaseSchema>;
- private readonly backoffCalculator: BackoffCalculator;
- private readonly debouncedNotifyQuerySubscribers: (
- affectedTables: string[],
- ) => void;
-
- constructor(workspace: Workspace, axios: AxiosInstance, accountPath: string) {
- this.workspace = workspace;
- this.axios = axios;
- this.backoffCalculator = new BackoffCalculator();
-
- const workspaceDir = `${accountPath}/${workspace.id}`;
- if (!fs.existsSync(workspaceDir)) {
- fs.mkdirSync(workspaceDir);
- }
-
- const dialect = new SqliteDialect({
- database: buildSqlite(`${workspaceDir}/workspace.db`),
- });
-
- this.database = new Kysely<WorkspaceDatabaseSchema>({
- dialect,
- });
-
- this.debouncedNotifyQuerySubscribers = debounce(
- this.notifyQuerySubscribers,
- 100,
- );
- }
-
- public getWorkspace(): Workspace {
- return this.workspace;
- }
-
- public async init(): Promise<void> {
- await this.migrate();
- }
-
- public async executeQuery<T>(
- query: CompiledQuery<T>,
- ): Promise<QueryResult<T>> {
- const result = await this.database.executeQuery(query);
-
- //only mutations should have side effects
- if (result.numAffectedRows > 0) {
- throw new Error('Query should not have any side effects');
- }
-
- return result;
- }
-
- public async executeMutation(
- mutation: CompiledQuery | CompiledQuery[],
- ): Promise<void> {
- const affectedTables = new Set<string>();
- let numAffectedRows = 0n;
-
- if (Array.isArray(mutation)) {
- // Execute multiple queries as a transaction
- await this.database.transaction().execute(async (trx) => {
- for (const query of mutation) {
- const result = await trx.executeQuery(query);
- if (result.numAffectedRows > 0n) {
- numAffectedRows += result.numAffectedRows;
- extractTablesFromSql(query.sql).forEach((table) =>
- affectedTables.add(table),
- );
- }
- }
- });
- } else {
- // Execute single query
- const result = await this.database.executeQuery(mutation);
- numAffectedRows = result.numAffectedRows;
- if (numAffectedRows > 0n) {
- extractTablesFromSql(mutation.sql).forEach((table) =>
- affectedTables.add(table),
- );
- }
- }
-
- if (numAffectedRows === 0n) {
- return;
- }
-
- if (affectedTables.size > 0) {
- this.notifyQuerySubscribers(Array.from(affectedTables));
- }
- }
-
- private async notifyQuerySubscribers(
- affectedTables: string[],
- ): Promise<void> {
- // for (const subscriber of this.subscribers.values()) {
- // const hasAffectedTables = affectedTables.some((table) =>
- // subscriber.tables.includes(table),
- // );
- // if (!hasAffectedTables) {
- // continue;
- // }
- // const newResult = await this.database.executeQuery(
- // subscriber.context.query,
- // );
- // if (resultHasChanged(subscriber.result, newResult)) {
- // subscriber.result = newResult;
- // eventBus.publish({
- // event: 'workspace_query_updated',
- // payload: {
- // key: subscriber.context.key,
- // page: subscriber.context.page,
- // result: newResult,
- // },
- // });
- // }
- // }
- }
-
- public async sendMutations(): Promise<void> {
- if (!this.backoffCalculator.canRetry()) {
- return;
- }
-
- try {
- const mutations = await this.database
- .selectFrom('mutations')
- .selectAll()
- .orderBy('id asc')
- .limit(20)
- .execute();
-
- if (!mutations || mutations.length === 0) {
- return;
- }
-
- const { status, data } =
- await this.axios.post<ServerExecuteMutationsResponse>('v1/mutations', {
- workspaceId: this.workspace.id,
- mutations: mutations,
- });
-
- if (status !== 200) {
- return;
- }
-
- const executedMutationIds = data.results
- .filter((result) => result.status === 'success')
- .map((result) => result.id);
-
- const mutliFailureMutationIds = mutations
- .filter(
- (mutation) =>
- mutation.retry_count >= 5 &&
- !executedMutationIds.includes(mutation.id),
- )
- .map((mutation) => mutation.id);
-
- const toDeleteMutationIds = [
- ...executedMutationIds,
- ...mutliFailureMutationIds,
- ];
-
- await this.database
- .deleteFrom('mutations')
- .where('id', 'in', toDeleteMutationIds)
- .execute();
-
- const failedMutationIds = data.results
- .filter((result) => result.status === 'error')
- .map((result) => result.id);
-
- if (failedMutationIds.length > 0) {
- await this.database
- .updateTable('mutations')
- .set((eb) => ({ retry_count: eb('retry_count', '+', 1) }))
- .where('id', 'in', failedMutationIds)
- .execute();
- }
-
- this.backoffCalculator.reset();
- } catch (error) {
- if (isAxiosError(error)) {
- this.backoffCalculator.increaseError();
- }
- }
- }
-
- public async executeServerMutation(
- mutation: ServerMutation,
- ): Promise<boolean> {
- if (mutation.table === 'nodes') {
- return this.executeNodeServerMutation(mutation);
- } else if (mutation.table === 'node_collaborators') {
- return this.executeNodeCollaboratorServerMutation(mutation);
- } else if (mutation.table === 'node_reactions') {
- return this.executeNodeReactionServerMutation(mutation);
- }
-
- return false;
- }
-
- private async executeNodeServerMutation(
- mutation: ServerMutation,
- ): Promise<boolean> {
- if (mutation.action === 'insert' && mutation.after) {
- return this.createNodeFromServer(mutation.after);
- } else if (mutation.action === 'update' && mutation.after) {
- return this.updateNodeFromServer(mutation.after);
- } else if (mutation.action === 'delete' && mutation.before) {
- return this.deleteNodeFromServer(mutation.before);
- }
- }
-
- private async executeNodeCollaboratorServerMutation(
- mutation: ServerMutation,
- ): Promise<boolean> {
- if (mutation.action === 'insert' && mutation.after) {
- return this.syncNodeCollaboratorFromServer(mutation.after);
- } else if (mutation.action === 'update' && mutation.after) {
- return this.syncNodeCollaboratorFromServer(mutation.after);
- } else if (mutation.action === 'delete' && mutation.before) {
- await this.database
- .deleteFrom('node_collaborators')
- .where((eb) =>
- eb.and([
- eb('node_id', '=', mutation.before.node_id),
- eb('collaborator_id', '=', mutation.before.collaborator_id),
- ]),
- )
- .execute();
-
- this.debouncedNotifyQuerySubscribers(['node_collaborators']);
- return true;
- }
- }
-
- private async executeNodeReactionServerMutation(
- mutation: ServerMutation,
- ): Promise<boolean> {
- if (mutation.action === 'insert' && mutation.after) {
- return this.syncNodeReactionFromServer(mutation.after);
- } else if (mutation.action === 'delete' && mutation.before) {
- await this.database
- .deleteFrom('node_reactions')
- .where((eb) =>
- eb.and([
- eb('node_id', '=', mutation.before.node_id),
- eb('actor_id', '=', mutation.before.actor_id),
- eb('reaction', '=', mutation.before.reaction),
- ]),
- )
- .execute();
-
- this.debouncedNotifyQuerySubscribers(['node_reactions']);
- return true;
- }
- }
-
- public isSynced(): boolean {
- return this.workspace.synced;
- }
-
- public async sync(): Promise<boolean> {
- if (!this.backoffCalculator.canRetry()) {
- return false;
- }
-
- try {
- if (this.workspace.synced) {
- return true;
- }
-
- const { data, status } = await this.axios.get<WorkspaceSyncData>(
- `v1/${this.workspace.id}/sync`,
- );
-
- if (status !== 200) {
- return false;
- }
-
- if (data.nodes.length === 0 && data.nodeReactions.length === 0) {
- return true;
- }
-
- await this.database.transaction().execute(async (trx) => {
- if (data.nodes.length > 0) {
- await trx.deleteFrom('nodes').execute();
-
- await trx
- .insertInto('nodes')
- .values(
- data.nodes.map((node) => {
- return {
- id: node.id,
- attributes: JSON.stringify(node.attributes),
- state: node.state,
- created_at: node.createdAt,
- created_by: node.createdBy,
- updated_by: node.updatedBy,
- updated_at: node.updatedAt,
- version_id: node.versionId,
- server_created_at: node.serverCreatedAt,
- server_updated_at: node.serverUpdatedAt,
- server_version_id: node.versionId,
- };
- }),
- )
- .execute();
- }
-
- if (data.nodeReactions.length > 0) {
- await trx.deleteFrom('node_reactions').execute();
-
- await trx
- .insertInto('node_reactions')
- .values(
- data.nodeReactions.map((nodeReaction) => {
- return {
- node_id: nodeReaction.nodeId,
- actor_id: nodeReaction.actorId,
- reaction: nodeReaction.reaction,
- created_at: nodeReaction.createdAt,
- server_created_at: nodeReaction.serverCreatedAt,
- };
- }),
- )
- .execute();
- }
- });
-
- this.workspace.synced = true;
- this.backoffCalculator.reset();
- this.debouncedNotifyQuerySubscribers(['nodes', 'node_reactions']);
- return true;
- } catch (error) {
- if (isAxiosError(error)) {
- this.backoffCalculator.increaseError();
- }
- }
-
- return false;
- }
-
- private async createNodeFromServer(node: ServerNode): Promise<boolean> {
- await this.database
- .insertInto('nodes')
- .values({
- id: node.id,
- attributes: JSON.stringify(node.attributes),
- state: node.state,
- created_at: node.createdAt,
- created_by: node.createdBy,
- updated_by: node.updatedBy,
- updated_at: node.updatedAt,
- version_id: node.versionId,
- server_created_at: node.serverCreatedAt,
- server_updated_at: node.serverUpdatedAt,
- server_version_id: node.versionId,
- })
- .onConflict((cb) =>
- cb
- .doUpdateSet({
- server_created_at: node.serverCreatedAt,
- server_updated_at: node.serverUpdatedAt,
- server_version_id: node.versionId,
- })
- .where('version_id', '=', node.versionId),
- )
- .execute();
-
- this.debouncedNotifyQuerySubscribers(['nodes']);
- return true;
- }
-
- private async updateNodeFromServer(node: ServerNode): Promise<boolean> {
- const existingNode = await this.database
- .selectFrom('nodes')
- .selectAll()
- .where('id', '=', node.id)
- .executeTakeFirst();
-
- if (!this.shouldUpdateNodeFromServer(existingNode, node)) {
- return true;
- }
- const doc = new Y.Doc({
- guid: node.id,
- });
-
- Y.applyUpdate(doc, toUint8Array(existingNode.state));
- Y.applyUpdate(doc, toUint8Array(node.state));
-
- const attributesMap = doc.getMap('attributes');
- const attributes = JSON.stringify(attributesMap.toJSON());
- const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
-
- await this.database
- .updateTable('nodes')
- .set({
- attributes: attributes,
- state: encodedState,
- updated_at: node.updatedAt,
- updated_by: node.updatedBy,
- version_id: node.versionId,
- server_created_at: node.serverCreatedAt,
- server_updated_at: node.serverUpdatedAt,
- server_version_id: node.versionId,
- })
- .where('id', '=', node.id)
- .execute();
-
- this.debouncedNotifyQuerySubscribers(['nodes']);
- return true;
- }
-
- private async deleteNodeFromServer(node: ServerNode): Promise<boolean> {
- await this.database.deleteFrom('nodes').where('id', '=', node.id).execute();
- this.debouncedNotifyQuerySubscribers(['nodes']);
- return true;
- }
-
- public async syncNodeCollaboratorFromServer(
- nodeCollaborator: ServerNodeCollaborator,
- ): Promise<boolean> {
- const existingNodeCollaborator = await this.database
- .selectFrom('node_collaborators')
- .selectAll()
- .where((eb) =>
- eb.and([
- eb('node_id', '=', nodeCollaborator.nodeId),
- eb('collaborator_id', '=', nodeCollaborator.collaboratorId),
- ]),
- )
- .executeTakeFirst();
-
- if (!existingNodeCollaborator) {
- await this.database
- .insertInto('node_collaborators')
- .values({
- node_id: nodeCollaborator.nodeId,
- collaborator_id: nodeCollaborator.collaboratorId,
- role: nodeCollaborator.role,
- created_at: nodeCollaborator.createdAt,
- created_by: nodeCollaborator.createdBy,
- updated_by: nodeCollaborator.updatedBy,
- updated_at: nodeCollaborator.updatedAt,
- version_id: nodeCollaborator.versionId,
- server_created_at: nodeCollaborator.serverCreatedAt,
- server_updated_at: nodeCollaborator.serverUpdatedAt,
- server_version_id: nodeCollaborator.versionId,
- })
- .execute();
-
- this.debouncedNotifyQuerySubscribers(['node_collaborators']);
- return true;
- }
-
- if (
- this.shouldUpdateNodeCollaboratorFromServer(
- existingNodeCollaborator,
- nodeCollaborator,
- )
- ) {
- await this.database
- .updateTable('node_collaborators')
- .set({
- role: nodeCollaborator.role,
- updated_at: nodeCollaborator.updatedAt,
- updated_by: nodeCollaborator.updatedBy,
- version_id: nodeCollaborator.versionId,
- server_created_at: nodeCollaborator.serverCreatedAt,
- server_updated_at: nodeCollaborator.serverUpdatedAt,
- server_version_id: nodeCollaborator.versionId,
- })
- .where((eb) =>
- eb.and([
- eb('node_id', '=', nodeCollaborator.nodeId),
- eb('collaborator_id', '=', nodeCollaborator.collaboratorId),
- ]),
- )
- .execute();
-
- this.debouncedNotifyQuerySubscribers(['node_collaborators']);
- return true;
- }
- }
-
- private async syncNodeReactionFromServer(
- nodeReaction: ServerNodeReaction,
- ): Promise<boolean> {
- await this.database
- .insertInto('node_reactions')
- .values({
- node_id: nodeReaction.nodeId,
- actor_id: nodeReaction.actorId,
- reaction: nodeReaction.reaction,
- created_at: nodeReaction.createdAt,
- server_created_at: nodeReaction.serverCreatedAt,
- })
- .onConflict((ob) =>
- ob.doUpdateSet({
- server_created_at: nodeReaction.serverCreatedAt,
- }),
- )
- .execute();
-
- this.debouncedNotifyQuerySubscribers(['node_reactions']);
- return true;
- }
-
- public shouldUpdateNodeFromServer(
- localNode: SelectNode,
- serverNode: ServerNode,
- ): boolean {
- if (localNode.server_version_id === serverNode.versionId) {
- return false;
- }
-
- return true;
- }
-
- public shouldUpdateNodeCollaboratorFromServer(
- localNodeCollaborator: SelectNodeCollaborator,
- serverNodeCollaborator: ServerNodeCollaborator,
- ): boolean {
- if (
- localNodeCollaborator.server_version_id ===
- serverNodeCollaborator.versionId
- ) {
- return false;
- }
-
- if (localNodeCollaborator.updated_at) {
- if (!serverNodeCollaborator.updatedAt) {
- return false;
- }
- const localUpdatedAt = new Date(localNodeCollaborator.updated_at);
- const serverUpdatedAt = new Date(serverNodeCollaborator.updatedAt);
- if (localUpdatedAt > serverUpdatedAt) {
- return false;
- }
- }
-
- return true;
- }
-
- private async migrate(): Promise<void> {
- const migrator = new Migrator({
- db: this.database,
- provider: {
- getMigrations(): Promise<Record<string, Migration>> {
- return Promise.resolve(workspaceDatabaseMigrations);
- },
- },
- });
-
- await migrator.migrateToLatest();
- }
-}
diff --git a/desktop/src/preload.ts b/desktop/src/preload.ts
index b05dc4c3..9ac306ee 100644
--- a/desktop/src/preload.ts
+++ b/desktop/src/preload.ts
@@ -14,10 +14,16 @@ contextBridge.exposeInMainWorld('neuron', {
},
executeQuery: <T extends QueryInput>(
+ input: T,
+ ): Promise<QueryMap[T['type']]['output']> => {
+ return ipcRenderer.invoke('execute-query', input);
+ },
+
+ executeQueryAndSubscribe: <T extends QueryInput>(
id: string,
input: T,
): Promise<QueryMap[T['type']]['output']> => {
- return ipcRenderer.invoke('execute-query', id, input);
+ return ipcRenderer.invoke('execute-query-and-subscribe', id, input);
},
unsubscribeQuery: (id: string): Promise<void> => {
diff --git a/desktop/src/renderer/hooks/use-infinite-query.tsx b/desktop/src/renderer/hooks/use-infinite-query.tsx
index 11e34732..b06156ad 100644
--- a/desktop/src/renderer/hooks/use-infinite-query.tsx
+++ b/desktop/src/renderer/hooks/use-infinite-query.tsx
@@ -65,7 +65,10 @@ export const useInfiniteQuery = <T extends QueryInput>({
const queryId = NeuronId.generate(NeuronId.Type.Query);
try {
- const result = await window.neuron.executeQuery(queryId, input);
+ const result = await window.neuron.executeQueryAndSubscribe(
+ queryId,
+ input,
+ );
// Update pages state
setPages((prevPages) => {
diff --git a/desktop/src/renderer/hooks/use-query.tsx b/desktop/src/renderer/hooks/use-query.tsx
index 97c06f0e..48ad14f5 100644
--- a/desktop/src/renderer/hooks/use-query.tsx
+++ b/desktop/src/renderer/hooks/use-query.tsx
@@ -19,7 +19,10 @@ export const useQuery = <T extends QueryInput>(input: T) => {
const queryId = NeuronId.generate(NeuronId.Type.Query);
const fetchData = async () => {
try {
- const result = await window.neuron.executeQuery(queryId, input);
+ const result = await window.neuron.executeQueryAndSubscribe(
+ queryId,
+ input,
+ );
setData(result);
} catch (error) {
console.error('Error executing query:', error);
diff --git a/desktop/src/types/window.d.ts b/desktop/src/types/window.d.ts
index abedbc22..3e331305 100644
--- a/desktop/src/types/window.d.ts
+++ b/desktop/src/types/window.d.ts
@@ -14,6 +14,10 @@ interface NeuronApi {
) => Promise<MutationMap[T['type']]['output']>;
executeQuery: <T extends QueryInput>(
+ input: T,
+ ) => Promise<QueryMap[T['type']]['output']>;
+
+ executeQueryAndSubscribe: <T extends QueryInput>(
id: string,
input: T,
) => Promise<QueryMap[T['type']]['output']>;