Skip to content

Commit

Permalink
fix(infra): memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
EYHN committed Dec 4, 2024
1 parent 4b52212 commit f94ebcc
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 26 deletions.
17 changes: 13 additions & 4 deletions packages/common/infra/src/framework/core/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ export class EventBus {
this.listeners[event.id].push(listener);
const off = this.parent?.on(event, listener);
return () => {
this.off(event, listener);
off?.();
this.off(event.id, listener);
};
}

off<T>(event: FrameworkEvent<T>, listener: (event: T) => void) {
if (!this.listeners[event.id]) {
private off(eventId: string, listener: (event: any) => void) {
if (!this.listeners[eventId]) {
return;
}
this.listeners[event.id] = this.listeners[event.id].filter(
this.listeners[eventId] = this.listeners[eventId].filter(
l => l !== listener
);
}
Expand All @@ -76,6 +76,15 @@ export class EventBus {
}
});
}

dispose(): void {
for (const eventId of Object.keys(this.listeners)) {
for (const listener of this.listeners[eventId]) {
this.parent?.off(eventId, listener);
}
}
this.listeners = {};
}
}

interface EventHandler {
Expand Down
2 changes: 2 additions & 0 deletions packages/common/infra/src/framework/core/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export class ComponentCachePool {
}
}
}
this.cache.clear();
}

[Symbol.dispose]() {
Expand Down Expand Up @@ -318,5 +319,6 @@ export class BasicFrameworkProvider extends FrameworkProvider {
}
this.disposed = true;
this.cache.dispose();
this.eventBus.dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,9 @@ export class Workspace extends Entity {
}),
undefined
);

override dispose(): void {
this.docCollection.awarenessStore.destroy();
(this.docCollection.awarenessStore.awareness as Awareness).destroy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class WorkspaceRepositoryService extends Service {
return {
workspace,
dispose: () => {
workspace.dispose();
workspace.scope.dispose();
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ export class WorkspaceService extends Service {
}
return this._workspace;
}

override dispose(): void {
this._workspace?.dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class TestingWorkspaceLocalProvider implements WorkspaceFlavourProvider {

applyUpdate(bs.doc, data);

bs.awarenessStore.awareness.destroy();

return {
name: bs.meta.name,
avatar: bs.meta.avatar,
Expand Down
4 changes: 3 additions & 1 deletion packages/common/infra/src/sync/awareness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export interface AwarenessConnection {
}

export class AwarenessEngine {
constructor(public readonly connections: AwarenessConnection[]) {}
constructor(public readonly connections: AwarenessConnection[]) {
console.log('AwarenessEngine', connections);
}

connect(awareness: Awareness) {
this.connections.forEach(connection => connection.connect(awareness));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export async function markDownToDoc(
const collection = new DocCollection({
schema,
});
collection.awarenessStore.awareness.destroy();
collection.meta.initialize();
const middlewares = [defaultImageProxyMiddleware];
if (additionalMiddlewares) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const WorkspaceList = ({
const toggleWorkspace = useCallback(
(id: string) => {
if (id !== currentWorkspace.id) {
jumpToPage(id, 'all');
jumpToPage(id, 'home');
}
onClose?.();
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,26 @@ export class DocsIndexer extends Entity {
}

setupListener() {
this.workspaceEngine.doc.storage.eventBus.on(event => {
if (WorkspaceDBService.isDBDocId(event.docId)) {
// skip db doc
return;
}
if (event.clientId === this.workspaceEngine.doc.clientId) {
this.jobQueue
.enqueue([
{
batchKey: event.docId,
payload: { storageDocId: event.docId },
},
])
.catch(err => {
console.error('Error enqueueing job', err);
});
}
});
this.disposables.push(
this.workspaceEngine.doc.storage.eventBus.on(event => {
if (WorkspaceDBService.isDBDocId(event.docId)) {
// skip db doc
return;
}
if (event.clientId === this.workspaceEngine.doc.clientId) {
this.jobQueue
.enqueue([
{
batchKey: event.docId,
payload: { storageDocId: event.docId },
},
])
.catch(err => {
console.error('Error enqueueing job', err);
});
}
})
);
}

async execJob(jobs: Job<IndexerJobPayload>[], signal: AbortSignal) {
Expand Down Expand Up @@ -298,6 +300,8 @@ export class DocsIndexer extends Entity {
}

override dispose(): void {
super.dispose();
this.runner.stop();
this.worker?.dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ export async function createWorker(abort: AbortSignal) {
});
},
dispose: () => {
worker.terminate();
terminateAbort.abort(MANUALLY_STOP);
worker.terminate();
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ class CloudWorkspaceFlavourProvider implements WorkspaceFlavourProvider {
id,
schema: getAFFiNEWorkspaceSchema(),
});
bs.awarenessStore.awareness.destroy();

if (localData) applyUpdate(bs.doc, localData);
if (cloudData) applyUpdate(bs.doc, cloudData.data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class LocalWorkspaceFlavourProvider implements WorkspaceFlavourProvider {
id,
schema: getAFFiNEWorkspaceSchema(),
});
bs.awarenessStore.awareness.destroy();

if (localData) applyUpdate(bs.doc, localData);

Expand Down

0 comments on commit f94ebcc

Please sign in to comment.