From 28e6c9a0f47a249b4969cc25e23e668d9eaa935d Mon Sep 17 00:00:00 2001 From: mackwang Date: Tue, 14 Jan 2025 15:13:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20ignoreReactivePattern=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=20shallowReactivePattern?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/core/mergeOptions.js | 3 ++- packages/core/src/core/proxy.js | 16 ++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/core/src/core/mergeOptions.js b/packages/core/src/core/mergeOptions.js index e89414a01a..dab3f17421 100644 --- a/packages/core/src/core/mergeOptions.js +++ b/packages/core/src/core/mergeOptions.js @@ -122,9 +122,10 @@ function extractObservers (options) { Object.keys(props).forEach(key => { const prop = props[key] if (prop && prop.observer) { + let callback = prop.observer + delete prop.observer mergeWatch(key, { handler (...rest) { - let callback = prop.observer if (typeof callback === 'string') { callback = this[callback] } diff --git a/packages/core/src/core/proxy.js b/packages/core/src/core/proxy.js index 78162e10b4..15ff6719c6 100644 --- a/packages/core/src/core/proxy.js +++ b/packages/core/src/core/proxy.js @@ -111,7 +111,7 @@ export default class MpxProxy { this.uid = uid++ this.name = options.name || '' this.options = options - this.ignoreReactivePattern = this.options.options?.ignoreReactivePattern + this.shallowReactivePattern = this.options.options?.shallowReactivePattern // beforeCreate -> created -> mounted -> unmounted this.state = BEFORECREATE this.ignoreProxyMap = makeMap(Mpx.config.ignoreProxyWhiteList) @@ -145,11 +145,11 @@ export default class MpxProxy { this.initApi() } - processIgnoreReactive (obj) { - if (this.ignoreReactivePattern && isObject(obj)) { + processShallowReactive (obj) { + if (this.shallowReactivePattern && isObject(obj)) { Object.keys(obj).forEach((key) => { - if (this.ignoreReactivePattern.test(key)) { - // 命中ignoreReactivePattern的属性将其设置为 shallowReactive + if (this.shallowReactivePattern.test(key)) { + // 命中shallowReactivePattern的属性将其设置为 shallowReactive defineReactive(obj, key, obj[key], true) Object.defineProperty(obj, key, { enumerable: true, @@ -292,10 +292,10 @@ export default class MpxProxy { if (isReact) { // react模式下props内部对象透传无需深clone,依赖对象深层的数据响应触发子组件更新 this.props = this.target.__getProps() - reactive(this.processIgnoreReactive(this.props)) + reactive(this.processShallowReactive(this.props)) } else { this.props = diffAndCloneA(this.target.__getProps(this.options)).clone - reactive(this.processIgnoreReactive(this.props)) + reactive(this.processShallowReactive(this.props)) } proxy(this.target, this.props, undefined, false, this.createProxyConflictHandler('props')) } @@ -335,7 +335,7 @@ export default class MpxProxy { if (isFunction(dataFn)) { Object.assign(this.data, callWithErrorHandling(dataFn.bind(this.target), this, 'data function')) } - reactive(this.processIgnoreReactive(this.data)) + reactive(this.processShallowReactive(this.data)) proxy(this.target, this.data, undefined, false, this.createProxyConflictHandler('data')) this.collectLocalKeys(this.data) }