Skip to content

Commit

Permalink
style: runtime-rn code format
Browse files Browse the repository at this point in the history
  • Loading branch information
yechunxi committed Jul 19, 2023
1 parent 4d4e838 commit 0a2116c
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 101 deletions.
118 changes: 59 additions & 59 deletions packages/taro-runtime-rn/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Provider as TCNProvider } from '@tarojs/components-rn'
import React, { Component, ComponentProps, createElement,createRef, forwardRef } from 'react'
import React, { Component, ComponentProps, createElement, createRef, forwardRef } from 'react'
import { RootSiblingParent } from 'react-native-root-siblings'

import { Current } from './current'
Expand All @@ -10,14 +10,12 @@ import { createRouter, getInitOptions, getRouteEventChannel } from './router'
import { RNAppConfig } from './types/index'
import { HOOKS_APP_ID, isFunction } from './utils'


export function isClassComponent (component): boolean {
return isFunction(component?.render) ||
!!component.prototype?.isReactComponent ||
component.prototype instanceof Component
return (
isFunction(component?.render) || !!component.prototype?.isReactComponent || component.prototype instanceof Component
)
}


export function createReactNativeApp (AppEntry: any, config: RNAppConfig, FirstPage: any) {
const singleMode = config?.appConfig?.rn?.singleMode ?? false
const needNavigate = config.pageList.length !== 1 || !singleMode
Expand All @@ -35,24 +33,21 @@ export function createReactNativeApp (AppEntry: any, config: RNAppConfig, FirstP

const appRef = createRef<AppInstance>()
const isReactComponent = isClassComponent(AppEntry)
let entryComponent:any = AppEntry
if(!isReactComponent){
let entryComponent: any = AppEntry
if (!isReactComponent) {
// eslint-disable-next-line react/display-name
entryComponent = forwardRef((props, ref) => {
return <AppEntry forwardRef={ref} {...props} />
return <AppEntry forwardRef={ref} {...props} />
})
}


const NewAppComponent = (AppComponent) => {
return class Entry extends Component <any, any> {

constructor (props){
return class Entry extends Component<any, any> {
constructor (props) {
super(props)
const { initPath = '', initParams = {} } = this.props
routerConfig.initPath = initPath
routerConfig.initParams = initParams

}

componentDidMount () {
Expand All @@ -65,8 +60,8 @@ export function createReactNativeApp (AppEntry: any, config: RNAppConfig, FirstP
}

// 导航onUnhandledAction
onUnhandledAction (options){
triggerAppLifecycle('onPageNotFound',options)
onUnhandledAction (options) {
triggerAppLifecycle('onPageNotFound', options)
}

render () {
Expand All @@ -80,11 +75,13 @@ export function createReactNativeApp (AppEntry: any, config: RNAppConfig, FirstP
let routerOptions: any = {}
if (needNavigate) {
routerOptions = {
onUnhandledAction: this.onUnhandledAction,
onUnhandledAction: this.onUnhandledAction
}
}

const child = needNavigate ? createRouter(routerConfig, routerOptions) : createElement(FirstPage, { ...this.props }, [])
const child = needNavigate
? createRouter(routerConfig, routerOptions)
: createElement(FirstPage, { ...this.props }, [])

return createElement(
RootSiblingParent,
Expand All @@ -98,58 +95,61 @@ export function createReactNativeApp (AppEntry: any, config: RNAppConfig, FirstP
}
}
}

const App = NewAppComponent(entryComponent)

// 与小程序端实例保持一致
const appInst = Object.create({}, {
config: {
writable: true,
enumerable: true,
configurable: true,
value: config.appConfig
},
onLaunch: {
enumerable: true,
writable: true,
value (options) {
triggerAppLifecycle('onLaunch', options)
}
},
onShow: {
enumerable: true,
writable: true,
value (options) {
triggerAppLifecycle('componentDidShow', options)
}
},
onHide: {
enumerable: true,
writable: true,
value (options: unknown) {
triggerAppLifecycle('componentDidHide',options)
}
},
onPageNotFound: {
enumerable: true,
writable: true,
value (options) {
triggerAppLifecycle('onPageNotFound', options)
const appInst = Object.create(
{},
{
config: {
writable: true,
enumerable: true,
configurable: true,
value: config.appConfig
},
onLaunch: {
enumerable: true,
writable: true,
value (options) {
triggerAppLifecycle('onLaunch', options)
}
},
onShow: {
enumerable: true,
writable: true,
value (options) {
triggerAppLifecycle('componentDidShow', options)
}
},
onHide: {
enumerable: true,
writable: true,
value (options: unknown) {
triggerAppLifecycle('componentDidHide', options)
}
},
onPageNotFound: {
enumerable: true,
writable: true,
value (options) {
triggerAppLifecycle('onPageNotFound', options)
}
}
},
})
}
)

function triggerAppLifecycle (lifecycle: keyof PageLifeCycle | keyof AppInstance, ...args){
function triggerAppLifecycle (lifecycle: keyof PageLifeCycle | keyof AppInstance, ...args) {
try {
const app = appRef.current
if(isReactComponent){
if (isReactComponent) {
app?.[lifecycle] && app?.[lifecycle](...args)
}else{
} else {
const instance = getPageInstance(HOOKS_APP_ID)
if(instance){
if (instance) {
const func = instance[lifecycle]
if (Array.isArray(func)) {
func.forEach(cb => cb.apply(app, args))
func.forEach((cb) => cb.apply(app, args))
}
}
}
Expand Down
Loading

0 comments on commit 0a2116c

Please sign in to comment.