Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

refactor(editor): remove non null asserts in turbo renderer (#10454)

Yifeng Wange9484e8e1553
diff --git a/blocksuite/affine/shared/src/viewport-renderer/dom-utils.ts b/blocksuite/affine/shared/src/viewport-renderer/dom-utils.ts
index ac77812aa4..35b8aafbc2 100644
--- a/blocksuite/affine/shared/src/viewport-renderer/dom-utils.ts
+++ b/blocksuite/affine/shared/src/viewport-renderer/dom-utils.ts
@@ -1,3 +1,4 @@
+import type { EditorHost } from '@blocksuite/block-std';
import { type Viewport } from '@blocksuite/block-std/gfx';
import { Pane } from 'tweakpane';
@@ -23,7 +24,7 @@ export function syncCanvasSize(canvas: HTMLCanvasElement, host: HTMLElement) {
}
export function getViewportLayout(
- host: HTMLElement,
+ host: EditorHost,
viewport: Viewport
): ViewportLayout {
const paragraphBlocks = host.querySelectorAll(
diff --git a/blocksuite/affine/shared/src/viewport-renderer/viewport-renderer.ts b/blocksuite/affine/shared/src/viewport-renderer/viewport-renderer.ts
index 86a43f4234..b68b182f56 100644
--- a/blocksuite/affine/shared/src/viewport-renderer/viewport-renderer.ts
+++ b/blocksuite/affine/shared/src/viewport-renderer/viewport-renderer.ts
@@ -43,7 +43,7 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
public readonly canvas: HTMLCanvasElement = document.createElement('canvas');
private readonly worker: Worker;
- private layoutCache: ViewportLayout | null = null;
+ private layoutCacheData: ViewportLayout | null = null;
private tile: Tile | null = null;
private viewportElement: GfxViewportElement | null = null;
@@ -95,6 +95,13 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
return this.std.get(GfxControllerIdentifier).viewport;
}
+ get layoutCache() {
+ if (this.layoutCacheData) return this.layoutCacheData;
+ const layout = getViewportLayout(this.std.host, this.viewport);
+ this.debugLog('Layout cache updated');
+ return (this.layoutCacheData = layout);
+ }
+
async refresh() {
if (this.state === 'inactive') return;
@@ -110,19 +117,15 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
else if (this.canUseBitmapCache()) {
this.debugLog('Using cached bitmap');
this.setState('ready');
- this.drawCachedBitmap(this.layoutCache!);
+ this.drawCachedBitmap(this.layoutCache);
this.updateOptimizedBlocks();
}
// -> rendering
else {
- if (!this.layoutCache) {
- this.updateLayoutCache();
- }
- const layout = this.layoutCache!;
this.setState('rendering');
this.toggleOptimization(false);
- await this.paintLayout(layout);
- this.drawCachedBitmap(layout);
+ await this.paintLayout(this.layoutCache);
+ this.drawCachedBitmap(this.layoutCache);
this.updateOptimizedBlocks();
}
}
@@ -137,7 +140,7 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
invalidate() {
this.layoutVersion++;
- this.layoutCache = null;
+ this.layoutCacheData = null;
this.clearTile();
this.clearCanvas();
this.clearOptimizedBlocks();
@@ -150,12 +153,6 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
debugLog(message, this.state);
}
- private updateLayoutCache() {
- const layout = getViewportLayout(this.std.host, this.viewport);
- this.layoutCache = layout;
- this.debugLog('Layout cache updated');
- }
-
private clearTile() {
if (!this.tile) return;
this.tile.bitmap.close();
@@ -204,9 +201,7 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
}
private handlePaintedBitmap(bitmap: ImageBitmap, resolve: () => void) {
- if (this.tile) {
- this.tile.bitmap.close();
- }
+ this.clearTile();
this.tile = {
bitmap,
zoom: this.viewport.zoom,
@@ -281,10 +276,9 @@ export class ViewportTurboRendererExtension extends LifeCycleWatcher {
}
private clearOptimizedBlocks() {
- if (this.viewportElement) {
- this.viewportElement.clearOptimizedBlocks();
- this.debugLog('Cleared optimized blocks');
- }
+ if (!this.viewportElement) return;
+ this.viewportElement.clearOptimizedBlocks();
+ this.debugLog('Cleared optimized blocks');
}
canOptimize(): boolean {