mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 04:18:54 +00:00
fix(editor): cleanup transformer middleware slot subscriptions (#12630)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Improved resource management by introducing explicit cleanup for various middleware components, ensuring that resources are properly released when no longer needed. - **Refactor** - Updated middleware logic to support cleanup functions, enhancing the stability and performance of the application by preventing potential memory leaks. - **Chores** - Enhanced lifecycle management in core systems to automatically dispose of resources when appropriate. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
@@ -113,6 +113,8 @@ type TransformerMiddlewareOptions = {
|
||||
transformerConfigs: Map<string, unknown>;
|
||||
};
|
||||
|
||||
type TransformerMiddlewareCleanup = () => void;
|
||||
|
||||
export type TransformerMiddleware = (
|
||||
options: TransformerMiddlewareOptions
|
||||
) => void;
|
||||
) => void | TransformerMiddlewareCleanup;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { DisposableGroup } from '@blocksuite/global/disposable';
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
import { nextTick } from '@blocksuite/global/utils';
|
||||
import { Subject } from 'rxjs';
|
||||
@@ -67,6 +68,8 @@ export class Transformer {
|
||||
|
||||
private readonly _docCRUD: DocCRUD;
|
||||
|
||||
private readonly _disposables: DisposableGroup = new DisposableGroup();
|
||||
|
||||
private readonly _slots: TransformerSlots = {
|
||||
beforeImport: new Subject<BeforeImportPayload>(),
|
||||
afterImport: new Subject<AfterImportPayload>(),
|
||||
@@ -366,13 +369,16 @@ export class Transformer {
|
||||
this._docCRUD = docCRUD;
|
||||
|
||||
middlewares.forEach(middleware => {
|
||||
middleware({
|
||||
const cleanup = middleware({
|
||||
slots: this._slots,
|
||||
docCRUD: this._docCRUD,
|
||||
assetsManager: this._assetsManager,
|
||||
adapterConfigs: this._adapterConfigs,
|
||||
transformerConfigs: this._transformerConfigs,
|
||||
});
|
||||
if (cleanup) {
|
||||
this._disposables.add(cleanup);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -646,4 +652,9 @@ export class Transformer {
|
||||
reset() {
|
||||
this._assetsManager.cleanup();
|
||||
}
|
||||
|
||||
[Symbol.dispose]() {
|
||||
this._disposables.dispose();
|
||||
this._assetsManager.cleanup();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user