Merge pull request #686 from smartxworks/feat/watch-property

refactor(runtime): watch property change on properties level
This commit is contained in:
tanbowensg 2023-02-13 15:04:15 +08:00 committed by GitHub
commit 3da843a7a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,5 @@
import { mapValues, isArray, set } from 'lodash';
import { mapValues, isArray } from 'lodash';
import dayjs from 'dayjs';
import produce from 'immer';
import 'dayjs/locale/zh-cn';
import isLeapYear from 'dayjs/plugin/isLeapYear';
import relativeTime from 'dayjs/plugin/relativeTime';
@ -208,30 +207,16 @@ export class StateManager {
};
// watch change
if (value && typeof value === 'object') {
let resultCache = evaluated as PropsAfterEvaled<Exclude<T, string>>;
this.mapValuesDeep(value, ({ value, path }) => {
const isDynamicExpression =
typeof value === 'string' &&
parseExpression(value).some(exp => typeof exp !== 'string');
if (!isDynamicExpression) return;
const stop = watch(
() => {
const result = this._maskedEval(value as string, options);
return result;
},
newV => {
resultCache = produce(resultCache, draft => {
set(draft, path, newV);
});
watcher({ result: resultCache as EvaledResult<T> });
}
);
stops.push(stop);
});
const stop = watch(
() => {
const result = this.deepEval(value);
return result;
},
newV => {
watcher({ result: newV as EvaledResult<T> });
}
);
stops.push(stop);
} else {
const stop = watch(
() => {
@ -240,9 +225,6 @@ export class StateManager {
},
newV => {
watcher({ result: newV as EvaledResult<T> });
},
{
deep: true,
}
);
stops.push(stop);