Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复ignoreReactivePattern #1812

Merged
merged 6 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/core/src/core/mergeOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down
20 changes: 11 additions & 9 deletions packages/core/src/core/proxy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { reactive } from '../observer/reactive'
import { reactive, defineReactive } from '../observer/reactive'
import { ReactiveEffect, pauseTracking, resetTracking } from '../observer/effect'
import { effectScope } from '../platform/export/index'
import { watch } from '../observer/watch'
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -145,10 +145,12 @@ 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)) {
if (this.shallowReactivePattern.test(key)) {
// 命中shallowReactivePattern的属性将其设置为 shallowReactive
defineReactive(obj, key, obj[key], true)
Object.defineProperty(obj, key, {
enumerable: true,
// set configurable to false to skip defineReactive
Expand Down Expand Up @@ -290,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'))
}
Expand Down Expand Up @@ -333,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)
}
Expand Down Expand Up @@ -514,7 +516,7 @@ export default class MpxProxy {
if (hasOwn(renderData, key)) {
const data = renderData[key]
const firstKey = getFirstKey(key)
if (!this.localKeysMap[firstKey] || (this.ignoreReactivePattern && this.ignoreReactivePattern.test(firstKey))) {
if (!this.localKeysMap[firstKey]) {
continue
}
// 外部clone,用于只需要clone的场景
Expand Down
2 changes: 0 additions & 2 deletions packages/core/src/platform/patch/getDefaultOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ function transformProperties (properties) {
} else {
newFiled = Object.assign({}, rawFiled)
}
const rawObserver = rawFiled?.observer
newFiled.observer = function (value, oldValue) {
if (this.__mpxProxy) {
this[key] = value
this.__mpxProxy.propsUpdated()
}
rawObserver && rawObserver.call(this, value, oldValue)
}
newProps[key] = newFiled
})
Expand Down
Loading