@mpxjs/core 2.10.4 → 2.10.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/core",
3
- "version": "2.10.4",
3
+ "version": "2.10.5",
4
4
  "description": "mpx runtime core",
5
5
  "keywords": [
6
6
  "miniprogram",
@@ -109,5 +109,5 @@
109
109
  "url": "https://github.com/didi/mpx/issues"
110
110
  },
111
111
  "sideEffects": false,
112
- "gitHead": "c877bcebc894c75fe107f7b997cee5b494932e43"
112
+ "gitHead": "80a4120733a1ee64e394ae58240497ea6721f435"
113
113
  }
@@ -4,6 +4,8 @@ import { makeMap, spreadProp, getFocusedNavigation, hasOwn } from '@mpxjs/utils'
4
4
  import { mergeLifecycle } from '../convertor/mergeLifecycle'
5
5
  import { LIFECYCLE } from '../platform/patch/lifecycle/index'
6
6
  import Mpx from '../index'
7
+ import { reactive } from '../observer/reactive'
8
+ import { watch } from '../observer/watch'
7
9
  import { createElement, memo, useRef, useEffect } from 'react'
8
10
  import * as ReactNative from 'react-native'
9
11
  import { initAppProvides } from './export/inject'
@@ -90,6 +92,60 @@ export default function createApp (options) {
90
92
  global.__navigationHelper.lastFailCallback = null
91
93
  }
92
94
  }
95
+ const appState = reactive({ state: '' })
96
+ // TODO hideReason 暂未完全模拟
97
+ // 0用户退出小程序
98
+ // 1进入其他小程序
99
+ // 2打开原生功能页
100
+ // 3其他
101
+ watch(() => appState.state, (value) => {
102
+ if (value === 'show') {
103
+ let options = appState.showOptions
104
+ delete appState.showOptions
105
+ if (!options) {
106
+ const navigation = getFocusedNavigation()
107
+ if (navigation) {
108
+ const state = navigation.getState()
109
+ const current = state.routes[state.index]
110
+ options = {
111
+ path: current.name,
112
+ query: current.params,
113
+ scene: 0,
114
+ shareTicket: '',
115
+ referrerInfo: {}
116
+ }
117
+ } else {
118
+ options = {}
119
+ }
120
+ }
121
+ global.__mpxAppCbs.show.forEach((cb) => {
122
+ cb(options)
123
+ })
124
+ } else if (value === 'hide') {
125
+ const reason = appState.hideReason ?? 3
126
+ delete appState.hideReason
127
+ global.__mpxAppCbs.hide.forEach((cb) => {
128
+ cb({
129
+ reason
130
+ })
131
+ })
132
+ }
133
+ }, { sync: true })
134
+ const onAppStateChange = (currentState) => {
135
+ const navigation = getFocusedNavigation()
136
+ if (currentState === 'active') {
137
+ appState.state = 'show'
138
+ if (navigation && hasOwn(global.__mpxPageStatusMap, navigation.pageId)) {
139
+ global.__mpxPageStatusMap[navigation.pageId] = 'show'
140
+ }
141
+ } else if (currentState === 'inactive' || currentState === 'background') {
142
+ appState.hideReason = 3
143
+ appState.state = 'hide'
144
+ if (navigation && hasOwn(global.__mpxPageStatusMap, navigation.pageId)) {
145
+ global.__mpxPageStatusMap[navigation.pageId] = 'hide'
146
+ }
147
+ }
148
+ }
93
149
 
94
150
  global.__mpxAppLaunched = false
95
151
  global.__mpxOptionsMap[currentInject.moduleId] = memo((props) => {
@@ -127,47 +183,18 @@ export default function createApp (options) {
127
183
  global.__mpxLaunchOptions = options
128
184
  defaultOptions.onLaunch && defaultOptions.onLaunch.call(appInstance, options)
129
185
  }
130
- global.__mpxAppCbs.show.forEach((cb) => {
131
- cb(options)
132
- })
186
+ appState.showOptions = options
187
+ appState.state = 'show'
133
188
  global.__mpxAppLaunched = true
134
189
  global.__mpxAppHotLaunched = true
135
190
  }
136
191
  }
137
192
 
138
193
  useEffect(() => {
139
- const changeSubscription = ReactNative.AppState.addEventListener('change', (currentState) => {
140
- if (currentState === 'active') {
141
- let options = global.__mpxEnterOptions
142
- const navigation = getFocusedNavigation()
143
- if (navigation) {
144
- const state = navigation.getState()
145
- const current = state.routes[state.index]
146
- options = {
147
- path: current.name,
148
- query: current.params,
149
- scene: 0,
150
- shareTicket: '',
151
- referrerInfo: {}
152
- }
153
- }
154
- global.__mpxAppCbs.show.forEach((cb) => {
155
- cb(options)
156
- })
157
- if (navigation && hasOwn(global.__mpxPageStatusMap, navigation.pageId)) {
158
- global.__mpxPageStatusMap[navigation.pageId] = 'show'
159
- }
160
- } else if (currentState === 'inactive' || currentState === 'background') {
161
- global.__mpxAppCbs.hide.forEach((cb) => {
162
- cb({
163
- reason: 3
164
- })
165
- })
166
- const navigation = getFocusedNavigation()
167
- if (navigation && hasOwn(global.__mpxPageStatusMap, navigation.pageId)) {
168
- global.__mpxPageStatusMap[navigation.pageId] = 'hide'
169
- }
170
- }
194
+ const changeSubscription = ReactNative.AppState.addEventListener('change', (state) => {
195
+ // 外层可能会异常设置此配置,因此加载监听函数内部
196
+ if (Mpx.config.rnConfig.disableAppStateListener) return
197
+ onAppStateChange(state)
171
198
  })
172
199
 
173
200
  let count = 0
@@ -182,12 +209,8 @@ export default function createApp (options) {
182
209
  }
183
210
  })
184
211
  return () => {
185
- // todo 跳到原生页面或者其他rn bundle可以考虑使用reason 1/2进行模拟抹平
186
- global.__mpxAppCbs.hide.forEach((cb) => {
187
- cb({
188
- reason: 0
189
- })
190
- })
212
+ appState.hideReason = 0
213
+ appState.state = 'hide'
191
214
  changeSubscription && changeSubscription.remove()
192
215
  resizeSubScription && resizeSubScription.remove()
193
216
  }
@@ -256,4 +279,12 @@ export default function createApp (options) {
256
279
  global.__mpxPageStatusMap[navigation.pageId] = status
257
280
  }
258
281
  }
282
+
283
+ // 用于外层业务用来设置App的展示情况
284
+ global.setAppShow = function () {
285
+ onAppStateChange('active')
286
+ }
287
+ global.setAppHide = function () {
288
+ onAppStateChange('inactive')
289
+ }
259
290
  }
@@ -392,7 +392,7 @@ function usePageEffect (mpxProxy, pageId) {
392
392
  } else if (/^resize/.test(newVal)) {
393
393
  triggerResizeEvent(mpxProxy)
394
394
  }
395
- })
395
+ }, { sync: true })
396
396
  }
397
397
  }
398
398
  return () => {
@@ -656,8 +656,13 @@ export function getDefaultOptions ({ type, rawOptions = {}, currentInject }) {
656
656
  return () => {
657
657
  proxy.unmounted()
658
658
  proxy.target.__resetInstance()
659
+ // 热更新下会销毁旧页面并创建新页面组件,且旧页面组件销毁时机晚于新页面组件创建,此时__mpxPagesMap中存储的为新页面组件,不应该删除
660
+ // 所以需要判断路由表中存储的页面实例是否为当前页面实例
659
661
  if (type === 'page') {
660
- delete global.__mpxPagesMap[props.route.key]
662
+ const routeKey = props.route.key
663
+ if (global.__mpxPagesMap[routeKey] && global.__mpxPagesMap[routeKey][0] === instance) {
664
+ delete global.__mpxPagesMap[routeKey]
665
+ }
661
666
  }
662
667
  }
663
668
  }, [])