@mpxjs/core 2.10.3-beta.4 → 2.10.3-beta.6

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.3-beta.4",
3
+ "version": "2.10.3-beta.6",
4
4
  "description": "mpx runtime core",
5
5
  "keywords": [
6
6
  "miniprogram",
@@ -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'
@@ -91,6 +93,57 @@ export default function createApp (options) {
91
93
  global.__navigationHelper.lastFailCallback = null
92
94
  }
93
95
  }
96
+ const appState = reactive({ state: '' })
97
+ // TODO hideReason 暂未完全模拟
98
+ // 0用户退出小程序
99
+ // 1进入其他小程序
100
+ // 2打开原生功能页
101
+ // 3其他
102
+ watch(() => appState.state, (value) => {
103
+ if (value === 'show') {
104
+ let options = appState.showOptions
105
+ delete appState.showOptions
106
+ if (!options) {
107
+ const navigation = getFocusedNavigation()
108
+ if (navigation) {
109
+ const state = navigation.getState()
110
+ const current = state.routes[state.index]
111
+ options = {
112
+ path: current.name,
113
+ query: current.params,
114
+ scene: 0,
115
+ shareTicket: '',
116
+ referrerInfo: {}
117
+ }
118
+ }
119
+ }
120
+ global.__mpxAppCbs.show.forEach((cb) => {
121
+ cb(options || {})
122
+ })
123
+ } else if (value === 'hide') {
124
+ global.__mpxAppCbs.hide.forEach((cb) => {
125
+ cb({
126
+ reason: appState.hideReason ?? 3
127
+ })
128
+ delete appState.hideReason
129
+ })
130
+ }
131
+ }, { sync: true })
132
+ const onAppStateChange = (currentState) => {
133
+ const navigation = getFocusedNavigation()
134
+ if (currentState === 'active') {
135
+ appState.state = 'show'
136
+ if (navigation && hasOwn(global.__mpxPageStatusMap, navigation.pageId)) {
137
+ global.__mpxPageStatusMap[navigation.pageId] = 'show'
138
+ }
139
+ } else if (currentState === 'inactive' || currentState === 'background') {
140
+ appState.hideReason = 3
141
+ appState.state = 'hide'
142
+ if (navigation && hasOwn(global.__mpxPageStatusMap, navigation.pageId)) {
143
+ global.__mpxPageStatusMap[navigation.pageId] = 'hide'
144
+ }
145
+ }
146
+ }
94
147
 
95
148
  global.__mpxAppLaunched = false
96
149
  global.__mpxOptionsMap[currentInject.moduleId] = memo((props) => {
@@ -128,47 +181,18 @@ export default function createApp (options) {
128
181
  global.__mpxLaunchOptions = options
129
182
  defaultOptions.onLaunch && defaultOptions.onLaunch.call(appInstance, options)
130
183
  }
131
- global.__mpxAppCbs.show.forEach((cb) => {
132
- cb(options)
133
- })
184
+ appState.showOptions = options
185
+ appState.state = 'show'
134
186
  global.__mpxAppLaunched = true
135
187
  global.__mpxAppHotLaunched = true
136
188
  }
137
189
  }
138
190
 
139
191
  useEffect(() => {
140
- const changeSubscription = ReactNative.AppState.addEventListener('change', (currentState) => {
141
- if (currentState === 'active') {
142
- let options = global.__mpxEnterOptions
143
- const navigation = getFocusedNavigation()
144
- if (navigation) {
145
- const state = navigation.getState()
146
- const current = state.routes[state.index]
147
- options = {
148
- path: current.name,
149
- query: current.params,
150
- scene: 0,
151
- shareTicket: '',
152
- referrerInfo: {}
153
- }
154
- }
155
- global.__mpxAppCbs.show.forEach((cb) => {
156
- cb(options)
157
- })
158
- if (navigation && hasOwn(global.__mpxPageStatusMap, navigation.pageId)) {
159
- global.__mpxPageStatusMap[navigation.pageId] = 'show'
160
- }
161
- } else if (currentState === 'inactive' || currentState === 'background') {
162
- global.__mpxAppCbs.hide.forEach((cb) => {
163
- cb({
164
- reason: 3
165
- })
166
- })
167
- const navigation = getFocusedNavigation()
168
- if (navigation && hasOwn(global.__mpxPageStatusMap, navigation.pageId)) {
169
- global.__mpxPageStatusMap[navigation.pageId] = 'hide'
170
- }
171
- }
192
+ const changeSubscription = ReactNative.AppState.addEventListener('change', (state) => {
193
+ // 外层可能会异常设置此配置,因此加载监听函数内部
194
+ if (Mpx.config.rnConfig.disableAppStateListener) return
195
+ onAppStateChange(state)
172
196
  })
173
197
 
174
198
  let count = 0
@@ -184,11 +208,8 @@ export default function createApp (options) {
184
208
  })
185
209
  return () => {
186
210
  // todo 跳到原生页面或者其他rn bundle可以考虑使用reason 1/2进行模拟抹平
187
- global.__mpxAppCbs.hide.forEach((cb) => {
188
- cb({
189
- reason: 0
190
- })
191
- })
211
+ appState.hideReason = 0
212
+ appState.state = 'hide'
192
213
  changeSubscription && changeSubscription.remove()
193
214
  resizeSubScription && resizeSubScription.remove()
194
215
  }
@@ -257,4 +278,12 @@ export default function createApp (options) {
257
278
  global.__mpxPageStatusMap[navigation.pageId] = status
258
279
  }
259
280
  }
281
+
282
+ // 用于外层业务用来设置App的展示情况
283
+ global.setAppShow = function () {
284
+ onAppStateChange('active')
285
+ }
286
+ global.setAppHide = function () {
287
+ onAppStateChange('inactive')
288
+ }
260
289
  }
@@ -12,10 +12,12 @@ import { createSelectorQuery, createIntersectionObserver } from '@mpxjs/api-prox
12
12
  import { IntersectionObserverContext, RouteContext, KeyboardAvoidContext } from '@mpxjs/webpack-plugin/lib/runtime/components/react/dist/context'
13
13
  import MpxKeyboardAvoidingView from '@mpxjs/webpack-plugin/lib/runtime/components/react/dist/mpx-keyboard-avoiding-view'
14
14
 
15
- const ProviderContext = createContext(null)
16
15
  const windowDimensions = ReactNative.Dimensions.get('window')
17
16
  const screenDimensions = ReactNative.Dimensions.get('screen')
17
+ const ProviderContext = createContext(null)
18
18
  function getSystemInfo () {
19
+ const windowDimensions = ReactNative.Dimensions.get('window')
20
+ const screenDimensions = ReactNative.Dimensions.get('screen')
19
21
  return {
20
22
  deviceOrientation: windowDimensions.width > windowDimensions.height ? 'landscape' : 'portrait',
21
23
  size: {
@@ -46,7 +48,7 @@ function createEffect (proxy, components) {
46
48
  if (tagName === 'block') return Fragment
47
49
  const appComponents = global.__getAppComponents?.() || {}
48
50
  const generichash = proxy.target.generichash || ''
49
- const genericComponents = global.__mpxGenericsMap[generichash] || noop
51
+ const genericComponents = global.__mpxGenericsMap?.[generichash] || noop
50
52
  return components[tagName] || genericComponents(tagName) || appComponents[tagName] || getByPath(ReactNative, tagName)
51
53
  }
52
54
  const innerCreateElement = (type, ...rest) => {
@@ -387,7 +389,7 @@ function usePageEffect (mpxProxy, pageId) {
387
389
  } else if (/^resize/.test(newVal)) {
388
390
  triggerResizeEvent(mpxProxy)
389
391
  }
390
- })
392
+ }, { sync: true })
391
393
  }
392
394
  }
393
395
  return () => {