Skip to content

Commit

Permalink
fix(rpc): skip missing mutations in remote replica (#1826)
Browse files Browse the repository at this point in the history
  • Loading branch information
wzhudev committed Apr 8, 2024
1 parent fc4cc4c commit 1e10cbf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
27 changes: 21 additions & 6 deletions packages/core/src/services/command/command.service.ts
Expand Up @@ -131,6 +131,13 @@ export interface IExecutionOptions {
export type CommandListener = (commandInfo: Readonly<ICommandInfo>, options?: IExecutionOptions) => void;

export interface ICommandService {
/**
* Check if a command is already registered at the current command service.
*
* @param commandId The id of the command.
*/
hasCommand(commandId: string): boolean;

registerCommand(command: ICommand<object, unknown>): IDisposable;

registerMultipleCommand(command: ICommand<object, unknown>): IDisposable;
Expand Down Expand Up @@ -177,6 +184,10 @@ export class CommandRegistry {
});
}

hasCommand(id: string): boolean {
return this._commands.has(id);
}

getCommand(id: string): [ICommand] | null {
if (!this._commands.has(id)) {
return null;
Expand All @@ -188,6 +199,12 @@ export class CommandRegistry {

interface ICommandExecutionStackItem extends ICommandInfo {}

export const NilCommand: ICommand = {
id: 'nil',
type: CommandType.COMMAND,
handler: () => true,
};

export class CommandService implements ICommandService {
private readonly _commandRegistry: CommandRegistry;

Expand All @@ -208,6 +225,10 @@ export class CommandService implements ICommandService {
this._registerCommand(NilCommand);
}

hasCommand(commandId: string): boolean {
return this._commandRegistry.hasCommand(commandId);
}

registerCommand(command: ICommand): IDisposable {
return this._registerCommand(command);
}
Expand Down Expand Up @@ -467,9 +488,3 @@ export function sequenceExecuteAsync(
const promises = tasks.map((task) => () => commandService.executeCommand(task.id, task.params, options));
return sequenceAsync(promises);
}

export const NilCommand: ICommand = {
id: 'nil',
type: CommandType.COMMAND,
handler: () => true,
};
Expand Up @@ -15,7 +15,7 @@
*/

import type { IExecutionOptions, IMutationInfo, IWorkbookData } from '@univerjs/core';
import { ICommandService, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
import { ICommandService, ILogService, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
import { createIdentifier } from '@wendellhu/redi';

export interface IRemoteSyncMutationOptions extends IExecutionOptions {
Expand Down Expand Up @@ -63,15 +63,22 @@ export interface IRemoteInstanceService {
export class RemoteInstanceReplicaService implements IRemoteInstanceService {
constructor(
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
@ICommandService private readonly _commandService: ICommandService
@ICommandService private readonly _commandService: ICommandService,
@ILogService private readonly _logService: ILogService
) {}

whenReady(): Promise<true> {
return Promise.resolve(true);
}

async syncMutation(params: { mutationInfo: IMutationInfo }): Promise<boolean> {
return this._commandService.syncExecuteCommand(params.mutationInfo.id, params.mutationInfo.params, {
const { id, params: mutationParams } = params.mutationInfo;
if (!this._commandService.hasCommand(id)) {
this._logService.debug('[RemoteInstanceReplicaService]', `command "${id}" not found. Skip sync mutation.`);
return true;
}

return this._commandService.syncExecuteCommand(id, mutationParams, {
onlyLocal: true,
fromSync: true,
});
Expand Down

0 comments on commit 1e10cbf

Please sign in to comment.