在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
在 React Native 启动流程简析 这篇文章里,我们梳理了 首先来看 mReactRootView.startReactApplication( getReactNativeHost().getReactInstanceManager(), appKey, mLaunchOptions); 可以看到是在 顺着
1.创建 final ReactContextInitParams initParams = new ReactContextInitParams(jsExecutorFactory, jsBundleLoader); 2.调用
createReactContext先看其调用的地方: final ReactApplicationContext reactApplicationContext = createReactContext( initParams.getJsExecutorFactory().create(), initParams.getJsBundleLoader()); 其两个入参分别为 JavaScriptExecutor
回顾 React Native 启动流程简析,在创建过程中实际上是调用下面的方法: ReactInstanceManager reactInstanceManager = builder.build()
private JavaScriptExecutorFactory getDefaultJSExecutorFactory( String appName, String deviceName, Context applicationContext) { try { // If JSC is included, use it as normal initializeSoLoaderIfNecessary(applicationContext); SoLoader.loadLibrary("jscexecutor"); return new JSCExecutorFactory(appName, deviceName); } catch (UnsatisfiedLinkError jscE) { /* ... */ } } 也就是说在创建 @Override public JavaScriptExecutor create() throws Exception { WritableNativeMap jscConfig = new WritableNativeMap(); jscConfig.putString("OwnerIdentity", "ReactNative"); jscConfig.putString("AppIdentity", mAppName); jscConfig.putString("DeviceIdentity", mDeviceName); return new JSCExecutor(jscConfig); } 再往下看 @DoNotStrip /* package */ class JSCExecutor extends JavaScriptExecutor { static { SoLoader.loadLibrary("jscexecutor"); } /* package */ JSCExecutor(ReadableNativeMap jscConfig) { super(initHybrid(jscConfig)); } @Override public String getName() { return "JSCExecutor"; } private static native HybridData initHybrid(ReadableNativeMap jscConfig); } 于是就很清楚了, JsBundleLoader同样的,在 来到其定义之处,发现其返回了 public static JSBundleLoader createAssetLoader( final Context context, final String assetUrl, final boolean loadSynchronously) { return new JSBundleLoader() { @Override public String loadScript(JSBundleLoaderDelegate delegate) { delegate.loadScriptFromAssets(context.getAssets(), assetUrl, loadSynchronously); return assetUrl; } }; } 在创建完 createReactContextprivate ReactApplicationContext createReactContext( final ReactApplicationContext reactContext = new ReactApplicationContext(mApplicationContext); CatalystInstanceImpl.Builder catalystInstanceBuilder = /* ... */ try { catalystInstance = catalystInstanceBuilder.build(); } finally { /* ... */ } reactContext.initializeWithInstance(catalystInstance); TurboModuleManager turboModuleManager = new TurboModuleManager( /* ... */ ) catalystInstance.setTurboModuleManager(turboModuleManager); if (mJSIModulePackage != null) { catalystInstance.addJSIModules( /* ... */ ); } catalystInstance.runJSBundle(); return reactContext; 在这里面,首先创建了 initializeWithInstance通过调用 runJSBundlepublic void runJSBundle() { mJSBundleLoader.loadScript(CatalystInstanceImpl.this); synchronized (mJSCallsPendingInitLock) { mAcceptCalls = true; for (PendingJSCall function : mJSCallsPendingInit) { function.call(this); } mJSCallsPendingInit.clear(); mJSBundleHasLoaded = true; } Systrace.registerListener(mTraceListener); } 通过先前返回的 public String loadScript(JSBundleLoaderDelegate delegate) { delegate.loadScriptFromAssets(context.getAssets(), assetUrl, loadSynchronously); return assetUrl; }
public void loadScriptFromAssets( AssetManager assetManager, String assetURL, boolean loadSynchronously) { mSourceURL = assetURL; jniLoadScriptFromAssets(assetManager, assetURL, loadSynchronously); } 这里的
通过 setupReactContextprivate void setupReactContext(final ReactApplicationContext reactContext) { synchronized (mAttachedReactRoots) { catalystInstance.initialize(); for (ReactRoot reactRoot : mAttachedReactRoots) { if (reactRoot.getState().compareAndSet(ReactRoot.STATE_STOPPED, ReactRoot.STATE_STARTED)) { attachRootViewToInstance(reactRoot); } } } UiThreadUtil.runOnUiThread( public void run() { listener.onReactContextInitialized(reactContext); } ) reactContext.runOnJSQueueThread( public void run() { Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); } ) reactContext.runOnNativeModulesQueueThread( public void run() { Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); } ) } 这里面做的事情如下:
总结本文从 createReactContext 和 setupReactContext 两个方法的源码出发,分析了 RN startReactApplication 方法的执行过程,其中: createReactContext 的主要作用是:创建 setupReactContext的主要作用是:初始化所有原生模块,绘制所有 rootview,创建 UI 模块、JS 模块和原生模块线程,并设置优先级。 到此这篇关于React Native startReactApplication 方法简析的文章就介绍到这了,更多相关React Native startReactApplication内容请搜索极客世界以前的文章或继续浏览下面的相关文章希望大家以后多多支持极客世界! |
请发表评论