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.

chore: add delayed replay commands (#3485)

Wenzhao Hudebc143f6bd3
diff --git a/packages-experimental/action-recorder/src/services/replay.service.ts b/packages-experimental/action-recorder/src/services/replay.service.ts
index c9543073c0..9545dd95e6 100644
--- a/packages-experimental/action-recorder/src/services/replay.service.ts
+++ b/packages-experimental/action-recorder/src/services/replay.service.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import { Disposable, ICommandService, ILogService, IUniverInstanceService } from '@univerjs/core';
+import { awaitTime, Disposable, ICommandService, ILogService, IUniverInstanceService } from '@univerjs/core';
import { MessageType } from '@univerjs/design';
import { ILocalFileService, IMessageService } from '@univerjs/ui';
import type { ICommandInfo } from '@univerjs/core';
@@ -81,8 +81,42 @@ export class ActionReplayService extends Disposable {
return true;
}
+
+ /**
+ * Replay a list of commands with a random delay between each command.
+ * @param commands - The commands to replay.
+ */
+ async replayCommandsWithDelay(commands: ICommandInfo[]): Promise<boolean> {
+ const focusedUnitId = this._instanceService.getFocusedUnit()?.getUnitId();
+ if (!focusedUnitId) {
+ this._logService.error('[ReplayService]', 'no focused unit to replay commands');
+ }
+
+ for (const command of commands) {
+ await awaitTime(randomBetween200And1k());
+
+ const { id, params } = command;
+ if (params) {
+ if (typeof (params as ISharedCommandParams).unitId !== 'undefined') {
+ (params as ISharedCommandParams).unitId = focusedUnitId;
+ }
+
+ const result = await this._commandService.executeCommand(id, params);
+ if (!result) return false;
+ } else {
+ const result = await this._commandService.executeCommand(id);
+ if (!result) return false;
+ }
+ }
+
+ return true;
+ }
}
interface ISharedCommandParams {
unitId?: string;
}
+
+function randomBetween200And1k() {
+ return Math.floor(Math.random() * 800) + 200;
+}
diff --git a/packages-experimental/debugger/src/controllers/e2e/e2e-memory.controller.ts b/packages-experimental/debugger/src/controllers/e2e/e2e-memory.controller.ts
index 38c88c2527..436f6688b0 100644
--- a/packages-experimental/debugger/src/controllers/e2e/e2e-memory.controller.ts
+++ b/packages-experimental/debugger/src/controllers/e2e/e2e-memory.controller.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import { Disposable, ICommandService, IUniverInstanceService, LifecycleStages, OnLifecycle, UniverInstanceType } from '@univerjs/core';
+import { awaitTime, Disposable, ICommandService, IUniverInstanceService, LifecycleStages, OnLifecycle, UniverInstanceType } from '@univerjs/core';
import { DisposeUniverCommand } from '../../commands/commands/unit.command';
import { getDefaultDocData } from './data/default-doc';
import { getDefaultWorkbookData } from './data/default-sheet';
@@ -74,20 +74,20 @@ export class E2EMemoryController extends Disposable {
snapshot.id = unitId;
this._univerInstanceService.createUnit(UniverInstanceType.UNIVER_SHEET, snapshot);
- await timer(loadingTimeout);
+ await awaitTime(loadingTimeout);
this._univerInstanceService.disposeUnit(unitId);
- await timer(disposingTimeout);
+ await awaitTime(disposingTimeout);
}
private async _loadDefaultSheet(loadingTimeout: number = AWAIT_LOADING_TIMEOUT): Promise<void> {
this._univerInstanceService.createUnit(UniverInstanceType.UNIVER_SHEET, getDefaultWorkbookData());
- await timer(loadingTimeout);
+ await awaitTime(loadingTimeout);
}
private async _loadDefaultDoc(loadingTimeout: number = AWAIT_LOADING_TIMEOUT): Promise<void> {
this._univerInstanceService.createUnit(UniverInstanceType.UNIVER_DOC, getDefaultDocData());
- await timer(loadingTimeout);
+ await awaitTime(loadingTimeout);
}
private async _disposeUniver(): Promise<void> {
@@ -98,12 +98,6 @@ export class E2EMemoryController extends Disposable {
const unit = this._univerInstanceService.getCurrentUnitForType(UniverInstanceType.UNIVER_SHEET);
const unitId = unit?.getUnitId();
await this._univerInstanceService.disposeUnit(unitId || '');
- await timer(disposingTimeout);
+ await awaitTime(disposingTimeout);
}
}
-
-function timer(timeout: number): Promise<void> {
- return new Promise((resolve) => {
- setTimeout(resolve, timeout);
- });
-}
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index c6d8b66fce..821431d44d 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -139,6 +139,7 @@ export { isBlackColor, isWhiteColor } from './shared/color/color-kit';
export { cellToRange } from './shared/common';
export { nameCharacterCheck } from './shared/name';
export { fromCallback, takeAfter } from './shared/rxjs';
+export { awaitTime } from './shared/timer';
export { Range } from './sheets/range';
export {
DEFAULT_WORKSHEET_COLUMN_COUNT,
diff --git a/packages/core/src/shared/timer.ts b/packages/core/src/shared/timer.ts
new file mode 100644
index 0000000000..dcfbecd285
--- /dev/null
+++ b/packages/core/src/shared/timer.ts
@@ -0,0 +1,19 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+export function awaitTime(ms: number): Promise<void> {
+ return new Promise((resolve) => setTimeout(resolve, ms));
+}
diff --git a/packages/network/src/services/http/interceptors/__tests__/threshold-interceptor.spec.ts b/packages/network/src/services/http/interceptors/__tests__/threshold-interceptor.spec.ts
index 65420effbe..c2de906e32 100644
--- a/packages/network/src/services/http/interceptors/__tests__/threshold-interceptor.spec.ts
+++ b/packages/network/src/services/http/interceptors/__tests__/threshold-interceptor.spec.ts
@@ -14,15 +14,16 @@
* limitations under the License.
*/
+import { awaitTime } from '@univerjs/core';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import type { Injector } from '@univerjs/core';
-import { HTTPService } from '../../http.service';
import { createHTTPTestBed, type MockHTTPImplementation } from '../../__testing__/http-testing-utils';
+import { HTTPHeaders } from '../../headers';
+import { HTTPService } from '../../http.service';
import { IHTTPImplementation } from '../../implementations/implementation';
import { __TEST_ONLY_RESET_REQUEST_UID_DO_NOT_USE_IN_PRODUCTION } from '../../request';
-import { ThresholdInterceptorFactory } from '../threshold-interceptor';
-import { HTTPHeaders } from '../../headers';
import { HTTPResponse, HTTPResponseError } from '../../response';
+import { ThresholdInterceptorFactory } from '../threshold-interceptor';
describe('test "HTTPThresholdInterceptor"', () => {
let httpService: HTTPService;
@@ -125,14 +126,10 @@ describe('test "HTTPThresholdInterceptor"', () => {
expect(handler1).toBeDefined();
emitError(0);
- await timer();
+ await awaitTime(20);
expect(errored).toBe(true);
emitSuccess(1);
expect(await request1).toBeDefined();
});
});
-
-function timer() {
- return new Promise((resolve) => setTimeout(resolve, 200));
-}
diff --git a/packages/rpc/src/services/rpc/__tests__/rpc.service.spec.ts b/packages/rpc/src/services/rpc/__tests__/rpc.service.spec.ts
index 5baf3ac7a9..42eb84e937 100644
--- a/packages/rpc/src/services/rpc/__tests__/rpc.service.spec.ts
+++ b/packages/rpc/src/services/rpc/__tests__/rpc.service.spec.ts
@@ -14,12 +14,13 @@
* limitations under the License.
*/
-import type { Observable } from 'rxjs';
+import { awaitTime } from '@univerjs/core';
import { of, Subject } from 'rxjs';
import { beforeEach, describe, expect, it } from 'vitest';
-import type { IMessageProtocol } from '../rpc.service';
+import type { Observable } from 'rxjs';
import { ChannelClient, ChannelServer, fromModule, toModule } from '../rpc.service';
+import type { IMessageProtocol } from '../rpc.service';
describe('Test ChannelClient & ChannelServer', () => {
let clientProtocol: TestMessageProtocolForClient;
@@ -88,20 +89,14 @@ describe('Test ChannelClient & ChannelServer', () => {
// Should not emit any value when the returned observable is not subscribe.
const values: number[] = [];
const observable = clientService.getCount$();
- await timer(300);
+ await awaitTime(300);
expect(values).toEqual([]);
// Should send request only after the observable is subscribed.
const subscription = observable.subscribe((value) => values.push(value));
- await timer(300);
+ await awaitTime(300);
expect(values).toEqual([1, 2, 3]);
expect(subscription.closed).toBe(true);
});
});
});
-
-function timer(ms: number): Promise<void> {
- return new Promise((resolve) => {
- setTimeout(resolve, ms);
- });
-}