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: add sheet fps monitor (#737)

Ran Luof2724994ad0c
diff --git a/examples/univer-plugin-debugger/src/controllers/performance-monitor.controller.ts b/examples/univer-plugin-debugger/src/controllers/performance-monitor.controller.ts
index 7f973212df..fc5a85120a 100644
--- a/examples/univer-plugin-debugger/src/controllers/performance-monitor.controller.ts
+++ b/examples/univer-plugin-debugger/src/controllers/performance-monitor.controller.ts
@@ -14,24 +14,47 @@
* limitations under the License.
*/
-import { LifecycleStages, OnLifecycle, RxDisposable } from '@univerjs/core';
-import { DocCanvasView } from '@univerjs/docs/views/doc-canvas-view.js';
-import { Inject } from '@wendellhu/redi';
+import { DocumentType, IUniverInstanceService, LifecycleStages, OnLifecycle, RxDisposable } from '@univerjs/core';
+import { DocCanvasView } from '@univerjs/docs';
+import { SheetCanvasView } from '@univerjs/sheets-ui';
+import { Inject, Injector } from '@wendellhu/redi';
import { interval, takeUntil, throttle } from 'rxjs';
@OnLifecycle(LifecycleStages.Rendered, PerformanceMonitorController)
export class PerformanceMonitorController extends RxDisposable {
- constructor(@Inject(DocCanvasView) private _docCanvasView: DocCanvasView) {
+ private _documentType: DocumentType = DocumentType.UNKNOWN;
+
+ private _hasWatched = false;
+
+ constructor(
+ @Inject(DocCanvasView) private _docCanvasView: DocCanvasView,
+ @Inject(Injector) private _injector: Injector,
+ @IUniverInstanceService private _instanceService: IUniverInstanceService
+ ) {
super();
- this._initialize();
+ this._listenDocumentTypeChange();
}
- private _initialize() {
- this._showFPS();
+ private _listenDocumentTypeChange() {
+ this._instanceService.focused$.pipe(takeUntil(this.dispose$)).subscribe((unitId) => {
+ if (unitId != null) {
+ const univerType = this._instanceService.getDocumentType(unitId);
+
+ this._documentType = univerType;
+
+ this._listenFPS();
+ }
+ });
}
- private _showFPS() {
+ private _listenFPS() {
+ if (this._hasWatched) {
+ return;
+ }
+
+ this._hasWatched = true;
+
const container = document.createElement('div');
container.classList.add('fps-monitor');
const THROTTLE_TIME = 500;
@@ -55,11 +78,21 @@ export class PerformanceMonitorController extends RxDisposable {
document.head.appendChild(document.createElement('style')).innerText = style;
- this._docCanvasView.fps$
- .pipe(takeUntil(this.dispose$))
- .pipe(throttle(() => interval(THROTTLE_TIME)))
- .subscribe((fps) => {
- container.innerText = `FPS: ${fps}`;
- });
+ if (this._documentType === DocumentType.DOC) {
+ this._docCanvasView.fps$
+ .pipe(takeUntil(this.dispose$))
+ .pipe(throttle(() => interval(THROTTLE_TIME)))
+ .subscribe((fps) => {
+ container.innerText = `FPS: ${fps}`;
+ });
+ } else if (this._documentType === DocumentType.SHEET) {
+ this._injector
+ .get(SheetCanvasView)
+ .fps$.pipe(takeUntil(this.dispose$))
+ .pipe(throttle(() => interval(THROTTLE_TIME)))
+ .subscribe((fps) => {
+ container.innerText = `FPS: ${fps}`;
+ });
+ }
}
}
diff --git a/packages/docs/src/index.ts b/packages/docs/src/index.ts
index 28872c1cea..f3245cbf38 100644
--- a/packages/docs/src/index.ts
+++ b/packages/docs/src/index.ts
@@ -55,3 +55,4 @@ export { type IUniverDocsConfig, UniverDocsPlugin } from './doc-plugin';
export { DocSkeletonManagerService } from './services/doc-skeleton-manager.service';
export { DocViewModelManagerService } from './services/doc-view-model-manager.service';
export { TextSelectionManagerService } from './services/text-selection-manager.service';
+export { DocCanvasView } from './views/doc-canvas-view';
diff --git a/packages/sheets-ui/src/index.ts b/packages/sheets-ui/src/index.ts
index af07a5b612..f92dbd3bab 100644
--- a/packages/sheets-ui/src/index.ts
+++ b/packages/sheets-ui/src/index.ts
@@ -60,3 +60,4 @@ export { SelectionRenderService } from './services/selection/selection-render.se
export { SelectionShape } from './services/selection/selection-shape';
export { SheetSkeletonManagerService } from './services/sheet-skeleton-manager.service';
export { UniverSheetsUIPlugin } from './sheets-ui-plugin';
+export { SheetCanvasView } from './views/sheet-canvas-view';
diff --git a/packages/sheets-ui/src/views/sheet-canvas-view.ts b/packages/sheets-ui/src/views/sheet-canvas-view.ts
index 114b454323..84c14ecbe3 100644
--- a/packages/sheets-ui/src/views/sheet-canvas-view.ts
+++ b/packages/sheets-ui/src/views/sheet-canvas-view.ts
@@ -15,7 +15,7 @@
*/
import type { Workbook, Worksheet } from '@univerjs/core';
-import { ICommandService, IUniverInstanceService, LifecycleStages, OnLifecycle } from '@univerjs/core';
+import { ICommandService, IUniverInstanceService, LifecycleStages, OnLifecycle, RxDisposable } from '@univerjs/core';
import type { IRender, IWheelEvent, Scene } from '@univerjs/engine-render';
import {
IRenderManagerService,
@@ -30,6 +30,7 @@ import {
Viewport,
} from '@univerjs/engine-render';
import { Inject } from '@wendellhu/redi';
+import { BehaviorSubject } from 'rxjs';
import { SetScrollRelativeCommand } from '../commands/commands/set-scroll.command';
import {
@@ -42,7 +43,7 @@ import { ISelectionRenderService } from '../services/selection/selection-render.
import { SheetSkeletonManagerService } from '../services/sheet-skeleton-manager.service';
@OnLifecycle(LifecycleStages.Ready, SheetCanvasView)
-export class SheetCanvasView {
+export class SheetCanvasView extends RxDisposable {
private _scene!: Scene;
private _currentWorkbook!: Workbook;
@@ -51,6 +52,10 @@ export class SheetCanvasView {
private _isLoadedEditor = false;
+ private readonly _fps$ = new BehaviorSubject<string>('');
+
+ readonly fps$ = this._fps$.asObservable();
+
constructor(
@IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,
@ICommandService private readonly _commandService: ICommandService,
@@ -59,6 +64,7 @@ export class SheetCanvasView {
private readonly _selectionRenderService: ISelectionRenderService,
@Inject(SheetSkeletonManagerService) private readonly _sheetSkeletonManagerService: SheetSkeletonManagerService
) {
+ super();
this._currentUniverService.currentSheet$.subscribe((workbook) => {
if (workbook == null) {
return;
@@ -74,6 +80,10 @@ export class SheetCanvasView {
});
}
+ override dispose(): void {
+ this._fps$.complete();
+ }
+
private _addNewRender() {
const workbook = this._currentWorkbook;
@@ -117,6 +127,7 @@ export class SheetCanvasView {
engine.runRenderLoop(() => {
// document.getElementById('app')!.innerHTML = engine.getFps().toString();
scene.render();
+ this._fps$.next(Math.round(engine.getFps()).toString());
});
}