소스 검색

fix: document is disposed and call toJSON should throw error (#646)

xiamidaxia 5 달 전
부모
커밋
e033f09d35

+ 1 - 0
apps/demo-fixed-layout/src/hooks/use-editor-props.ts

@@ -147,6 +147,7 @@ export function useEditorProps(
         enable: true,
         enableChangeNode: true, // Listen Node engine data change
         onApply: debounce((ctx, opt) => {
+          if (ctx.document.disposed) return;
           // Listen change to trigger auto save
           console.log('auto save: ', ctx.document.toJSON());
         }, 100),

+ 1 - 0
apps/demo-free-layout/src/hooks/use-editor-props.tsx

@@ -216,6 +216,7 @@ export function useEditorProps(
        * Content change
        */
       onContentChange: debounce((ctx, event) => {
+        if (ctx.document.disposed) return;
         console.log('Auto Save: ', event, ctx.document.toJSON());
       }, 1000),
       /**

+ 4 - 0
packages/canvas-engine/document/__tests__/flow-document.test.ts

@@ -280,4 +280,8 @@ describe('flow-document', () => {
       document.getNode('block_0')!.getNodeMeta() === document.getNode('block_1')!.getNodeMeta()
     ).toBeTruthy();
   });
+  it('document is disposed and call toJSON should throw error', () => {
+    document.dispose();
+    expect(() => document.toJSON()).toThrowError(/disposed/);
+  });
 });

+ 5 - 0
packages/canvas-engine/document/src/flow-document.ts

@@ -462,6 +462,11 @@ export class FlowDocument<T = FlowDocumentJSON> implements Disposable {
    * 导出数据,可以重载
    */
   toJSON(): T | any {
+    if (this.disposed) {
+      throw new Error(
+        'The FlowDocument has been disposed and it is no longer possible to call toJSON.'
+      );
+    }
     return {
       nodes: this.root.toJSON().blocks,
     };

+ 4 - 0
packages/canvas-engine/free-layout-core/__tests__/workflow-document.test.ts

@@ -547,4 +547,8 @@ describe('workflow-document with nestedJSON & subCanvas', () => {
     expect(canvasNode.collapsedChildren.length).toEqual(2);
     expect(document.toJSON()).toEqual(subCanvasInlinePortSchema);
   });
+  it('document is disposed and call toJSON should throw error', () => {
+    document.dispose();
+    expect(() => document.toJSON()).toThrowError(/disposed/);
+  });
 });

+ 5 - 0
packages/canvas-engine/free-layout-core/src/workflow-document.ts

@@ -645,6 +645,11 @@ export class WorkflowDocument extends FlowDocument {
    * 导出数据
    */
   toJSON(): WorkflowJSON {
+    if (this.disposed) {
+      throw new Error(
+        'The WorkflowDocument has been disposed and it is no longer possible to call toJSON.'
+      );
+    }
     const rootJSON = this.toNodeJSON(this.root);
     const json = {
       nodes: rootJSON.blocks ?? [],