Skip to content

Commit

Permalink
Merge branch 'refs/heads/master' into fix-drn
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackgan3 committed Dec 5, 2024
2 parents 7500ac8 + 7ab2770 commit a01f03d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ module.exports = {
rules: {
'no-cond-assign': 0,
camelcase: 0,
indent: 0
indent: 0,
'symbol-description': 0
},
env: {
'jest/globals': true,
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/core/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ export default class MpxProxy {
if (isReact) {
// react专用,正确触发updated钩子
this.pendingUpdatedFlag = false
// react专用,用于提供render渲染函数中访问props数据同时不追踪props数据的顶层响应性
this.propsWithoutReactive = {}
this.memoVersion = Symbol()
this.finalMemoVersion = Symbol()
}
}
this.initApi()
Expand Down Expand Up @@ -290,7 +290,6 @@ export default class MpxProxy {
if (isReact) {
// react模式下props内部对象透传无需深clone,依赖对象深层的数据响应触发子组件更新
this.props = this.target.__getProps()
this.propsWithoutReactive = Object.assign({}, this.props)
shallowReactive(this.processIgnoreReactive(this.props))
} else {
this.props = diffAndCloneA(this.target.__getProps(this.options)).clone
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/observer/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ export function queueJob (job) {
}
}

export function hasPendingJob (job) {
return queue.length && queue.includes(job, isFlushing && job.allowRecurse ? flushIndex + 1 : flushIndex)
}

function queueFlush () {
if (!isFlushing && !isFlushPending) {
isFlushPending = true
Expand Down
32 changes: 17 additions & 15 deletions packages/core/src/platform/patch/react/getDefaultOptions.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { hasOwn, isFunction, noop, isObject, isArray, getByPath, collectDataset,
import MpxProxy from '../../../core/proxy'
import { BEFOREUPDATE, ONLOAD, UPDATED, ONSHOW, ONHIDE, ONRESIZE, REACTHOOKSEXEC } from '../../../core/innerLifecycle'
import mergeOptions from '../../../core/mergeOptions'
import { queueJob } from '../../../observer/scheduler'
import { queueJob, hasPendingJob } from '../../../observer/scheduler'
import { createSelectorQuery, createIntersectionObserver } from '@mpxjs/api-proxy'
import { IntersectionObserverContext, RouteContext, KeyboardAvoidContext } from '@mpxjs/webpack-plugin/lib/runtime/components/react/dist/context'

Expand Down Expand Up @@ -35,7 +35,6 @@ function createEffect (proxy, components) {
proxy.callHook(BEFOREUPDATE)
proxy.pendingUpdatedFlag = true
}
// eslint-disable-next-line symbol-description
proxy.stateVersion = Symbol()
proxy.onStoreChange && proxy.onStoreChange()
}
Expand All @@ -49,18 +48,14 @@ function createEffect (proxy, components) {
if (!type) return null
return createElement(type, ...rest)
}
const getValue = (name) => {
if (hasOwn(proxy.propsWithoutReactive, name)) {
return proxy.propsWithoutReactive[name]
} else {
return proxy.target[name]
}
}

proxy.effect = new ReactiveEffect(() => {
// reset instance
proxy.target.__resetInstance()
return proxy.target.__injectedRender(innerCreateElement, getComponent, getValue)
return proxy.target.__injectedRender(innerCreateElement, getComponent)
}, () => queueJob(update), proxy.scope)
// render effect允许自触发
proxy.toggleRecurse(true)
}

function getRootProps (props) {
Expand Down Expand Up @@ -272,12 +267,10 @@ function createInstance ({ propsRef, type, rawOptions, currentInject, validProps

Object.assign(proxy, {
onStoreChange: null,
// eslint-disable-next-line symbol-description
stateVersion: Symbol(),
subscribe: (onStoreChange) => {
if (!proxy.effect) {
createEffect(proxy, components)
// eslint-disable-next-line symbol-description
proxy.stateVersion = Symbol()
}
proxy.onStoreChange = onStoreChange
Expand Down Expand Up @@ -434,12 +427,10 @@ export function getDefaultOptions ({ type, rawOptions = {}, currentInject }) {
Object.keys(validProps).forEach((key) => {
if (hasOwn(props, key)) {
instance[key] = props[key]
proxy.propsWithoutReactive[key] = props[key]
} else {
const altKey = hump2dash(key)
if (hasOwn(props, altKey)) {
instance[key] = props[altKey]
proxy.propsWithoutReactive[key] = props[altKey]
}
}
})
Expand Down Expand Up @@ -473,7 +464,18 @@ export function getDefaultOptions ({ type, rawOptions = {}, currentInject }) {

useSyncExternalStore(proxy.subscribe, proxy.getSnapshot)

const root = rawOptions.options?.disableMemo ? proxy.effect.run() : useMemo(() => proxy.effect.run(), [proxy.stateVersion])
if ((rawOptions.options?.disableMemo)) {
proxy.memoVersion = Symbol()
}

const finalMemoVersion = useMemo(() => {
if (!hasPendingJob(proxy.update)) {
proxy.finalMemoVersion = Symbol()
}
return proxy.finalMemoVersion
}, [proxy.stateVersion, proxy.memoVersion])

const root = useMemo(() => proxy.effect.run(), [finalMemoVersion])
if (root && root.props.ishost) {
const rootProps = getRootProps(props)
rootProps.style = Object.assign({}, root.props.style, rootProps.style)
Expand Down
5 changes: 2 additions & 3 deletions packages/webpack-plugin/lib/react/processTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,15 @@ module.exports = function (template, {
try {
const ignoreMap = Object.assign({
createElement: true,
getComponent: true,
getValue: true
getComponent: true
}, meta.wxsModuleMap)
const bindResult = bindThis.transform(rawCode, {
ignoreMap
// customBindThis (path, t) {
// path.replaceWith(t.callExpression(t.identifier('getValue'), [t.stringLiteral(path.node.name)]))
// }
})
output += `global.currentInject.render = function (createElement, getComponent, getValue) {
output += `global.currentInject.render = function (createElement, getComponent) {
return ${bindResult.code}
};\n`
} catch (e) {
Expand Down

0 comments on commit a01f03d

Please sign in to comment.