Kaynağa Gözat

feat(version): add version in scope.available (#649)

* feat(variable): add scope available version

* feat: any var change update version in output

* fix: bump version
Yiwei Mao 5 ay önce
ebeveyn
işleme
41e1453042

+ 1 - 1
packages/variable-engine/variable-core/__tests__/ast/variable-declaration.test.ts

@@ -108,7 +108,7 @@ describe('test Basic Variable Declaration', () => {
     expect(previousVariable3.version).toBe(1); // 更新次数为一次
     expect(testScope.ast.version).toBe(2); // 调用了两次 fromJSON,因此更新了两次
     expect(globalVariableTable.variables.map((_v) => _v.key)).toMatchSnapshot();
-    expect(globalVariableTable.version).toBe(2); // 调用了两次 fromJSON,因此 version 是 2
+    expect(globalVariableTable.version).toBe(2 + 1); // 调用了两次 fromJSON + Object 变量的下钻发生变化,因此 version 是 2 + 1
     expect(testScope.available.variables.map((_v) => _v.key)).toMatchSnapshot();
   });
 

+ 15 - 0
packages/variable-engine/variable-core/src/scope/datas/scope-available-data.ts

@@ -38,10 +38,23 @@ export class ScopeAvailableData {
     return this.scope.variableEngine.globalVariableTable;
   }
 
+  protected _version: number = 0;
+
   protected refresh$: Subject<void> = new Subject();
 
   protected _variables: VariableDeclaration[] = [];
 
+  get version() {
+    return this._version;
+  }
+
+  protected bumpVersion() {
+    this._version = this._version + 1;
+    if (this._version === Number.MAX_SAFE_INTEGER) {
+      this._version = 0;
+    }
+  }
+
   // 刷新可访问变量列表
   refresh(): void {
     // 销毁的作用域不用触发 refresh
@@ -118,10 +131,12 @@ export class ScopeAvailableData {
         this._variables = _variables;
         this.memo.clear();
         this.onDataChangeEmitter.fire(this._variables);
+        this.bumpVersion();
         this.onListOrAnyVarChangeEmitter.fire(this._variables);
       }),
       this.onAnyVariableChange(() => {
         this.onDataChangeEmitter.fire(this._variables);
+        this.bumpVersion();
         this.onListOrAnyVarChangeEmitter.fire(this._variables);
       }),
       Disposable.create(() => {

+ 4 - 0
packages/variable-engine/variable-core/src/scope/datas/scope-output-data.ts

@@ -29,6 +29,10 @@ export class ScopeOutputData {
     return this.scope.variableEngine.globalVariableTable;
   }
 
+  get version() {
+    return this.variableTable.version;
+  }
+
   /**
    * @deprecated use onListOrAnyVarChange instead
    */

+ 14 - 2
packages/variable-engine/variable-core/src/scope/variable-table.ts

@@ -16,6 +16,9 @@ export class VariableTable implements IVariableTable {
 
   toDispose = new DisposableCollection();
 
+  /**
+   * @deprecated
+   */
   protected onDataChangeEmitter = new Emitter<void>();
 
   protected variables$: Subject<VariableDeclaration[]> = new Subject<VariableDeclaration[]>();
@@ -71,7 +74,7 @@ export class VariableTable implements IVariableTable {
   protected _version: number = 0;
 
   fireChange() {
-    this._version++;
+    this.bumpVersion();
     this.onDataChangeEmitter.fire();
     this.variables$.next(this.variables);
     this.parentTable?.fireChange();
@@ -81,13 +84,22 @@ export class VariableTable implements IVariableTable {
     return this._version;
   }
 
+  protected bumpVersion() {
+    this._version = this._version + 1;
+    if (this._version === Number.MAX_SAFE_INTEGER) {
+      this._version = 0;
+    }
+  }
+
   constructor(
     public parentTable?: IVariableTable // 父变量表,会包含所有子表的变量
   ) {
     this.toDispose.pushAll([
       this.onDataChangeEmitter,
       // active share()
-      this.onAnyVariableChange(() => null),
+      this.onAnyVariableChange(() => {
+        this.bumpVersion();
+      }),
     ]);
   }