mirror of
https://github.com/colanode/colanode.git
synced 2025-12-25 16:09:31 +01:00
Remove synchronizers on logout or workspace delete
This commit is contained in:
@@ -14,6 +14,7 @@ import { FileSynchronizer } from '@/main/synchronizers/files';
|
||||
import { EntryInteractionSynchronizer } from '@/main/synchronizers/entry-interactions';
|
||||
import { FileInteractionSynchronizer } from '@/main/synchronizers/file-interactions';
|
||||
import { MessageInteractionSynchronizer } from '@/main/synchronizers/message-interactions';
|
||||
import { eventBus } from '@/shared/lib/event-bus';
|
||||
|
||||
class SyncService {
|
||||
private readonly debug = createDebugger('service:sync');
|
||||
@@ -22,6 +23,18 @@ class SyncService {
|
||||
BaseSynchronizer<SynchronizerInput>
|
||||
> = new Map();
|
||||
|
||||
constructor() {
|
||||
eventBus.subscribe((event) => {
|
||||
if (event.type === 'workspace_deleted') {
|
||||
this.removeWorkspaceSynchronizers(event.workspace.userId);
|
||||
} else if (event.type === 'account_deleted') {
|
||||
this.removeAccountSynchronizers(event.account.id);
|
||||
} else if (event.type === 'collaboration_deleted') {
|
||||
this.removeRootNodeSynchronizers(event.userId, event.entryId);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async initSynchronizers(userId: string) {
|
||||
const workspace = await databaseService.appDatabase
|
||||
.selectFrom('workspaces')
|
||||
@@ -298,6 +311,36 @@ class SyncService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private removeWorkspaceSynchronizers(userId: string) {
|
||||
const keys = Array.from(this.synchronizers.keys());
|
||||
|
||||
for (const key of keys) {
|
||||
const synchronizer = this.synchronizers.get(key);
|
||||
if (!synchronizer) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (synchronizer.userId === userId) {
|
||||
this.synchronizers.delete(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private removeAccountSynchronizers(accountId: string) {
|
||||
const keys = Array.from(this.synchronizers.keys());
|
||||
|
||||
for (const key of keys) {
|
||||
const synchronizer = this.synchronizers.get(key);
|
||||
if (!synchronizer) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (synchronizer.accountId === accountId) {
|
||||
this.synchronizers.delete(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const syncService = new SyncService();
|
||||
|
||||
Reference in New Issue
Block a user