-
Notifications
You must be signed in to change notification settings - Fork 392
样式缓存&媒体查询&resize重新计算rpx和style #2101
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
base: master
Are you sure you want to change the base?
Conversation
| const empty = {} | ||
|
|
||
| function formatValue (value) { | ||
| function formatValue (value, screenInfo = global.__mpxAppDimensionsInfo.screen) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个地方没必要传参吧,从全局取就好了
| export default function styleHelperMixin () { | ||
| return { | ||
| watch: { | ||
| '__dimensionsInfo.screen.width, __dimensionsInfo.screen.height' (newValue, oldValue) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个缓存没必要再组件里清除,组件只要能刷新自己就好
| } else { | ||
| const classMapCode = Object.entries(classMap).reduce((result, [key, value]) => { | ||
| result += `get ['${key}']() { | ||
| if(!this.__classMapValueCache) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你这个地方的this绑定到哪里去了?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
直接走闭包变量作为缓存就可以了
| _media | ||
| } | ||
| } else { | ||
| classMap[key] = Object.assign(classMap[key] || {}, classMapValue) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你这逻辑不会覆盖么
| triggerPageStatusHook(mpxProxy, newVal) | ||
| // 如果当前全局size与pagesize不一致,在show之后触发一次resize事件 | ||
| if (newVal === 'show') { | ||
| triggerResizeEvent(mpxProxy, pageId) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
确认一下时序
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为啥不是在下面if条件中进行的?
|
|
||
| // // 对比 screen 高宽是否存在变化 | ||
| if (getPageSize(screen) === oldScreen) return | ||
| global.__appClassMapValueCache = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这是个Map
| const oldScreen = getPageSize(global.__mpxAppDimensionsInfo.screen) | ||
| useDimensionsInfo({ window, screen }) | ||
|
|
||
| if (getPageSize(screen) === oldScreen) console.log('Dimensions change, but screen size not changed', getPageSize(screen), oldScreen) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
移除调试代码
| return concat(staticClass, stringifyDynamicClass(dynamicClass)) | ||
| }, | ||
| __getStyle (staticClass, dynamicClass, staticStyle, dynamicStyle, hide) { | ||
| console.log('__getStyle ', JSON.stringify(global.__mpxAppDimensionsInfo.screen)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
移除调试代码
| if (ctorType === 'app') { | ||
| output += ` | ||
| /* styles */ | ||
| global.__classMapValueCache = new Map(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个cache应该是每个style独立一份
|
|
||
| function createInstance ({ propsRef, type, rawOptions, currentInject, validProps, componentsMap, pageId, intersectionCtx, relation, parentProvides }) { | ||
| if (type === 'page') { | ||
| set(global.__mpxPageSizeCountMap, pageId, global.__mpxSizeCount) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
逻辑合并到下面处理page的条件中去
| // 如果当前全局size与pagesize不一致,在show之后触发一次resize事件 | ||
| if (newVal === 'show' && global.__mpxPageSizeCountMap[pageId] !== global.__mpxSizeCount) { | ||
| // 刷新__mpxPageSizeCountMap, 每个页面仅会执行一次,直接驱动render刷新 | ||
| set(global.__mpxPageSizeCountMap, pageId, global.__mpxSizeCount) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
前面set过了这里可以直接赋值不用set
| const navigation = getFocusedNavigation() | ||
|
|
||
| if (navigation) { | ||
| set(global.__mpxPageSizeCountMap, navigation.pageId, global.__mpxSizeCount) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
直接赋值
| let content = '' | ||
| let output = '/* styles */\n' | ||
| let output = '' | ||
| if (ctorType === 'app') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里全局方法不要挂在app当中,独立输出组件时没有app,都统一放到运行时的styleHelperMixin.ios.js中
| }) | ||
| const classMapCode = Object.entries(classMap).reduce((result, [key, value]) => { | ||
| result += `get ['${key}']() { | ||
| return global.__getCacheClass(__classMapCache, '${key}', () => (${shallowStringify(value)})); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
感觉样板代码有点太多了,特别是key会出现两次,可以优化为:
classMap只转为getter, __getClassMap方法移除,改为__getClassStyle,接收classname返回样式obj,在运行时处理缓存逻辑
global.currentInject.injectMethods = {
__getClassStyle: function(classname) {
if(!__classMap) {
__classMap = {
'class-a':()=>({'color': 'red'}),
'class-b':()=>({'font-size': 12})
}
}
return global.__getCachedClassStyle(classname, __classMap, __classMapCache);
}| }, '') | ||
| if (ctorType === 'app') { | ||
| output += ` | ||
| const __classMapCache = new Map() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
简化叫classCache吧
| // todo 全局样式在每个页面和组件中生效,以支持全局原子类,后续支持样式模块复用后可考虑移除 | ||
| mergeResult(appClassMap[className]) | ||
| const _thisStyle = this.__getClassStyle?.(className) | ||
| const appStyle = global.__getAppClassStyle?.(className) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里实现优化一下,在本地classStyle存在的情况下不需要访问App
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let localStyle, appStyle
const getAppClassStyle = global.__getAppClassStyle || noop
if(localStyle = this.__getClassStyle(className)){
// use localStyle
} else if(appStyle = getAppClassStyle(className)){
// use appStyle
}| } | ||
| } else if (isObject(this.__props[className])) { | ||
| // externalClasses必定以对象形式传递下来 | ||
| mergeResult(this.__props[className]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
确认externalClasses是否需要处理default和media,需要的话处理统一放在mergeResult里吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个我看了一下不需要处理,你改完代码需要测试啊。。。
| let localStyle, appStyle | ||
| const getAppClassStyle = global.__getAppClassStyle || noop | ||
| if (localStyle = this.__getClassStyle(className)) { | ||
| mergeResult(localStyle || empty) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不需要||empty
fix: RN 屏幕尺寸变化时,页面onResize触发时机由原来所有页面立即触发onResize更改为只有栈顶页面立即触发,其他页面在其onShow之后触发
feat: RN rpx,vh,vw单位支持在屏幕尺寸变化时重新渲染
feat: RN 新增rnConfig.customDimensionsInfo配置用于修改控制rpx,vh,vw渲染的屏幕尺寸信息
feat: RN 支持css媒体查询