소스 검색

fix(reactive): enhance the type inference capability of the ReactiveState.get and set (#796)

xiamidaxia 4 달 전
부모
커밋
9383fe33f6

+ 3 - 0
common/config/rush/pnpm-lock.yaml

@@ -1447,6 +1447,9 @@ importers:
       '@flowgram.ai/playground-react':
         specifier: workspace:*
         version: link:../playground-react
+      '@flowgram.ai/reactive':
+        specifier: workspace:*
+        version: link:../../common/reactive
       '@flowgram.ai/redux-devtool-plugin':
         specifier: workspace:*
         version: link:../../plugins/redux-devtool-plugin

+ 1 - 0
packages/client/editor/package.json

@@ -45,6 +45,7 @@
     "@flowgram.ai/shortcuts-plugin": "workspace:*",
     "@flowgram.ai/utils": "workspace:*",
     "@flowgram.ai/variable-plugin": "workspace:*",
+    "@flowgram.ai/reactive": "workspace:*",
     "inversify": "^6.0.1",
     "reflect-metadata": "~0.2.2"
   },

+ 9 - 0
packages/client/editor/src/index.ts

@@ -15,6 +15,15 @@ export * from '@flowgram.ai/variable-plugin';
 export * from '@flowgram.ai/shortcuts-plugin';
 export * from '@flowgram.ai/node-core-plugin';
 export * from '@flowgram.ai/i18n-plugin';
+export {
+  ReactiveState,
+  ReactiveBaseState,
+  Tracker,
+  useReactiveState,
+  useReadonlyReactiveState,
+  useObserve,
+  observe,
+} from '@flowgram.ai/reactive';
 export {
   type interfaces,
   injectable,

+ 1 - 1
packages/common/reactive/__tests__/reactive-state.test.ts

@@ -9,7 +9,7 @@ import { ReactiveState, Tracker } from '../src';
 
 describe('reactive-state', () => {
   it('base', () => {
-    const state = new ReactiveState<{ a: number; b: number }>({ a: 0, b: 0 });
+    const state = new ReactiveState({ a: 0, b: 0 });
     const { value } = state;
     let autorunTimes = -1;
     const compute = Tracker.autorun<number>(() => {

+ 2 - 2
packages/common/reactive/src/core/reactive-state.ts

@@ -13,7 +13,7 @@ import { createProxy } from '../utils/create-proxy';
 export class ReactiveState<V extends Record<string, any>> extends ReactiveBaseState<V> {
   private _keyDeps: Map<string, Dependency> = new Map();
 
-  set(key: keyof V & string, value: any): boolean {
+  set<K extends keyof V & string>(key: K, value: V[K]): boolean {
     this._ensureKey(key);
     const oldValue = this._value[key];
     if (!this._isEqual(oldValue, value)) {
@@ -24,7 +24,7 @@ export class ReactiveState<V extends Record<string, any>> extends ReactiveBaseSt
     return false;
   }
 
-  get(key: keyof V & string) {
+  get<K extends keyof V & string>(key: K): V[K] {
     this._ensureKey(key);
     this._addDepend(this._keyDeps.get(key)!);
     return this._value[key];