@mpxjs/core 2.10.4-beta.9 → 2.10.5-beta.1

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/@types/node.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  declare let global: Record<string, any> // in web, we use global varible to do some things, here to declare
2
2
 
3
+ declare let mpxGlobal: Record<string, any>
4
+
3
5
  type Dict<T> = {
4
6
  [k: string]: T | undefined
5
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mpxjs/core",
3
- "version": "2.10.4-beta.9",
3
+ "version": "2.10.5-beta.1",
4
4
  "description": "mpx runtime core",
5
5
  "keywords": [
6
6
  "miniprogram",
@@ -19,7 +19,7 @@
19
19
  ],
20
20
  "main": "src/index.js",
21
21
  "dependencies": {
22
- "@mpxjs/utils": "^2.10.4",
22
+ "@mpxjs/utils": "^2.10.4 | ^2.10.5-beta.1",
23
23
  "lodash": "^4.1.1",
24
24
  "miniprogram-api-typings": "^3.10.0"
25
25
  },
@@ -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,36 +92,55 @@ export default function createApp (options) {
90
92
  global.__navigationHelper.lastFailCallback = null
91
93
  }
92
94
  }
93
-
94
- const onAppStateChange = (currentState) => {
95
- // 业务上配置禁止的话就不响应监听事件
96
- if (currentState === 'active') {
97
- let options = global.__mpxEnterOptions || {}
98
- const navigation = getFocusedNavigation()
99
- if (navigation) {
100
- const state = navigation.getState()
101
- const current = state.routes[state.index]
102
- options = {
103
- path: current.name,
104
- query: current.params,
105
- scene: 0,
106
- shareTicket: '',
107
- referrerInfo: {}
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 = {}
108
119
  }
109
120
  }
110
121
  global.__mpxAppCbs.show.forEach((cb) => {
111
122
  cb(options)
112
123
  })
113
- if (navigation && hasOwn(global.__mpxPageStatusMap, navigation.pageId)) {
114
- global.__mpxPageStatusMap[navigation.pageId] = 'show'
115
- }
116
- } else if (currentState === 'inactive' || currentState === 'background') {
124
+ } else if (value === 'hide') {
125
+ const reason = appState.hideReason ?? 3
126
+ delete appState.hideReason
117
127
  global.__mpxAppCbs.hide.forEach((cb) => {
118
128
  cb({
119
- reason: 3
129
+ reason
120
130
  })
121
131
  })
122
- const navigation = getFocusedNavigation()
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'
123
144
  if (navigation && hasOwn(global.__mpxPageStatusMap, navigation.pageId)) {
124
145
  global.__mpxPageStatusMap[navigation.pageId] = 'hide'
125
146
  }
@@ -162,9 +183,8 @@ export default function createApp (options) {
162
183
  global.__mpxLaunchOptions = options
163
184
  defaultOptions.onLaunch && defaultOptions.onLaunch.call(appInstance, options)
164
185
  }
165
- global.__mpxAppCbs.show.forEach((cb) => {
166
- cb(options)
167
- })
186
+ appState.showOptions = options
187
+ appState.state = 'show'
168
188
  global.__mpxAppLaunched = true
169
189
  global.__mpxAppHotLaunched = true
170
190
  }
@@ -173,7 +193,7 @@ export default function createApp (options) {
173
193
  useEffect(() => {
174
194
  const changeSubscription = ReactNative.AppState.addEventListener('change', (state) => {
175
195
  // 外层可能会异常设置此配置,因此加载监听函数内部
176
- if (Mpx.config.rnConfig.disableReactNativeAppStateChange) return
196
+ if (Mpx.config.rnConfig.disableAppStateListener) return
177
197
  onAppStateChange(state)
178
198
  })
179
199
 
@@ -189,12 +209,8 @@ export default function createApp (options) {
189
209
  }
190
210
  })
191
211
  return () => {
192
- // todo 跳到原生页面或者其他rn bundle可以考虑使用reason 1/2进行模拟抹平
193
- global.__mpxAppCbs.hide.forEach((cb) => {
194
- cb({
195
- reason: 0
196
- })
197
- })
212
+ appState.hideReason = 0
213
+ appState.state = 'hide'
198
214
  changeSubscription && changeSubscription.remove()
199
215
  resizeSubScription && resizeSubScription.remove()
200
216
  }
@@ -235,18 +251,11 @@ export default function createApp (options) {
235
251
  return []
236
252
  }
237
253
 
238
- global.setCurrentPageStatus = function (status) {
239
- const navigation = getFocusedNavigation()
240
- if (navigation && hasOwn(global.__mpxPageStatusMap, navigation.pageId)) {
241
- global.__mpxPageStatusMap[navigation.pageId] = status
242
- }
243
- }
244
-
245
254
  // 用于外层业务用来设置App的展示情况
246
255
  global.setAppShow = function () {
247
256
  onAppStateChange('active')
248
257
  }
249
258
  global.setAppHide = function () {
250
- onAppStateChange('background')
259
+ onAppStateChange('inactive')
251
260
  }
252
261
  }
@@ -3,6 +3,8 @@ import { createI18n } from '../builtInMixins/i18nMixin'
3
3
  import * as navigationHelper from './navigationHelper'
4
4
 
5
5
  export function init (Mpx) {
6
+ // 为避免多个mpx应用运行时互相覆盖global __mpx对象,导致业务异常,例如插件模式下,插件应用和业务应用互相覆盖global.__mpx,因此创建mpxGlobal局部对象
7
+ mpxGlobal.__mpx = Mpx
6
8
  global.__mpx = Mpx
7
9
  global.__mpxAppCbs = global.__mpxAppCbs || {
8
10
  show: [],
@@ -1,6 +1,8 @@
1
1
  import { createI18n } from '../builtInMixins/i18nMixin'
2
2
 
3
3
  export function init (Mpx) {
4
+ // 为避免多个mpx应用运行时互相覆盖global __mpx对象,导致业务异常,例如插件模式下,插件应用和业务应用互相覆盖global.__mpx,因此创建mpxGlobal局部对象
5
+ mpxGlobal.__mpx = Mpx
4
6
  global.__mpx = Mpx
5
7
  if (global.i18n) {
6
8
  Mpx.i18n = createI18n(global.i18n)
@@ -4,6 +4,8 @@ import { isBrowser, error, warn } from '@mpxjs/utils'
4
4
  import { initEvent } from './event'
5
5
 
6
6
  export function init (Mpx) {
7
+ // 为避免多个mpx应用运行时互相覆盖global __mpx对象,导致业务异常,例如插件模式下,插件应用和业务应用互相覆盖global.__mpx,因此创建mpxGlobal局部对象
8
+ mpxGlobal.__mpx = Mpx
7
9
  global.__mpx = Mpx
8
10
  global.__mpxAppCbs = global.__mpxAppCbs || {
9
11
  show: [],
@@ -303,15 +303,8 @@ function createInstance ({ propsRef, type, rawOptions, currentInject, validProps
303
303
  proxy.created()
304
304
 
305
305
  if (type === 'page') {
306
- const loadParams = {}
307
306
  const props = propsRef.current
308
- // 此处拿到的props.route.params内属性的value被进行过了一次decode, 不符合预期,此处额外进行一次encode来与微信对齐
309
- if (isObject(props.route.params)) {
310
- for (const key in props.route.params) {
311
- loadParams[key] = encodeURIComponent(props.route.params[key])
312
- }
313
- }
314
- proxy.callHook(ONLOAD, [loadParams])
307
+ proxy.callHook(ONLOAD, [props.route.params || {}])
315
308
  }
316
309
 
317
310
  Object.assign(proxy, {
@@ -393,7 +386,7 @@ function usePageEffect (mpxProxy, pageId) {
393
386
  } else if (/^resize/.test(newVal)) {
394
387
  triggerResizeEvent(mpxProxy)
395
388
  }
396
- })
389
+ }, { sync: true })
397
390
  }
398
391
  }
399
392
  return () => {
@@ -631,8 +624,13 @@ export function getDefaultOptions ({ type, rawOptions = {}, currentInject }) {
631
624
  return () => {
632
625
  proxy.unmounted()
633
626
  proxy.target.__resetInstance()
627
+ // 热更新下会销毁旧页面并创建新页面组件,且旧页面组件销毁时机晚于新页面组件创建,此时__mpxPagesMap中存储的为新页面组件,不应该删除
628
+ // 所以需要判断路由表中存储的页面实例是否为当前页面实例
634
629
  if (type === 'page') {
635
- delete global.__mpxPagesMap[props.route.key]
630
+ const routeKey = props.route.key
631
+ if (global.__mpxPagesMap[routeKey] && global.__mpxPagesMap[routeKey][0] === instance) {
632
+ delete global.__mpxPagesMap[routeKey]
633
+ }
636
634
  }
637
635
  }
638
636
  }, [])