Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(core): skip command execution after disposal (#6898)

白熱28fcb8d76643
diff --git a/packages/core/src/services/command/__tests__/command.service.spec.ts b/packages/core/src/services/command/__tests__/command.service.spec.ts
index fda92c5534..bacdea26a7 100644
--- a/packages/core/src/services/command/__tests__/command.service.spec.ts
+++ b/packages/core/src/services/command/__tests__/command.service.spec.ts
@@ -34,6 +34,7 @@ const anotherCommandID = 'another-command';
describe('Test CommandService', () => {
let injector: Injector;
let commandService: ICommandService;
+ let logService: ILogService;
beforeEach(() => {
injector = new Injector();
@@ -43,6 +44,7 @@ describe('Test CommandService', () => {
injector.add([IConfigService, { useClass: ConfigService }]);
commandService = injector.get(ICommandService);
+ logService = injector.get(ILogService);
commandService.registerCommand({
id: commandID,
type: CommandType.COMMAND,
@@ -202,6 +204,58 @@ describe('Test CommandService', () => {
commandService.syncExecuteCommand(pushValCommandID);
expect(numbers).toEqual([-1, 0, 1, -1, 0, 1, 0]);
});
+
+ it('Should skip command execution after the command service is disposed', async () => {
+ const handler = vi.fn(() => true);
+ const beforeListener = vi.fn();
+ const listener = vi.fn();
+ const warn = vi.spyOn(logService, 'warn');
+ const pushValCommandID = 'push-val-after-dispose';
+ commandService.registerCommand({
+ id: pushValCommandID,
+ type: CommandType.COMMAND,
+ handler,
+ });
+ commandService.beforeCommandExecuted(beforeListener);
+ commandService.onCommandExecuted(listener);
+
+ injector.dispose();
+
+ expect(commandService.syncExecuteCommand(pushValCommandID)).toBe(false);
+ await expect(commandService.executeCommand(pushValCommandID)).resolves.toBe(false);
+ expect(handler).not.toHaveBeenCalled();
+ expect(beforeListener).not.toHaveBeenCalled();
+ expect(listener).not.toHaveBeenCalled();
+ expect(warn).toHaveBeenCalledTimes(2);
+ expect(warn).toHaveBeenCalledWith(
+ '[CommandService]',
+ `command "${pushValCommandID}" skipped because CommandService is disposed.`
+ );
+ });
+
+ it('Should stop before invoking command handler when disposed by a before hook', async () => {
+ const handler = vi.fn(() => true);
+ const listener = vi.fn();
+ const warn = vi.spyOn(logService, 'warn');
+ const beforeListener = vi.fn(() => (commandService as CommandService).dispose());
+ const pushValCommandID = 'push-val-dispose-before-hook';
+ commandService.registerCommand({
+ id: pushValCommandID,
+ type: CommandType.COMMAND,
+ handler,
+ });
+ commandService.beforeCommandExecuted(beforeListener);
+ commandService.onCommandExecuted(listener);
+
+ expect(await commandService.executeCommand(pushValCommandID)).toBe(false);
+ expect(beforeListener).toHaveBeenCalledTimes(1);
+ expect(handler).not.toHaveBeenCalled();
+ expect(listener).not.toHaveBeenCalled();
+ expect(warn).toHaveBeenCalledWith(
+ '[CommandService]',
+ `command "${pushValCommandID}" skipped because CommandService is disposed.`
+ );
+ });
});
describe('Test MultiCommand', () => {
diff --git a/packages/core/src/services/command/command.service.ts b/packages/core/src/services/command/command.service.ts
index 3c2c855a9f..fada1c2f36 100644
--- a/packages/core/src/services/command/command.service.ts
+++ b/packages/core/src/services/command/command.service.ts
@@ -354,6 +354,10 @@ export class CommandService extends Disposable implements ICommandService {
return this._disposed;
}
+ private _warnCommandSkippedAfterDisposed(id: string): void {
+ this._logService.warn('[CommandService]', `command "${id}" skipped because CommandService is disposed.`);
+ }
+
hasCommand(commandId: string): boolean {
return this._commandRegistry.hasCommand(commandId);
}
@@ -415,6 +419,11 @@ export class CommandService extends Disposable implements ICommandService {
params?: P,
options?: IExecutionOptions
): Promise<R> {
+ if (this._disposed) {
+ this._warnCommandSkippedAfterDisposed(id);
+ return false as R;
+ }
+
try {
const item = this._commandRegistry.getCommand(id);
if (item) {
@@ -429,6 +438,12 @@ export class CommandService extends Disposable implements ICommandService {
const _options = options ?? {};
this._beforeCommandExecutionListeners.forEach((listener) => listener(commandInfo, _options));
+ if (this._disposed) {
+ stackItemDisposable.dispose();
+ this._warnCommandSkippedAfterDisposed(id);
+ return false as R;
+ }
+
const result = await this._execute<P, R>(command as ICommand<P, R>, params, _options);
// For syncOnly mutations, only call collab listeners, not regular listeners
if (_options.syncOnly) {
@@ -463,6 +478,11 @@ export class CommandService extends Disposable implements ICommandService {
params?: P | undefined,
options?: IExecutionOptions
): R {
+ if (this._disposed) {
+ this._warnCommandSkippedAfterDisposed(id);
+ return false as R;
+ }
+
try {
const item = this._commandRegistry.getCommand(id);
if (item) {
@@ -490,6 +510,12 @@ export class CommandService extends Disposable implements ICommandService {
const _options = options ?? {};
this._beforeCommandExecutionListeners.forEach((listener) => listener(commandInfo, _options));
+ if (this._disposed) {
+ stackItemDisposable.dispose();
+ this._warnCommandSkippedAfterDisposed(id);
+ return false as R;
+ }
+
const result = this._syncExecute<P, R>(command as ICommand<P, R>, params, _options);
// For syncOnly mutations, only call collab listeners, not regular listeners
if (_options.syncOnly) {
diff --git a/packages/engine-formula/src/services/__tests__/register-other-formula.service.spec.ts b/packages/engine-formula/src/services/__tests__/register-other-formula.service.spec.ts
index 18be3c89b5..26a232b551 100644
--- a/packages/engine-formula/src/services/__tests__/register-other-formula.service.spec.ts
+++ b/packages/engine-formula/src/services/__tests__/register-other-formula.service.spec.ts
@@ -27,10 +27,13 @@ interface ICommandServiceMock {
executeCommand: ReturnType<typeof vi.fn>;
onCommandExecuted: (callback: (commandInfo: ICommandInfo) => void) => { dispose: () => void };
emit: (id: string, params?: unknown) => void;
+ disposed: () => boolean;
+ dispose: () => void;
}
function createCommandServiceMock(): ICommandServiceMock {
const callbacks = new Set<(commandInfo: ICommandInfo) => void>();
+ let disposed = false;
return {
executeCommand: vi.fn(async (id: string, params?: unknown) => {
callbacks.forEach((callback) => callback({ id, params } as ICommandInfo));
@@ -45,6 +48,11 @@ function createCommandServiceMock(): ICommandServiceMock {
emit: (id: string, params?: unknown) => {
callbacks.forEach((callback) => callback({ id, params } as ICommandInfo));
},
+ disposed: () => disposed,
+ dispose: () => {
+ disposed = true;
+ callbacks.clear();
+ },
};
}
@@ -133,6 +141,34 @@ describe('RegisterOtherFormulaService', () => {
);
});
+ it('should not mark formula dirty after disposed while formula registration is pending', async () => {
+ const commandService = createCommandServiceMock();
+ const activeDirtyManagerService = { register: vi.fn() } as unknown as IActiveDirtyManagerService;
+ const service = new RegisterOtherFormulaService(
+ commandService as never,
+ activeDirtyManagerService,
+ {} as LifecycleService
+ );
+ let resolveSetOtherFormula!: () => void;
+
+ commandService.executeCommand.mockImplementationOnce(() => new Promise<boolean>((resolve) => {
+ resolveSetOtherFormula = () => resolve(true);
+ }));
+
+ service.calculateStarted$.next(true);
+ service.registerFormulaWithRange('unit-1', 'sheet-1', '=A1');
+
+ expect(commandService.executeCommand).toHaveBeenCalledTimes(1);
+ service.dispose();
+ commandService.dispose();
+
+ resolveSetOtherFormula();
+ await Promise.resolve();
+ await Promise.resolve();
+
+ expect(commandService.executeCommand).toHaveBeenCalledTimes(1);
+ });
+
it('should not recursively reopen buffer when calculation starts', () => {
const commandService = createCommandServiceMock();
const activeDirtyManagerService = { register: vi.fn() } as unknown as IActiveDirtyManagerService;
diff --git a/packages/engine-formula/src/services/register-other-formula.service.ts b/packages/engine-formula/src/services/register-other-formula.service.ts
index 43735578ee..4bf32d290f 100644
--- a/packages/engine-formula/src/services/register-other-formula.service.ts
+++ b/packages/engine-formula/src/services/register-other-formula.service.ts
@@ -123,6 +123,10 @@ export class RegisterOtherFormulaService extends Disposable {
};
this._commandService.executeCommand(SetOtherFormulaMutation.id, params, { onlyLocal: true }).then(() => {
+ if (this._disposed) {
+ return;
+ }
+
this._commandService.executeCommand(
OtherFormulaMarkDirty.id,
{ [unitId]: { [subUnitId]: { [formulaId]: true } } },