Sfoglia il codice sorgente

refactor(core): remove resize-polling (#831)

xiamidaxia 4 mesi fa
parent
commit
cf1b31394a

+ 0 - 44
packages/canvas-engine/core/src/core/utils/resize-polling.ts

@@ -1,44 +0,0 @@
-/**
- * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
- * SPDX-License-Identifier: MIT
- */
-
-import { Disposable } from '@flowgram.ai/utils';
-
-const POLLING_MAX_TIME = 5000;
-const POLLING_INTERVAL = 100;
-
-/**
- * ResizeObserver 在有动画情况不准确,需要轮循兜底执行 resize 几秒
- */
-export class ResizePolling implements Disposable {
-  private _intervalKey?: number;
-
-  private _disposed = false;
-
-  private _startTime = 0;
-
-  start(fn: () => boolean) {
-    if (this._intervalKey !== undefined) {
-      clearInterval(this._intervalKey);
-    }
-    if (this._disposed) {
-      return;
-    }
-    this._startTime = Date.now();
-    this._intervalKey = window.setInterval(() => {
-      const success = fn();
-      if (!success || Date.now() - this._startTime >= POLLING_MAX_TIME) {
-        clearInterval(this._intervalKey);
-      }
-    }, POLLING_INTERVAL);
-  }
-
-  dispose() {
-    if (this._intervalKey !== undefined) {
-      clearInterval(this._intervalKey);
-    }
-    this._disposed = true;
-    this._intervalKey = undefined;
-  }
-}

+ 5 - 15
packages/canvas-engine/core/src/playground.ts

@@ -13,7 +13,6 @@ import { CommandService } from '@flowgram.ai/command';
 import { SelectionService } from './services';
 import { PlaygroundContribution, PlaygroundRegistry } from './playground-contribution';
 import { PlaygroundConfig } from './playground-config';
-import { ResizePolling } from './core/utils/resize-polling';
 import {
   type PipelineDimension,
   // PipelineDispatcher,
@@ -61,8 +60,6 @@ export class Playground<CONTEXT = PlaygroundContext> implements Disposable {
   // 唯一 className,适配画布多实例场景
   private playgroundClassName = nanoid();
 
-  private _resizePolling = new ResizePolling();
-
   constructor(
     // @inject(PlaygroundId) readonly id: PlaygroundId,
     @inject(EntityManager) readonly entityManager: EntityManager,
@@ -99,7 +96,6 @@ export class Playground<CONTEXT = PlaygroundContext> implements Disposable {
       // this.ableManager,
       this.commandService,
       this.selectionService,
-      this._resizePolling,
       Disposable.create(() => {
         this.node.remove();
       }),
@@ -237,13 +233,12 @@ export class Playground<CONTEXT = PlaygroundContext> implements Disposable {
     if (this.isReady) return;
     this.isReady = true;
     if (this.playgroundConfig.autoResize) {
-      const resizeWithPolling = () => {
+      const resize = () => {
         if (this.disposed) return;
         this.resize();
-        this._resizePolling.start(() => this.resize());
       };
       if (typeof ResizeObserver !== 'undefined') {
-        const resizeObserver = new ResizeObserver(resizeWithPolling);
+        const resizeObserver = new ResizeObserver(resize);
         resizeObserver.observe(this.node);
         this.toDispose.push(
           Disposable.create(() => {
@@ -252,14 +247,9 @@ export class Playground<CONTEXT = PlaygroundContext> implements Disposable {
         );
       } else {
         this.toDispose.push(
-          domUtils.addStandardDisposableListener(
-            window.document.body,
-            'resize',
-            resizeWithPolling,
-            {
-              passive: true,
-            }
-          )
+          domUtils.addStandardDisposableListener(window.document.body, 'resize', resize, {
+            passive: true,
+          })
         );
       }
       this.resize();