@tarojs/plugin-platform-harmony-cpp 4.1.0 → 4.1.1-alpha.0
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/dist/index.js +1237 -0
- package/dist/runtime/apis/apis.ts +20 -0
- package/dist/runtime/apis/base/crypto.ts +4 -0
- package/dist/runtime/apis/base/debug.ts +5 -0
- package/dist/runtime/apis/base/index.ts +13 -0
- package/dist/runtime/apis/base/performance.ts +8 -0
- package/dist/runtime/apis/base/system.ts +181 -0
- package/dist/runtime/apis/base/update.ts +5 -0
- package/dist/runtime/apis/base/weapp/app-event.ts +75 -0
- package/dist/runtime/apis/base/weapp/life-cycle.ts +21 -0
- package/dist/runtime/apis/canvas/index.ts +27 -0
- package/dist/runtime/apis/data-analysis/index.ts +6 -0
- package/dist/runtime/apis/device/accelerometer.ts +79 -0
- package/dist/runtime/apis/device/accessibility.ts +4 -0
- package/dist/runtime/apis/device/battery.ts +24 -0
- package/dist/runtime/apis/device/bluetooth-ble.ts +19 -0
- package/dist/runtime/apis/device/bluetooth-peripheral.ts +6 -0
- package/dist/runtime/apis/device/bluetooth.ts +16 -0
- package/dist/runtime/apis/device/calendar.ts +5 -0
- package/dist/runtime/apis/device/clipboard.ts +85 -0
- package/dist/runtime/apis/device/compass.ts +21 -0
- package/dist/runtime/apis/device/contact.ts +5 -0
- package/dist/runtime/apis/device/crypto.ts +4 -0
- package/dist/runtime/apis/device/gyroscope.ts +77 -0
- package/dist/runtime/apis/device/iBeacon.ts +10 -0
- package/dist/runtime/apis/device/index.ts +24 -0
- package/dist/runtime/apis/device/keyboard.ts +63 -0
- package/dist/runtime/apis/device/memory.ts +11 -0
- package/dist/runtime/apis/device/motion.ts +6 -0
- package/dist/runtime/apis/device/network.ts +123 -0
- package/dist/runtime/apis/device/nfc.ts +10 -0
- package/dist/runtime/apis/device/phone.ts +44 -0
- package/dist/runtime/apis/device/scan.ts +4 -0
- package/dist/runtime/apis/device/screen.ts +80 -0
- package/dist/runtime/apis/device/sms.ts +4 -0
- package/dist/runtime/apis/device/vibrate.ts +32 -0
- package/dist/runtime/apis/device/wifi.ts +15 -0
- package/dist/runtime/apis/ext/index.ts +5 -0
- package/dist/runtime/apis/files/index.ts +136 -0
- package/dist/runtime/apis/files/manager.ts +942 -0
- package/dist/runtime/apis/framework/index.ts +48 -0
- package/dist/runtime/apis/harmony/task-pool.ts +39 -0
- package/dist/runtime/apis/index.ts +61 -0
- package/dist/runtime/apis/location/index.ts +133 -0
- package/dist/runtime/apis/media/EditorContext.ts +32 -0
- package/dist/runtime/apis/media/audio/index.ts +36 -0
- package/dist/runtime/apis/media/background-audio/index.ts +16 -0
- package/dist/runtime/apis/media/camera.ts +16 -0
- package/dist/runtime/apis/media/common.ts +58 -0
- package/dist/runtime/apis/media/image/index.ts +291 -0
- package/dist/runtime/apis/media/index.ts +13 -0
- package/dist/runtime/apis/media/live.ts +5 -0
- package/dist/runtime/apis/media/map.ts +4 -0
- package/dist/runtime/apis/media/media-recorder.ts +4 -0
- package/dist/runtime/apis/media/recorder.ts +6 -0
- package/dist/runtime/apis/media/video/VideoContext.ts +67 -0
- package/dist/runtime/apis/media/video/index.ts +45 -0
- package/dist/runtime/apis/media/video-decoder.ts +4 -0
- package/dist/runtime/apis/media/video-processing.ts +4 -0
- package/dist/runtime/apis/media/voip.ts +19 -0
- package/dist/runtime/apis/navigate/index.ts +8 -0
- package/dist/runtime/apis/network/downloadFile.ts +85 -0
- package/dist/runtime/apis/network/index.ts +7 -0
- package/dist/runtime/apis/network/mdns.ts +13 -0
- package/dist/runtime/apis/network/request.ts +140 -0
- package/dist/runtime/apis/network/tcp.ts +4 -0
- package/dist/runtime/apis/network/udp.ts +4 -0
- package/dist/runtime/apis/network/uploadFile.ts +105 -0
- package/dist/runtime/apis/network/webSocket.ts +126 -0
- package/dist/runtime/apis/open-api/account.ts +4 -0
- package/dist/runtime/apis/open-api/address.ts +4 -0
- package/dist/runtime/apis/open-api/authorize.ts +5 -0
- package/dist/runtime/apis/open-api/card.ts +5 -0
- package/dist/runtime/apis/open-api/channels-live.ts +11 -0
- package/dist/runtime/apis/open-api/customer-service.ts +4 -0
- package/dist/runtime/apis/open-api/device-voip.ts +5 -0
- package/dist/runtime/apis/open-api/facial.ts +7 -0
- package/dist/runtime/apis/open-api/favorites.ts +5 -0
- package/dist/runtime/apis/open-api/group.ts +4 -0
- package/dist/runtime/apis/open-api/index.ts +21 -0
- package/dist/runtime/apis/open-api/invoice.ts +5 -0
- package/dist/runtime/apis/open-api/license-plate.ts +4 -0
- package/dist/runtime/apis/open-api/login.ts +6 -0
- package/dist/runtime/apis/open-api/my-miniprogram.ts +4 -0
- package/dist/runtime/apis/open-api/privacy.ts +7 -0
- package/dist/runtime/apis/open-api/red-package.ts +4 -0
- package/dist/runtime/apis/open-api/settings.ts +5 -0
- package/dist/runtime/apis/open-api/soter.ts +6 -0
- package/dist/runtime/apis/open-api/subscribe-message.ts +6 -0
- package/dist/runtime/apis/open-api/user-info.ts +70 -0
- package/dist/runtime/apis/open-api/werun.ts +5 -0
- package/dist/runtime/apis/payment/index.ts +6 -0
- package/dist/runtime/apis/route/index.ts +88 -0
- package/dist/runtime/apis/share/index.ts +33 -0
- package/dist/runtime/apis/storage/background-fetch.ts +7 -0
- package/dist/runtime/apis/storage/cache-manager.ts +4 -0
- package/dist/runtime/apis/storage/index.ts +203 -0
- package/dist/runtime/apis/ui/animation/animation.ts +264 -0
- package/dist/runtime/apis/ui/animation/index.ts +7 -0
- package/dist/runtime/apis/ui/background.ts +20 -0
- package/dist/runtime/apis/ui/custom-component.ts +1 -0
- package/dist/runtime/apis/ui/fonts.ts +4 -0
- package/dist/runtime/apis/ui/index.ts +12 -0
- package/dist/runtime/apis/ui/interaction/index.ts +198 -0
- package/dist/runtime/apis/ui/menu.ts +4 -0
- package/dist/runtime/apis/ui/navigation-bar/index.ts +72 -0
- package/dist/runtime/apis/ui/pull-down-refresh.ts +47 -0
- package/dist/runtime/apis/ui/scroll/index.ts +82 -0
- package/dist/runtime/apis/ui/sticky.ts +4 -0
- package/dist/runtime/apis/ui/tab-bar.ts +144 -0
- package/dist/runtime/apis/ui/window.ts +20 -0
- package/dist/runtime/apis/utils/constant.ts +1 -0
- package/dist/runtime/apis/utils/handler.ts +117 -0
- package/dist/runtime/apis/utils/index.ts +105 -0
- package/dist/runtime/apis/utils/permissions.ts +6 -0
- package/dist/runtime/apis/utils/types.ts +12 -0
- package/dist/runtime/apis/utils/unit.ts +104 -0
- package/dist/runtime/apis/utils/validate.ts +87 -0
- package/dist/runtime/apis/worker/index.ts +4 -0
- package/dist/runtime/apis/wxml/index.ts +17 -0
- package/dist/runtime/apis/wxml/intersectionObserver.ts +100 -0
- package/dist/runtime/apis/wxml/nodesRef.ts +58 -0
- package/dist/runtime/apis/wxml/selectorQuery.ts +221 -0
- package/dist/runtime/apischunk/index.d.ts +802 -0
- package/dist/runtime/apischunk/index.js +5054 -0
- package/dist/runtime/components/deprecated.ets +43 -0
- package/dist/runtime/components/index.ets +49 -0
- package/dist/runtime/components/innerHtml.ets +16 -0
- package/dist/runtime/components/navigationBar.ets +65 -0
- package/dist/runtime/components/pageMeta.ets +94 -0
- package/dist/runtime/components/richText.ets +24 -0
- package/dist/runtime/components/slider.ets +119 -0
- package/dist/runtime/components/style.ets +286 -0
- package/dist/runtime/components/switch.ets +73 -0
- package/dist/runtime/components/tag.ts +58 -0
- package/dist/runtime/components/utils/AttributeManager.ets +252 -0
- package/dist/runtime/components/utils/DynamicCenter.ts +41 -0
- package/dist/runtime/components/utils/constant/event.ets +25 -0
- package/dist/runtime/components/utils/constant/style.ets +91 -0
- package/dist/runtime/components/utils/events.ts +26 -0
- package/dist/runtime/components/utils/flexManager.ets +49 -0
- package/dist/runtime/components/utils/helper.ets +51 -0
- package/dist/runtime/components/utils/htmlParser/HarmonyHTMLParser.ts +100 -0
- package/dist/runtime/components/utils/htmlParser/index.ts +58 -0
- package/dist/runtime/components/utils/index.ts +77 -0
- package/dist/runtime/components/utils/styles.ets +41 -0
- package/dist/runtime/components/video.ets +119 -0
- package/dist/runtime/components/webView.ets +55 -0
- package/dist/runtime/components/xComponent.ets +89 -0
- package/dist/runtime/framework/app.ts +248 -0
- package/dist/runtime/framework/connect.ts +24 -0
- package/dist/runtime/framework/constant.ts +3 -0
- package/dist/runtime/framework/hooks.ts +99 -0
- package/dist/runtime/framework/index.ts +15 -0
- package/dist/runtime/framework/native-page.ts +511 -0
- package/dist/runtime/framework/page.ts +256 -0
- package/dist/runtime/framework/utils/index.ts +17 -0
- package/dist/runtime/framework/utils/is.ts +26 -0
- package/dist/runtime/framework-reconciler/componentTree.ts +81 -0
- package/dist/runtime/framework-reconciler/constant.ts +86 -0
- package/dist/runtime/framework-reconciler/domInput.ts +90 -0
- package/dist/runtime/framework-reconciler/event.ts +108 -0
- package/dist/runtime/framework-reconciler/index.ts +99 -0
- package/dist/runtime/framework-reconciler/inputValueTracking.ts +106 -0
- package/dist/runtime/framework-reconciler/props.ts +132 -0
- package/dist/runtime/framework-reconciler/reconciler.ts +202 -0
- package/dist/runtime/framework-reconciler/render.ts +139 -0
- package/dist/runtime/framework-reconciler/workTags.ts +53 -0
- package/dist/runtime/runtime-cpp/bom/URL.ts +2 -0
- package/dist/runtime/runtime-cpp/bom/document.ts +45 -0
- package/dist/runtime/runtime-cpp/bom/history.ts +1 -0
- package/dist/runtime/runtime-cpp/bom/location.ts +1 -0
- package/dist/runtime/runtime-cpp/bom/navigator.ts +1 -0
- package/dist/runtime/runtime-cpp/bom/raf.ts +1 -0
- package/dist/runtime/runtime-cpp/bom/window.ts +57 -0
- package/dist/runtime/runtime-cpp/constant.ts +29 -0
- package/dist/runtime/runtime-cpp/current.ts +48 -0
- package/dist/runtime/runtime-cpp/dom/class-list.ts +41 -0
- package/dist/runtime/runtime-cpp/dom/comment.ts +9 -0
- package/dist/runtime/runtime-cpp/dom/dataSource.ts +87 -0
- package/dist/runtime/runtime-cpp/dom/document.ts +113 -0
- package/dist/runtime/runtime-cpp/dom/element/canvas.ts +285 -0
- package/dist/runtime/runtime-cpp/dom/element/element.ts +262 -0
- package/dist/runtime/runtime-cpp/dom/element/form.ts +371 -0
- package/dist/runtime/runtime-cpp/dom/element/index.ts +129 -0
- package/dist/runtime/runtime-cpp/dom/element/movable.ts +250 -0
- package/dist/runtime/runtime-cpp/dom/element/normal.ts +123 -0
- package/dist/runtime/runtime-cpp/dom/element/scroll_view.ts +32 -0
- package/dist/runtime/runtime-cpp/dom/element/text.ts +9 -0
- package/dist/runtime/runtime-cpp/dom/element/video.ts +66 -0
- package/dist/runtime/runtime-cpp/dom/element/web_view.ts +72 -0
- package/dist/runtime/runtime-cpp/dom/event-source.ts +1 -0
- package/dist/runtime/runtime-cpp/dom/event.ts +187 -0
- package/dist/runtime/runtime-cpp/dom/eventTarget.ts +81 -0
- package/dist/runtime/runtime-cpp/dom/node.ts +307 -0
- package/dist/runtime/runtime-cpp/dom/style.ts +44 -0
- package/dist/runtime/runtime-cpp/dom/stylesheet/index.ts +379 -0
- package/dist/runtime/runtime-cpp/dom/stylesheet/type.ts +212 -0
- package/dist/runtime/runtime-cpp/dom/stylesheet/util.ts +186 -0
- package/dist/runtime/runtime-cpp/emitter/emitter.ts +29 -0
- package/dist/runtime/runtime-cpp/env.ts +1 -0
- package/dist/runtime/runtime-cpp/harmony-library.ts +1 -0
- package/dist/runtime/runtime-cpp/index.ts +79 -0
- package/dist/runtime/runtime-cpp/interface/event.ts +10 -0
- package/dist/runtime/runtime-cpp/interface/index.ts +7 -0
- package/dist/runtime/runtime-cpp/next-tick.ts +14 -0
- package/dist/runtime/runtime-cpp/system.ts +213 -0
- package/dist/runtime/runtime-cpp/utils/index.ts +63 -0
- package/dist/runtime/runtime-cpp/utils/info.ts +123 -0
- package/dist/runtime/runtime-cpp/utils/page.ts +12 -0
- package/dist/runtime/runtime-cpp/utils/router.ts +1 -0
- package/dist/runtime/runtime-harmony/apis/helper.ets +33 -0
- package/dist/runtime/runtime-harmony/apis/network/common.ets +41 -0
- package/dist/runtime/runtime-harmony/apis/network/downloadFile.ets +327 -0
- package/dist/runtime/runtime-harmony/apis/network/index.ets +58 -0
- package/dist/runtime/runtime-harmony/apis/network/request.ets +206 -0
- package/dist/runtime/runtime-harmony/apis/network/uploadFile.ets +190 -0
- package/dist/runtime/runtime-harmony/apis/route.ets +110 -0
- package/dist/runtime/runtime-harmony/apis/taskpool.ets +149 -0
- package/dist/runtime/runtime-harmony/index.ets +41 -0
- package/dist/runtime/runtime-harmony/utils.ts +53 -0
- package/package.json +24 -23
- package/LICENSE +0 -174
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { Current, document, eventCenter } from '@tarojs/runtime'
|
|
2
|
+
import { EMPTY_OBJ } from '@tarojs/shared'
|
|
3
|
+
|
|
4
|
+
import { setReconciler } from './connect'
|
|
5
|
+
import { injectPageInstance } from './page'
|
|
6
|
+
import { incrementId, isClassComponent } from './utils'
|
|
7
|
+
|
|
8
|
+
import type { AppInstance } from '@tarojs/runtime'
|
|
9
|
+
import type React from 'react'
|
|
10
|
+
|
|
11
|
+
let h: typeof React.createElement
|
|
12
|
+
let ReactDOM
|
|
13
|
+
|
|
14
|
+
interface IReactMeta {
|
|
15
|
+
PageContext: React.Context<string>
|
|
16
|
+
R: typeof React
|
|
17
|
+
Container: any
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const ReactMeta: IReactMeta = {
|
|
21
|
+
R: EMPTY_OBJ,
|
|
22
|
+
Container: EMPTY_OBJ,
|
|
23
|
+
PageContext: EMPTY_OBJ
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const pageKeyId = incrementId(1)
|
|
27
|
+
|
|
28
|
+
export function connectReactPage (
|
|
29
|
+
R: typeof React,
|
|
30
|
+
id: string,
|
|
31
|
+
getCtx: () => any
|
|
32
|
+
) {
|
|
33
|
+
const ctx = getCtx?.()
|
|
34
|
+
|
|
35
|
+
return (Page): React.ComponentClass<any> => {
|
|
36
|
+
// eslint-disable-next-line dot-notation
|
|
37
|
+
const isReactComponent = isClassComponent(R, Page)
|
|
38
|
+
const inject = (node?: any) => node && injectPageInstance(node, id)
|
|
39
|
+
const refs = isReactComponent ? { ref: inject } : {
|
|
40
|
+
forwardedRef: inject,
|
|
41
|
+
// 兼容 react-redux 7.20.1+
|
|
42
|
+
reactReduxForwardedRef: inject
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (ReactMeta.PageContext === EMPTY_OBJ) {
|
|
46
|
+
ReactMeta.PageContext = R.createContext('')
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return class PageWrapper extends R.Component<any, { hasError: boolean }> {
|
|
50
|
+
state = {
|
|
51
|
+
hasError: false
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
static getDerivedStateFromError (error: Error) {
|
|
55
|
+
Current.app?.onError?.(error.message + error.stack)
|
|
56
|
+
return { hasError: true }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// React 16 uncaught error 会导致整个应用 crash,
|
|
60
|
+
// 目前把错误缩小到页面
|
|
61
|
+
componentDidCatch (error, info: React.ErrorInfo) {
|
|
62
|
+
console.warn(error)
|
|
63
|
+
console.error(info.componentStack)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
render () {
|
|
67
|
+
const children = this.state.hasError
|
|
68
|
+
? []
|
|
69
|
+
: h(ReactMeta.PageContext.Provider, { value: id }, h(Page, {
|
|
70
|
+
...Object.assign({}, ctx?.props, this.props),
|
|
71
|
+
...refs
|
|
72
|
+
}))
|
|
73
|
+
|
|
74
|
+
return h(
|
|
75
|
+
'taro-page',
|
|
76
|
+
{ id, className: 'taro_page' },
|
|
77
|
+
children
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function createReactApp (
|
|
85
|
+
App,
|
|
86
|
+
react,
|
|
87
|
+
dom,
|
|
88
|
+
config?: any
|
|
89
|
+
) {
|
|
90
|
+
ReactMeta.R = react
|
|
91
|
+
h = react.createElement
|
|
92
|
+
ReactDOM = dom
|
|
93
|
+
const appInstanceRef = react.createRef()
|
|
94
|
+
const isReactComponent = isClassComponent(react, App)
|
|
95
|
+
let appWrapper: AppWrapper
|
|
96
|
+
let appWrapperResolver: (value: AppWrapper) => void
|
|
97
|
+
const appWrapperPromise = new Promise<AppWrapper>(resolve => (appWrapperResolver = resolve))
|
|
98
|
+
|
|
99
|
+
setReconciler(ReactDOM)
|
|
100
|
+
|
|
101
|
+
function getAppInstance (): any {
|
|
102
|
+
return appInstanceRef.current
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function waitAppWrapper (cb: () => void) {
|
|
106
|
+
appWrapper ? cb() : appWrapperPromise.then(() => cb())
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function renderReactRoot () {
|
|
110
|
+
const appId = config?.appId || 'app'
|
|
111
|
+
|
|
112
|
+
if (ReactMeta.Container === EMPTY_OBJ) {
|
|
113
|
+
const Container = document.getElementById(appId)!
|
|
114
|
+
|
|
115
|
+
Container.id = appId
|
|
116
|
+
ReactMeta.Container = Container
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const root = ReactDOM.createRoot(ReactMeta.Container)
|
|
120
|
+
root.render?.(h(AppWrapper as any))
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
class AppWrapper extends react.Component {
|
|
124
|
+
private pages: Array<any> = []
|
|
125
|
+
private elements: Array<any> = []
|
|
126
|
+
|
|
127
|
+
constructor (props) {
|
|
128
|
+
super(props)
|
|
129
|
+
appWrapper = this
|
|
130
|
+
appWrapperResolver(this)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
public mount (pageComponent: any, id: string, getCtx: () => any, cb: () => void) {
|
|
134
|
+
const pageWrapper = connectReactPage(react, id, getCtx)(pageComponent)
|
|
135
|
+
const key = `${id}_${pageKeyId()}`
|
|
136
|
+
const page = () => h(pageWrapper, { key, tid: id })
|
|
137
|
+
this.pages.push(page)
|
|
138
|
+
this.forceUpdate(cb)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
public unmount (id: string, cb: () => void) {
|
|
142
|
+
const elements = this.elements
|
|
143
|
+
const idx = elements.findIndex(item => item.props.tid === id)
|
|
144
|
+
elements.splice(idx, 1)
|
|
145
|
+
this.forceUpdate(cb)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
public render () {
|
|
149
|
+
const { pages, elements } = this
|
|
150
|
+
|
|
151
|
+
while (pages.length > 0) {
|
|
152
|
+
const page = pages.pop()!
|
|
153
|
+
elements.push(page())
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
let props: React.ComponentProps<any> | null = null
|
|
157
|
+
|
|
158
|
+
if (isReactComponent) {
|
|
159
|
+
props = { ref: appInstanceRef }
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return h(
|
|
163
|
+
App,
|
|
164
|
+
props,
|
|
165
|
+
elements.slice()
|
|
166
|
+
)
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
renderReactRoot()
|
|
171
|
+
|
|
172
|
+
const app = {
|
|
173
|
+
render (cb: () => void) {
|
|
174
|
+
appWrapper.forceUpdate(cb)
|
|
175
|
+
},
|
|
176
|
+
mount (component: any, id: string, getCtx: () => any, cb: () => void) {
|
|
177
|
+
if (appWrapper) {
|
|
178
|
+
appWrapper.mount(component, id, getCtx, cb)
|
|
179
|
+
} else {
|
|
180
|
+
appWrapperPromise.then(appWrapper => appWrapper.mount(component, id, getCtx, cb))
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
unmount (id: string, cb: () => void) {
|
|
184
|
+
appWrapper?.unmount(id, cb)
|
|
185
|
+
},
|
|
186
|
+
onLaunch (launchParam?: any) {
|
|
187
|
+
waitAppWrapper(() => {
|
|
188
|
+
// 用户编写的入口组件实例
|
|
189
|
+
const app = getAppInstance()
|
|
190
|
+
this.$app = app
|
|
191
|
+
|
|
192
|
+
if (app) {
|
|
193
|
+
// 把 App Class 上挂载的额外属性同步到全局 app 对象中
|
|
194
|
+
if (app.taroGlobalData) {
|
|
195
|
+
const globalData = app.taroGlobalData
|
|
196
|
+
const keys = Object.keys(globalData)
|
|
197
|
+
const descriptors = Object.getOwnPropertyDescriptors(globalData)
|
|
198
|
+
keys.forEach(key => {
|
|
199
|
+
Object.defineProperty(Current?.app || this, key, {
|
|
200
|
+
configurable: true,
|
|
201
|
+
enumerable: true,
|
|
202
|
+
get () {
|
|
203
|
+
return globalData[key]
|
|
204
|
+
},
|
|
205
|
+
set (value) {
|
|
206
|
+
globalData[key] = value
|
|
207
|
+
}
|
|
208
|
+
})
|
|
209
|
+
})
|
|
210
|
+
Object.defineProperties(Current?.app || this, descriptors)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
app.onCreate?.()
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
eventCenter.trigger('__taroRouterLaunch', launchParam)
|
|
217
|
+
})
|
|
218
|
+
},
|
|
219
|
+
onShow () {
|
|
220
|
+
waitAppWrapper(() => {
|
|
221
|
+
/**
|
|
222
|
+
* trigger lifecycle
|
|
223
|
+
*/
|
|
224
|
+
const app = getAppInstance()
|
|
225
|
+
// class component, componentDidShow
|
|
226
|
+
app?.componentDidShow?.()
|
|
227
|
+
})
|
|
228
|
+
},
|
|
229
|
+
onHide () {
|
|
230
|
+
waitAppWrapper(() => {
|
|
231
|
+
/**
|
|
232
|
+
* trigger lifecycle
|
|
233
|
+
*/
|
|
234
|
+
const app = getAppInstance()
|
|
235
|
+
// class component, componentDidShow
|
|
236
|
+
app?.componentDidHide?.()
|
|
237
|
+
})
|
|
238
|
+
}
|
|
239
|
+
} as unknown as AppInstance
|
|
240
|
+
|
|
241
|
+
if (Current.app) {
|
|
242
|
+
Current.app = Object.assign(app, Current.app)
|
|
243
|
+
} else {
|
|
244
|
+
Current.app = app
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return app
|
|
248
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { hooks } from '@tarojs/shared'
|
|
2
|
+
|
|
3
|
+
import { ensureIsArray } from './utils'
|
|
4
|
+
|
|
5
|
+
export function setReconciler (ReactDOM) {
|
|
6
|
+
hooks.tap('batchedEventUpdates', function (cb) {
|
|
7
|
+
ReactDOM.unstable_batchedUpdates(cb)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
hooks.tap('mergePageInstance', function (prev, next) {
|
|
11
|
+
if (!prev || !next) return
|
|
12
|
+
|
|
13
|
+
// 子组件使用 lifecycle hooks 注册了生命周期后,会存在 prev,里面是注册的生命周期回调。
|
|
14
|
+
|
|
15
|
+
// prev 使用 Object.create(null) 创建,H5 的 fast-refresh 可能也会导致存在 prev,要排除这些意外产生的 prev
|
|
16
|
+
if ('constructor' in prev) return
|
|
17
|
+
|
|
18
|
+
Object.keys(prev).forEach(item => {
|
|
19
|
+
const prevList = prev[item]
|
|
20
|
+
const nextList = ensureIsArray<() => any>(next[item])
|
|
21
|
+
next[item] = nextList.concat(prevList)
|
|
22
|
+
})
|
|
23
|
+
})
|
|
24
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { Current, HOOKS_APP_ID } from '@tarojs/runtime'
|
|
2
|
+
import { isArray, isFunction } from '@tarojs/shared'
|
|
3
|
+
|
|
4
|
+
import { ReactMeta } from './app'
|
|
5
|
+
import { getPageInstance, injectPageInstance } from './page'
|
|
6
|
+
|
|
7
|
+
import type {
|
|
8
|
+
AppInstance,
|
|
9
|
+
Instance,
|
|
10
|
+
PageLifeCycle,
|
|
11
|
+
PageProps,
|
|
12
|
+
TFunc
|
|
13
|
+
} from '@tarojs/runtime'
|
|
14
|
+
|
|
15
|
+
const createTaroHook = (lifecycle: keyof PageLifeCycle | keyof AppInstance) => {
|
|
16
|
+
return (fn: TFunc) => {
|
|
17
|
+
const { R: React, PageContext } = ReactMeta
|
|
18
|
+
const id = React.useContext(PageContext) || HOOKS_APP_ID
|
|
19
|
+
const instRef = React.useRef<Instance<PageProps>>()
|
|
20
|
+
|
|
21
|
+
// hold fn ref and keep up to date
|
|
22
|
+
const fnRef = React.useRef(fn)
|
|
23
|
+
if (fnRef.current !== fn) fnRef.current = fn
|
|
24
|
+
|
|
25
|
+
React.useLayoutEffect(() => {
|
|
26
|
+
let inst = instRef.current = getPageInstance(id)
|
|
27
|
+
let first = false
|
|
28
|
+
if (!inst) {
|
|
29
|
+
first = true
|
|
30
|
+
instRef.current = Object.create(null)
|
|
31
|
+
inst = instRef.current!
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// callback is immutable but inner function is up to date
|
|
35
|
+
const callback = (...args: any) => fnRef.current(...args)
|
|
36
|
+
|
|
37
|
+
if (isFunction(inst[lifecycle])) {
|
|
38
|
+
(inst[lifecycle]) = [inst[lifecycle], callback]
|
|
39
|
+
} else {
|
|
40
|
+
(inst[lifecycle]) = [
|
|
41
|
+
...((inst[lifecycle]) || []),
|
|
42
|
+
callback
|
|
43
|
+
]
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (first) {
|
|
47
|
+
injectPageInstance(inst!, id)
|
|
48
|
+
}
|
|
49
|
+
return () => {
|
|
50
|
+
const inst = instRef.current
|
|
51
|
+
if (!inst) return
|
|
52
|
+
const list = inst![lifecycle]
|
|
53
|
+
if (list === callback) {
|
|
54
|
+
(inst[lifecycle]) = undefined
|
|
55
|
+
} else if (isArray(list)) {
|
|
56
|
+
(inst[lifecycle]) = list.filter(item => item !== callback)
|
|
57
|
+
}
|
|
58
|
+
instRef.current = undefined
|
|
59
|
+
}
|
|
60
|
+
}, [])
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** LifeCycle */
|
|
65
|
+
export const useDidHide = createTaroHook('componentDidHide')
|
|
66
|
+
export const useDidShow = createTaroHook('componentDidShow')
|
|
67
|
+
|
|
68
|
+
/** App */
|
|
69
|
+
export const useError = createTaroHook('onError')
|
|
70
|
+
export const useUnhandledRejection = createTaroHook('onUnhandledRejection')
|
|
71
|
+
export const useLaunch = createTaroHook('onLaunch')
|
|
72
|
+
export const usePageNotFound = createTaroHook('onPageNotFound')
|
|
73
|
+
|
|
74
|
+
/** Page */
|
|
75
|
+
export const useLoad = createTaroHook('onLoad')
|
|
76
|
+
export const usePageScroll = createTaroHook('onPageScroll')
|
|
77
|
+
export const usePullDownRefresh = createTaroHook('onPullDownRefresh')
|
|
78
|
+
export const usePullIntercept = createTaroHook('onPullIntercept')
|
|
79
|
+
export const useReachBottom = createTaroHook('onReachBottom')
|
|
80
|
+
export const useResize = createTaroHook('onResize')
|
|
81
|
+
export const useUnload = createTaroHook('onUnload')
|
|
82
|
+
|
|
83
|
+
/** Mini-Program */
|
|
84
|
+
export const useAddToFavorites = createTaroHook('onAddToFavorites')
|
|
85
|
+
export const useOptionMenuClick = createTaroHook('onOptionMenuClick')
|
|
86
|
+
export const useSaveExitState = createTaroHook('onSaveExitState')
|
|
87
|
+
export const useShareAppMessage = createTaroHook('onShareAppMessage')
|
|
88
|
+
export const useShareTimeline = createTaroHook('onShareTimeline')
|
|
89
|
+
export const useTitleClick = createTaroHook('onTitleClick')
|
|
90
|
+
|
|
91
|
+
/** Router */
|
|
92
|
+
export const useReady = createTaroHook('onReady')
|
|
93
|
+
export const useRouter = (dynamic = false) => {
|
|
94
|
+
const React = ReactMeta.R
|
|
95
|
+
return dynamic ? Current.router : React.useMemo(() => Current.router, [])
|
|
96
|
+
}
|
|
97
|
+
export const useTabItemTap = createTaroHook('onTabItemTap')
|
|
98
|
+
|
|
99
|
+
export const useScope = () => undefined
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { hooks } from '@tarojs/shared'
|
|
2
|
+
|
|
3
|
+
import * as taroHooks from './hooks'
|
|
4
|
+
|
|
5
|
+
hooks.tap('initNativeApi', function (taro) {
|
|
6
|
+
for (const hook in taroHooks) {
|
|
7
|
+
taro[hook] = taroHooks[hook]
|
|
8
|
+
}
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
export * from './app'
|
|
12
|
+
export * from './connect'
|
|
13
|
+
export * from './hooks'
|
|
14
|
+
export * from './native-page'
|
|
15
|
+
export * from './page'
|