@tarojs/plugin-platform-harmony-cpp 4.1.0 → 4.1.1-alpha.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.
Files changed (225) hide show
  1. package/README.md +0 -7
  2. package/dist/index.js +1236 -0
  3. package/dist/runtime/apis/apis.ts +20 -0
  4. package/dist/runtime/apis/base/crypto.ts +4 -0
  5. package/dist/runtime/apis/base/debug.ts +5 -0
  6. package/dist/runtime/apis/base/index.ts +13 -0
  7. package/dist/runtime/apis/base/performance.ts +8 -0
  8. package/dist/runtime/apis/base/system.ts +181 -0
  9. package/dist/runtime/apis/base/update.ts +5 -0
  10. package/dist/runtime/apis/base/weapp/app-event.ts +75 -0
  11. package/dist/runtime/apis/base/weapp/life-cycle.ts +21 -0
  12. package/dist/runtime/apis/canvas/index.ts +27 -0
  13. package/dist/runtime/apis/data-analysis/index.ts +6 -0
  14. package/dist/runtime/apis/device/accelerometer.ts +79 -0
  15. package/dist/runtime/apis/device/accessibility.ts +4 -0
  16. package/dist/runtime/apis/device/battery.ts +24 -0
  17. package/dist/runtime/apis/device/bluetooth-ble.ts +19 -0
  18. package/dist/runtime/apis/device/bluetooth-peripheral.ts +6 -0
  19. package/dist/runtime/apis/device/bluetooth.ts +16 -0
  20. package/dist/runtime/apis/device/calendar.ts +5 -0
  21. package/dist/runtime/apis/device/clipboard.ts +85 -0
  22. package/dist/runtime/apis/device/compass.ts +21 -0
  23. package/dist/runtime/apis/device/contact.ts +5 -0
  24. package/dist/runtime/apis/device/crypto.ts +4 -0
  25. package/dist/runtime/apis/device/gyroscope.ts +77 -0
  26. package/dist/runtime/apis/device/iBeacon.ts +10 -0
  27. package/dist/runtime/apis/device/index.ts +24 -0
  28. package/dist/runtime/apis/device/keyboard.ts +63 -0
  29. package/dist/runtime/apis/device/memory.ts +11 -0
  30. package/dist/runtime/apis/device/motion.ts +6 -0
  31. package/dist/runtime/apis/device/network.ts +123 -0
  32. package/dist/runtime/apis/device/nfc.ts +10 -0
  33. package/dist/runtime/apis/device/phone.ts +44 -0
  34. package/dist/runtime/apis/device/scan.ts +4 -0
  35. package/dist/runtime/apis/device/screen.ts +80 -0
  36. package/dist/runtime/apis/device/sms.ts +4 -0
  37. package/dist/runtime/apis/device/vibrate.ts +32 -0
  38. package/dist/runtime/apis/device/wifi.ts +15 -0
  39. package/dist/runtime/apis/ext/index.ts +5 -0
  40. package/dist/runtime/apis/files/index.ts +136 -0
  41. package/dist/runtime/apis/files/manager.ts +942 -0
  42. package/dist/runtime/apis/framework/index.ts +48 -0
  43. package/dist/runtime/apis/harmony/task-pool.ts +39 -0
  44. package/dist/runtime/apis/index.ts +61 -0
  45. package/dist/runtime/apis/location/index.ts +133 -0
  46. package/dist/runtime/apis/media/EditorContext.ts +32 -0
  47. package/dist/runtime/apis/media/audio/index.ts +36 -0
  48. package/dist/runtime/apis/media/background-audio/index.ts +16 -0
  49. package/dist/runtime/apis/media/camera.ts +16 -0
  50. package/dist/runtime/apis/media/common.ts +58 -0
  51. package/dist/runtime/apis/media/image/index.ts +291 -0
  52. package/dist/runtime/apis/media/index.ts +13 -0
  53. package/dist/runtime/apis/media/live.ts +5 -0
  54. package/dist/runtime/apis/media/map.ts +4 -0
  55. package/dist/runtime/apis/media/media-recorder.ts +4 -0
  56. package/dist/runtime/apis/media/recorder.ts +6 -0
  57. package/dist/runtime/apis/media/video/VideoContext.ts +67 -0
  58. package/dist/runtime/apis/media/video/index.ts +45 -0
  59. package/dist/runtime/apis/media/video-decoder.ts +4 -0
  60. package/dist/runtime/apis/media/video-processing.ts +4 -0
  61. package/dist/runtime/apis/media/voip.ts +19 -0
  62. package/dist/runtime/apis/navigate/index.ts +8 -0
  63. package/dist/runtime/apis/network/downloadFile.ts +85 -0
  64. package/dist/runtime/apis/network/index.ts +7 -0
  65. package/dist/runtime/apis/network/mdns.ts +13 -0
  66. package/dist/runtime/apis/network/request.ts +140 -0
  67. package/dist/runtime/apis/network/tcp.ts +4 -0
  68. package/dist/runtime/apis/network/udp.ts +4 -0
  69. package/dist/runtime/apis/network/uploadFile.ts +105 -0
  70. package/dist/runtime/apis/network/webSocket.ts +126 -0
  71. package/dist/runtime/apis/open-api/account.ts +4 -0
  72. package/dist/runtime/apis/open-api/address.ts +4 -0
  73. package/dist/runtime/apis/open-api/authorize.ts +5 -0
  74. package/dist/runtime/apis/open-api/card.ts +5 -0
  75. package/dist/runtime/apis/open-api/channels-live.ts +11 -0
  76. package/dist/runtime/apis/open-api/customer-service.ts +4 -0
  77. package/dist/runtime/apis/open-api/device-voip.ts +5 -0
  78. package/dist/runtime/apis/open-api/facial.ts +7 -0
  79. package/dist/runtime/apis/open-api/favorites.ts +5 -0
  80. package/dist/runtime/apis/open-api/group.ts +4 -0
  81. package/dist/runtime/apis/open-api/index.ts +21 -0
  82. package/dist/runtime/apis/open-api/invoice.ts +5 -0
  83. package/dist/runtime/apis/open-api/license-plate.ts +4 -0
  84. package/dist/runtime/apis/open-api/login.ts +6 -0
  85. package/dist/runtime/apis/open-api/my-miniprogram.ts +4 -0
  86. package/dist/runtime/apis/open-api/privacy.ts +7 -0
  87. package/dist/runtime/apis/open-api/red-package.ts +4 -0
  88. package/dist/runtime/apis/open-api/settings.ts +5 -0
  89. package/dist/runtime/apis/open-api/soter.ts +6 -0
  90. package/dist/runtime/apis/open-api/subscribe-message.ts +6 -0
  91. package/dist/runtime/apis/open-api/user-info.ts +70 -0
  92. package/dist/runtime/apis/open-api/werun.ts +5 -0
  93. package/dist/runtime/apis/payment/index.ts +6 -0
  94. package/dist/runtime/apis/route/index.ts +88 -0
  95. package/dist/runtime/apis/share/index.ts +33 -0
  96. package/dist/runtime/apis/storage/background-fetch.ts +7 -0
  97. package/dist/runtime/apis/storage/cache-manager.ts +4 -0
  98. package/dist/runtime/apis/storage/index.ts +203 -0
  99. package/dist/runtime/apis/ui/animation/animation.ts +264 -0
  100. package/dist/runtime/apis/ui/animation/index.ts +7 -0
  101. package/dist/runtime/apis/ui/background.ts +20 -0
  102. package/dist/runtime/apis/ui/custom-component.ts +1 -0
  103. package/dist/runtime/apis/ui/fonts.ts +4 -0
  104. package/dist/runtime/apis/ui/index.ts +12 -0
  105. package/dist/runtime/apis/ui/interaction/index.ts +198 -0
  106. package/dist/runtime/apis/ui/menu.ts +4 -0
  107. package/dist/runtime/apis/ui/navigation-bar/index.ts +72 -0
  108. package/dist/runtime/apis/ui/pull-down-refresh.ts +47 -0
  109. package/dist/runtime/apis/ui/scroll/index.ts +82 -0
  110. package/dist/runtime/apis/ui/sticky.ts +4 -0
  111. package/dist/runtime/apis/ui/tab-bar.ts +144 -0
  112. package/dist/runtime/apis/ui/window.ts +20 -0
  113. package/dist/runtime/apis/utils/constant.ts +1 -0
  114. package/dist/runtime/apis/utils/handler.ts +117 -0
  115. package/dist/runtime/apis/utils/index.ts +105 -0
  116. package/dist/runtime/apis/utils/permissions.ts +6 -0
  117. package/dist/runtime/apis/utils/types.ts +12 -0
  118. package/dist/runtime/apis/utils/unit.ts +104 -0
  119. package/dist/runtime/apis/utils/validate.ts +87 -0
  120. package/dist/runtime/apis/worker/index.ts +4 -0
  121. package/dist/runtime/apis/wxml/index.ts +17 -0
  122. package/dist/runtime/apis/wxml/intersectionObserver.ts +100 -0
  123. package/dist/runtime/apis/wxml/nodesRef.ts +58 -0
  124. package/dist/runtime/apis/wxml/selectorQuery.ts +221 -0
  125. package/dist/runtime/apischunk/index.d.ts +802 -0
  126. package/dist/runtime/apischunk/index.js +5054 -0
  127. package/dist/runtime/components/deprecated.ets +43 -0
  128. package/dist/runtime/components/index.ets +49 -0
  129. package/dist/runtime/components/innerHtml.ets +16 -0
  130. package/dist/runtime/components/navigationBar.ets +65 -0
  131. package/dist/runtime/components/pageMeta.ets +94 -0
  132. package/dist/runtime/components/richText.ets +24 -0
  133. package/dist/runtime/components/slider.ets +119 -0
  134. package/dist/runtime/components/style.ets +286 -0
  135. package/dist/runtime/components/switch.ets +73 -0
  136. package/dist/runtime/components/tag.ts +58 -0
  137. package/dist/runtime/components/utils/AttributeManager.ets +252 -0
  138. package/dist/runtime/components/utils/DynamicCenter.ts +41 -0
  139. package/dist/runtime/components/utils/constant/event.ets +25 -0
  140. package/dist/runtime/components/utils/constant/style.ets +91 -0
  141. package/dist/runtime/components/utils/events.ts +26 -0
  142. package/dist/runtime/components/utils/flexManager.ets +49 -0
  143. package/dist/runtime/components/utils/helper.ets +51 -0
  144. package/dist/runtime/components/utils/htmlParser/HarmonyHTMLParser.ts +100 -0
  145. package/dist/runtime/components/utils/htmlParser/index.ts +58 -0
  146. package/dist/runtime/components/utils/index.ts +77 -0
  147. package/dist/runtime/components/utils/styles.ets +41 -0
  148. package/dist/runtime/components/video.ets +119 -0
  149. package/dist/runtime/components/webView.ets +55 -0
  150. package/dist/runtime/components/xComponent.ets +89 -0
  151. package/dist/runtime/framework/app.ts +248 -0
  152. package/dist/runtime/framework/connect.ts +24 -0
  153. package/dist/runtime/framework/constant.ts +3 -0
  154. package/dist/runtime/framework/hooks.ts +99 -0
  155. package/dist/runtime/framework/index.ts +15 -0
  156. package/dist/runtime/framework/native-page.ts +511 -0
  157. package/dist/runtime/framework/page.ts +256 -0
  158. package/dist/runtime/framework/utils/index.ts +17 -0
  159. package/dist/runtime/framework/utils/is.ts +26 -0
  160. package/dist/runtime/framework-reconciler/componentTree.ts +81 -0
  161. package/dist/runtime/framework-reconciler/constant.ts +86 -0
  162. package/dist/runtime/framework-reconciler/domInput.ts +90 -0
  163. package/dist/runtime/framework-reconciler/event.ts +108 -0
  164. package/dist/runtime/framework-reconciler/index.ts +99 -0
  165. package/dist/runtime/framework-reconciler/inputValueTracking.ts +106 -0
  166. package/dist/runtime/framework-reconciler/props.ts +132 -0
  167. package/dist/runtime/framework-reconciler/reconciler.ts +202 -0
  168. package/dist/runtime/framework-reconciler/render.ts +139 -0
  169. package/dist/runtime/framework-reconciler/workTags.ts +53 -0
  170. package/dist/runtime/runtime-cpp/bom/URL.ts +2 -0
  171. package/dist/runtime/runtime-cpp/bom/document.ts +45 -0
  172. package/dist/runtime/runtime-cpp/bom/history.ts +1 -0
  173. package/dist/runtime/runtime-cpp/bom/location.ts +1 -0
  174. package/dist/runtime/runtime-cpp/bom/navigator.ts +1 -0
  175. package/dist/runtime/runtime-cpp/bom/raf.ts +1 -0
  176. package/dist/runtime/runtime-cpp/bom/window.ts +57 -0
  177. package/dist/runtime/runtime-cpp/constant.ts +29 -0
  178. package/dist/runtime/runtime-cpp/current.ts +48 -0
  179. package/dist/runtime/runtime-cpp/dom/class-list.ts +41 -0
  180. package/dist/runtime/runtime-cpp/dom/comment.ts +9 -0
  181. package/dist/runtime/runtime-cpp/dom/dataSource.ts +87 -0
  182. package/dist/runtime/runtime-cpp/dom/document.ts +113 -0
  183. package/dist/runtime/runtime-cpp/dom/element/canvas.ts +285 -0
  184. package/dist/runtime/runtime-cpp/dom/element/element.ts +262 -0
  185. package/dist/runtime/runtime-cpp/dom/element/form.ts +371 -0
  186. package/dist/runtime/runtime-cpp/dom/element/index.ts +129 -0
  187. package/dist/runtime/runtime-cpp/dom/element/movable.ts +250 -0
  188. package/dist/runtime/runtime-cpp/dom/element/normal.ts +123 -0
  189. package/dist/runtime/runtime-cpp/dom/element/scroll_view.ts +32 -0
  190. package/dist/runtime/runtime-cpp/dom/element/text.ts +9 -0
  191. package/dist/runtime/runtime-cpp/dom/element/video.ts +66 -0
  192. package/dist/runtime/runtime-cpp/dom/element/web_view.ts +72 -0
  193. package/dist/runtime/runtime-cpp/dom/event-source.ts +1 -0
  194. package/dist/runtime/runtime-cpp/dom/event.ts +187 -0
  195. package/dist/runtime/runtime-cpp/dom/eventTarget.ts +81 -0
  196. package/dist/runtime/runtime-cpp/dom/node.ts +307 -0
  197. package/dist/runtime/runtime-cpp/dom/style.ts +44 -0
  198. package/dist/runtime/runtime-cpp/dom/stylesheet/index.ts +379 -0
  199. package/dist/runtime/runtime-cpp/dom/stylesheet/type.ts +212 -0
  200. package/dist/runtime/runtime-cpp/dom/stylesheet/util.ts +186 -0
  201. package/dist/runtime/runtime-cpp/emitter/emitter.ts +29 -0
  202. package/dist/runtime/runtime-cpp/env.ts +1 -0
  203. package/dist/runtime/runtime-cpp/harmony-library.ts +1 -0
  204. package/dist/runtime/runtime-cpp/index.ts +79 -0
  205. package/dist/runtime/runtime-cpp/interface/event.ts +10 -0
  206. package/dist/runtime/runtime-cpp/interface/index.ts +7 -0
  207. package/dist/runtime/runtime-cpp/next-tick.ts +14 -0
  208. package/dist/runtime/runtime-cpp/system.ts +213 -0
  209. package/dist/runtime/runtime-cpp/utils/index.ts +63 -0
  210. package/dist/runtime/runtime-cpp/utils/info.ts +123 -0
  211. package/dist/runtime/runtime-cpp/utils/page.ts +12 -0
  212. package/dist/runtime/runtime-cpp/utils/router.ts +1 -0
  213. package/dist/runtime/runtime-harmony/apis/helper.ets +33 -0
  214. package/dist/runtime/runtime-harmony/apis/network/common.ets +41 -0
  215. package/dist/runtime/runtime-harmony/apis/network/downloadFile.ets +327 -0
  216. package/dist/runtime/runtime-harmony/apis/network/index.ets +58 -0
  217. package/dist/runtime/runtime-harmony/apis/network/request.ets +206 -0
  218. package/dist/runtime/runtime-harmony/apis/network/uploadFile.ets +190 -0
  219. package/dist/runtime/runtime-harmony/apis/route.ets +110 -0
  220. package/dist/runtime/runtime-harmony/apis/taskpool.ets +149 -0
  221. package/dist/runtime/runtime-harmony/index.ets +41 -0
  222. package/dist/runtime/runtime-harmony/utils.ts +53 -0
  223. package/package.json +25 -23
  224. package/static/@taro-oh/library-4.1.1-alpha.0.har +0 -0
  225. package/LICENSE +0 -174
@@ -0,0 +1,511 @@
1
+ import { addLeadingSlash, CONTEXT_ACTIONS, Current, document, env, eventCenter, getPageById, initStyleSheetConfig, removePageById, requestAnimationFrame, setPageById, window } from '@tarojs/runtime'
2
+ import { EMPTY_OBJ, hooks, isUndefined } from '@tarojs/shared'
3
+
4
+ import { ReactMeta as reactMeta } from './app'
5
+ import { setReconciler } from './connect'
6
+ import { ON_HIDE, ON_READY, ON_SHOW } from './constant'
7
+ import {
8
+ getOnHideEventKey,
9
+ getOnReadyEventKey,
10
+ getOnShowEventKey,
11
+ getPath,
12
+ injectPageInstance,
13
+ removePageInstance,
14
+ safeExecute,
15
+ } from './page'
16
+ import { incrementId, isClassComponent } from './utils'
17
+
18
+ import type { TaroElement, TFunc } from '@tarojs/runtime'
19
+ import type * as React from 'react'
20
+
21
+ const getNativeCompId = incrementId(1)
22
+ let h: typeof React.createElement
23
+ let ReactDOM
24
+
25
+ interface InitNativeComponentEntryParams {
26
+ R: typeof React
27
+ ReactDOM: typeof ReactDOM
28
+ cb?: TFunc
29
+ isUseReact18?: boolean
30
+ }
31
+
32
+ function initNativeComponentEntry (params: InitNativeComponentEntryParams) {
33
+ const { R, ReactDOM, cb, isUseReact18 = true } = params
34
+
35
+ interface IEntryState {
36
+ components: {
37
+ compId: string
38
+ element: React.ReactElement
39
+ }[]
40
+ }
41
+
42
+ interface IWrapperProps {
43
+ compId: string
44
+ getCtx: () => any
45
+ renderComponent: (ctx: any) => React.ReactElement
46
+ }
47
+
48
+ class NativeComponentWrapper extends R.Component<IWrapperProps, Record<any, any>> {
49
+ root = R.createRef<TaroElement>()
50
+ ctx = this.props.getCtx()
51
+
52
+ state = {
53
+ hasError: false,
54
+ }
55
+
56
+ componentDidMount () {
57
+ this.ctx.component = this
58
+ if (this.root.current) {
59
+ // @ts-ignore
60
+ this.root.current = this.ctx
61
+ }
62
+ }
63
+
64
+ // React 16 uncaught error 会导致整个应用 crash,
65
+ // 目前把错误缩小到页面,否则任何页面的错误都会导致所有页面的白屏
66
+ componentDidCatch (error, info: React.ErrorInfo) {
67
+ if (Current.isDebug) {
68
+ setTimeout(() => {
69
+ throw Error(`[TARO_LOG] ErrorPage(${this.props.compId}) 报错信息:${error}\n${info.componentStack}`)
70
+ }, 20)
71
+ } else {
72
+ console.error(`[TARO_LOG] ErrorPage(${this.props.compId}) 报错信息:${error}\n${info.componentStack}`)
73
+ }
74
+ }
75
+
76
+ static getDerivedStateFromError (error) {
77
+ return {
78
+ hasError: !!error
79
+ }
80
+ }
81
+
82
+ render () {
83
+ return !this.state.hasError ? h(
84
+ 'taro-page',
85
+ {
86
+ ref: this.root,
87
+ id: this.props.compId,
88
+ },
89
+ this.props.renderComponent(this.ctx)
90
+ ) : null
91
+ }
92
+ }
93
+
94
+ class Entry extends R.Component<Record<any, any>, IEntryState> {
95
+ state: IEntryState = {
96
+ components: [],
97
+ }
98
+
99
+ componentDidMount () {
100
+ if (isUseReact18) {
101
+ if (Current.entryAsync) {
102
+ Current.entryAsync = Object.assign(this, Current.entryAsync)
103
+ } else {
104
+ Current.entryAsync = this
105
+ }
106
+ } else {
107
+ if (Current.app) {
108
+ Current.app = Object.assign(this, Current.app)
109
+ } else {
110
+ Current.app = this
111
+ }
112
+ }
113
+
114
+ cb && cb()
115
+ }
116
+
117
+ mount (Component, compId, getCtx, cb?) {
118
+ const isReactComponent = isClassComponent(R, Component)
119
+ const inject = (node?: any) => node && injectPageInstance(node, compId)
120
+ const refs = isReactComponent
121
+ ? { ref: inject }
122
+ : {
123
+ forwardedRef: inject,
124
+ reactReduxForwardedRef: inject,
125
+ }
126
+ if (reactMeta.PageContext === EMPTY_OBJ) {
127
+ reactMeta.PageContext = R.createContext('')
128
+ }
129
+ const item = {
130
+ compId,
131
+ element: h(NativeComponentWrapper, {
132
+ key: compId,
133
+ compId,
134
+ getCtx,
135
+ renderComponent (ctx) {
136
+ return h(
137
+ reactMeta.PageContext.Provider,
138
+ { value: compId },
139
+ h(Component, {
140
+ // TODO: 传递 Props
141
+ ...(ctx.props || {}),
142
+ ...refs,
143
+ $scope: ctx,
144
+ })
145
+ )
146
+ },
147
+ }),
148
+ }
149
+
150
+ if (isUseReact18) {
151
+ ReactDOM.flushSync(() => {
152
+ this.setState(
153
+ {
154
+ components: [...this.state.components, item],
155
+ },
156
+ () => cb && cb()
157
+ )
158
+ })
159
+ } else {
160
+ this.setState(
161
+ {
162
+ components: [...this.state.components, item],
163
+ },
164
+ () => cb && cb()
165
+ )
166
+ }
167
+ }
168
+
169
+ unmount (compId, cb?) {
170
+ const components = this.state.components
171
+ const index = components.findIndex((item) => item.compId === compId)
172
+ // 如果找不到对应compound,说明已经删,不用多余处理
173
+ if (index === -1) return
174
+ const next = [...components.slice(0, index), ...components.slice(index + 1)]
175
+
176
+ if (isUseReact18) {
177
+ ReactDOM.flushSync(() => {
178
+ this.setState(
179
+ {
180
+ components: next,
181
+ },
182
+ () => {
183
+ removePageInstance(compId)
184
+ cb && cb()
185
+ }
186
+ )
187
+ })
188
+ } else {
189
+ this.setState(
190
+ {
191
+ components: next,
192
+ },
193
+ () => {
194
+ removePageInstance(compId)
195
+ cb && cb()
196
+ }
197
+ )
198
+ }
199
+ }
200
+
201
+ render () {
202
+ const components = this.state.components
203
+
204
+ return components.map(({ element }) => element)
205
+ }
206
+ }
207
+
208
+ setReconciler(ReactDOM)
209
+
210
+ // @ts-ignore
211
+ const app = isUseReact18 ? document.entryAsync : document.app
212
+ // eslint-disable-next-line react/no-deprecated
213
+ if (isUseReact18) {
214
+ const root = ReactDOM.createRoot(app)
215
+
216
+ ReactDOM.flushSync(() => {
217
+ root.render?.(h(Entry))
218
+ })
219
+ } else {
220
+ // eslint-disable-next-line react/no-deprecated
221
+ ReactDOM.render(h(Entry, {}), app)
222
+ }
223
+ }
224
+
225
+ export { getPageById, removePageById, setPageById }
226
+
227
+ export function createNativePageConfig (
228
+ Component,
229
+ pageName: string,
230
+ react: typeof React,
231
+ reactDOM: typeof ReactDOM,
232
+ pageConfig: any = {}
233
+ ) {
234
+ reactMeta.R = react
235
+ h = react.createElement
236
+ ReactDOM = reactDOM
237
+ setReconciler(ReactDOM)
238
+ const [ONLOAD, ONUNLOAD, ONREADY, ONSHOW, ONHIDE, LIFECYCLES, SIDE_EFFECT_LIFECYCLES] =
239
+ hooks.call('getMiniLifecycleImpl')!.page
240
+ let unmounting = false
241
+ let prepareMountList: (() => void)[] = []
242
+ let pageElement: TaroElement | null = null
243
+ let loadResolver: (...args: unknown[]) => void
244
+ let hasLoaded: Promise<void>
245
+ const id = pageName ?? `taro_page_${getNativeCompId()}`
246
+ const { isUseReact18 = true } = pageConfig
247
+
248
+ function setCurrentRouter (page) {
249
+ const router = page.route || page.__route__ || page.$taroPath
250
+ Current.router = {
251
+ params: page.$taroParams!,
252
+ path: addLeadingSlash(router),
253
+ $taroPath: page.$taroPath,
254
+ onReady: getOnReadyEventKey(page.$taroPath),
255
+ onShow: getOnShowEventKey(page.$taroPath),
256
+ onHide: getOnHideEventKey(page.$taroPath),
257
+ getEventName (eventName: string) {
258
+ return `${page.$taroPath}.${eventName}`
259
+ }
260
+ }
261
+ if (!isUndefined(page.exitState)) {
262
+ Current.router.exitState = page.exitState
263
+ }
264
+ }
265
+
266
+ const pageObj: Record<string, any> = {
267
+ options: pageConfig,
268
+ [ONLOAD] (options: Readonly<Record<string, unknown>> = {}, cb?: TFunc) {
269
+ hasLoaded = new Promise((resolve) => {
270
+ loadResolver = resolve
271
+ })
272
+ Current.page = this as any
273
+ this.config = pageConfig || {}
274
+ // this.$taroPath 是页面唯一标识
275
+ const uniqueOptions = Object.assign({}, options, { $taroTimestamp: Date.now() })
276
+ const $taroPath = (this.$taroPath = getPath(id, uniqueOptions))
277
+
278
+ this.$isUseReact18 = isUseReact18
279
+
280
+ setPageById(this, $taroPath)
281
+
282
+ // this.$taroParams 作为暴露给开发者的页面参数对象,可以被随意修改
283
+ if (this.$taroParams == null) {
284
+ this.$taroParams = uniqueOptions
285
+ }
286
+
287
+ setCurrentRouter(this)
288
+ window.trigger(CONTEXT_ACTIONS.INIT, $taroPath)
289
+
290
+ const mountCallback = () => {
291
+ pageElement = document.getElementById($taroPath)
292
+
293
+ if (!pageElement) {
294
+ console.error(`[TARO_LOG] Page Error: ${$taroPath}, 该页面执行时出现了报错,导致没有找到页面实例。`)
295
+ return
296
+ }
297
+
298
+ safeExecute($taroPath, ONLOAD, this.$taroParams)
299
+ loadResolver()
300
+ cb && cb(pageElement)
301
+ pageElement.ctx = this
302
+ }
303
+
304
+ let app = isUseReact18 ? Current.entryAsync : Current.app
305
+ const mount = () => {
306
+ if (!app) {
307
+ initNativeComponentEntry({
308
+ R: react,
309
+ ReactDOM,
310
+ isUseReact18,
311
+ cb: () => {
312
+ app = isUseReact18 ? Current.entryAsync : Current.app
313
+ app!.mount!(Component, $taroPath, () => this, mountCallback)
314
+ },
315
+ })
316
+ } else {
317
+ app!.mount!(Component, $taroPath, () => this, mountCallback)
318
+ }
319
+ }
320
+
321
+ if (unmounting) {
322
+ prepareMountList.push(mount)
323
+ } else {
324
+ mount()
325
+ }
326
+ },
327
+ [ONUNLOAD] () {
328
+ const $taroPath = this.$taroPath
329
+ // 销毁当前页面的上下文信息
330
+ window.trigger(CONTEXT_ACTIONS.DESTORY, $taroPath)
331
+ // 触发onUnload生命周期
332
+ safeExecute($taroPath, ONUNLOAD)
333
+
334
+ removePageById($taroPath)
335
+
336
+ resetCurrent.call(this)
337
+ unmounting = true
338
+
339
+ const app = isUseReact18 ? Current.entryAsync : Current.app
340
+ app!.unmount!($taroPath, () => {
341
+ unmounting = false
342
+ removePageInstance($taroPath)
343
+ if (pageElement) {
344
+ pageElement.ctx = null
345
+ pageElement = null
346
+ }
347
+ if (prepareMountList.length) {
348
+ prepareMountList.forEach((fn) => fn())
349
+ prepareMountList = []
350
+ }
351
+ })
352
+ },
353
+ [ONREADY] () {
354
+ hasLoaded.then(() => {
355
+ // 触发生命周期
356
+ safeExecute(this.$taroPath, ON_READY)
357
+ // 通过事件触发子组件的生命周期
358
+ requestAnimationFrame(() => eventCenter.trigger(getOnReadyEventKey(this.$taroPath)))
359
+ this.onReady = {}
360
+ this.onReady.called = true
361
+ })
362
+ },
363
+ [ONSHOW] (options = {}) {
364
+ hasLoaded.then(() => {
365
+ // 设置 Current 的 page 和 router
366
+ Current.page = this as any
367
+ setCurrentRouter(this)
368
+
369
+ if (this.__layoutSize && this.getNavHeight) {
370
+ Current.nativeModule.updateDimensionContext(initStyleSheetConfig(this.__layoutSize, this.getNavHeight()))
371
+ }
372
+ // 恢复上下文信息
373
+ window.trigger(CONTEXT_ACTIONS.RECOVER, this.$taroPath)
374
+ // 触发生命周期
375
+ safeExecute(this.$taroPath, ON_SHOW, options)
376
+ // 通过事件触发子组件的生命周期
377
+ requestAnimationFrame(() => eventCenter.trigger(getOnShowEventKey(this.$taroPath)))
378
+ })
379
+ },
380
+ [ONHIDE] () {
381
+ // 缓存当前页面上下文信息
382
+ window.trigger(CONTEXT_ACTIONS.RESTORE, this.$taroPath)
383
+ // 设置 Current 的 page 和 router
384
+ if (Current.page === this) {
385
+ Current.page = null
386
+ Current.router = null
387
+ }
388
+ // 触发生命周期
389
+ safeExecute(this.$taroPath, ON_HIDE)
390
+ // 通过事件触发子组件的生命周期
391
+ eventCenter.trigger(getOnHideEventKey(this.$taroPath))
392
+ },
393
+ }
394
+
395
+ function resetCurrent () {
396
+ if (Current.page === this) {
397
+ // 小程序插件页面卸载之后返回到宿主页面时,需重置Current页面和路由。否则引发插件组件二次加载异常 fix:#11991
398
+ Current.page = null
399
+ Current.router = null
400
+ }
401
+ }
402
+
403
+ LIFECYCLES.forEach((lifecycle) => {
404
+ let isDefer = false
405
+ lifecycle = lifecycle.replace(/^defer:/, () => {
406
+ isDefer = true
407
+ return ''
408
+ })
409
+ pageObj[lifecycle] = function () {
410
+ const exec = () => {
411
+ eventCenter.trigger(`${this.$taroPath}.${lifecycle}`, ...arguments)
412
+ return safeExecute(this.$taroPath, lifecycle, ...arguments)
413
+ }
414
+ if (isDefer) {
415
+ hasLoaded.then(exec)
416
+ } else {
417
+ return exec()
418
+ }
419
+ }
420
+ })
421
+
422
+ // onShareAppMessage 和 onShareTimeline 一样,会影响小程序右上方按钮的选项,因此不能默认注册。
423
+ SIDE_EFFECT_LIFECYCLES.forEach((lifecycle) => {
424
+ if (Component[lifecycle] || Component.prototype?.[lifecycle] || Component[lifecycle.replace(/^on/, 'enable')]) {
425
+ pageObj[lifecycle] = function (...args) {
426
+ const target = args[0]?.target
427
+ if (target?.id) {
428
+ const id = target.id
429
+ const element = env.document.getElementById(id)
430
+ if (element) {
431
+ target.dataset = element.dataset
432
+ }
433
+ }
434
+ eventCenter.trigger(`${this.$taroPath}.${lifecycle}`, ...arguments)
435
+ return safeExecute(this.$taroPath, lifecycle, ...args)
436
+ }
437
+ }
438
+ })
439
+
440
+ hooks.call('modifyPageObject', pageObj)
441
+
442
+ return pageObj
443
+ }
444
+
445
+ export function createNativeComponentConfig (
446
+ Component,
447
+ compName: string,
448
+ react: typeof React,
449
+ reactdom,
450
+ componentConfig: any = {}
451
+ ) {
452
+ reactMeta.R = react
453
+ h = react.createElement
454
+ ReactDOM = reactdom
455
+ setReconciler(ReactDOM)
456
+ const { isUseReact18 = true } = componentConfig
457
+
458
+ const componentObj: Record<string, any> = {
459
+ options: componentConfig,
460
+ onLoad (
461
+ options: Readonly<Record<string, unknown>> = {}, // eslint-disable-line @typescript-eslint/no-unused-vars
462
+ cb?: TFunc
463
+ ) {
464
+ let app = isUseReact18 ? Current.entryAsync : Current.app
465
+
466
+ const mountComponent = () => {
467
+ compName ??= `taro_comp_${getNativeCompId()}`
468
+ const compId = (this.compId = getPath(compName, { $taroTimestamp: Date.now() }))
469
+
470
+ this.config = componentConfig
471
+ app = isUseReact18 ? Current.entryAsync : Current.app
472
+
473
+ app!.mount!(
474
+ Component,
475
+ compId,
476
+ () => this,
477
+ () => {
478
+ const el = document.getElementById(compId)
479
+
480
+ if (!el) {
481
+ throw new Error(`没有找到组件实例。`)
482
+ } else {
483
+ el.ctx = this
484
+ cb && cb(el)
485
+ safeExecute(compId, 'onLoad', options)
486
+ }
487
+ }
488
+ )
489
+ }
490
+
491
+ if (!app) {
492
+ initNativeComponentEntry({
493
+ R: react,
494
+ ReactDOM,
495
+ isUseReact18,
496
+ cb: mountComponent,
497
+ })
498
+ } else {
499
+ mountComponent()
500
+ }
501
+ },
502
+
503
+ onUnload () {
504
+ const app = isUseReact18 ? Current.entryAsync : Current.app
505
+ app!.unmount!(this.compId)
506
+ safeExecute(this.compId, 'onUnload')
507
+ },
508
+ }
509
+
510
+ return componentObj
511
+ }