Пример #1
0
 @Override
 public CatalystInstance build() {
   JavaScriptExecutor executor = null;
   try {
     executor = new JSCJavaScriptExecutor.Factory(new WritableNativeMap()).create();
   } catch (Exception e) {
     throw new RuntimeException(e);
   }
   return new CatalystInstanceImpl.Builder()
       .setReactQueueConfigurationSpec(ReactQueueConfigurationSpec.createDefault())
       .setJSExecutor(executor)
       .setRegistry(
           new NativeModuleRegistry(
               mModuleSpecList, Collections.<Class, ReactModuleInfo>emptyMap()))
       .setJSModuleRegistry(mJSModuleRegistryBuilder.build())
       .setJSBundleLoader(
           JSBundleLoader.createAssetLoader(mContext, "assets://AndroidTestBundle.js"))
       .setNativeModuleCallExceptionHandler(
           new NativeModuleCallExceptionHandler() {
             @Override
             public void handleException(Exception e) {
               throw new RuntimeException(e);
             }
           })
       .build();
 }
 private void processPackage(
     ReactPackage reactPackage,
     ReactApplicationContext reactContext,
     NativeModuleRegistry.Builder nativeRegistryBuilder,
     JavaScriptModuleRegistry.Builder jsModulesBuilder) {
   for (NativeModule nativeModule : reactPackage.createNativeModules(reactContext)) {
     nativeRegistryBuilder.add(nativeModule);
   }
   for (Class<? extends JavaScriptModule> jsModuleClass : reactPackage.createJSModules()) {
     jsModulesBuilder.add(jsModuleClass);
   }
 }
Пример #3
0
 @Override
 public ReactInstanceEasyBuilder addJSModule(Class moduleInterfaceClass) {
   mJSModuleRegistryBuilder.add(moduleInterfaceClass);
   return this;
 }
  /** @return instance of {@link ReactContext} configured a {@link CatalystInstance} set */
  private ReactApplicationContext createReactContext(
      JavaScriptExecutor jsExecutor, JSBundleLoader jsBundleLoader) {
    FLog.i(ReactConstants.TAG, "Creating react context.");
    ReactMarker.logMarker(CREATE_REACT_CONTEXT_START);
    mSourceUrl = jsBundleLoader.getSourceUrl();
    NativeModuleRegistry.Builder nativeRegistryBuilder = new NativeModuleRegistry.Builder();
    JavaScriptModuleRegistry.Builder jsModulesBuilder = new JavaScriptModuleRegistry.Builder();

    final ReactApplicationContext reactContext = new ReactApplicationContext(mApplicationContext);
    if (mUseDeveloperSupport) {
      reactContext.setNativeModuleCallExceptionHandler(mDevSupportManager);
    }

    ReactMarker.logMarker(PROCESS_PACKAGES_START);
    Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "createAndProcessCoreModulesPackage");
    try {
      CoreModulesPackage coreModulesPackage =
          new CoreModulesPackage(this, mBackBtnHandler, mUIImplementationProvider);
      processPackage(coreModulesPackage, reactContext, nativeRegistryBuilder, jsModulesBuilder);
    } finally {
      Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
    }

    // TODO(6818138): Solve use-case of native/js modules overriding
    for (ReactPackage reactPackage : mPackages) {
      Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "createAndProcessCustomReactPackage");
      try {
        processPackage(reactPackage, reactContext, nativeRegistryBuilder, jsModulesBuilder);
      } finally {
        Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
      }
    }
    ReactMarker.logMarker(PROCESS_PACKAGES_END);

    ReactMarker.logMarker(BUILD_NATIVE_MODULE_REGISTRY_START);
    Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "buildNativeModuleRegistry");
    NativeModuleRegistry nativeModuleRegistry;
    try {
      nativeModuleRegistry = nativeRegistryBuilder.build();
    } finally {
      Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
      ReactMarker.logMarker(BUILD_NATIVE_MODULE_REGISTRY_END);
    }

    NativeModuleCallExceptionHandler exceptionHandler =
        mNativeModuleCallExceptionHandler != null
            ? mNativeModuleCallExceptionHandler
            : mDevSupportManager;
    CatalystInstanceImpl.Builder catalystInstanceBuilder =
        new CatalystInstanceImpl.Builder()
            .setReactQueueConfigurationSpec(ReactQueueConfigurationSpec.createDefault())
            .setJSExecutor(jsExecutor)
            .setRegistry(nativeModuleRegistry)
            .setJSModuleRegistry(jsModulesBuilder.build())
            .setJSBundleLoader(jsBundleLoader)
            .setNativeModuleCallExceptionHandler(exceptionHandler);

    ReactMarker.logMarker(CREATE_CATALYST_INSTANCE_START);
    // CREATE_CATALYST_INSTANCE_END is in JSCExecutor.cpp
    Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "createCatalystInstance");
    final CatalystInstance catalystInstance;
    try {
      catalystInstance = catalystInstanceBuilder.build();
    } finally {
      Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
      ReactMarker.logMarker(CREATE_CATALYST_INSTANCE_END);
    }

    if (mBridgeIdleDebugListener != null) {
      catalystInstance.addBridgeIdleDebugListener(mBridgeIdleDebugListener);
    }

    ReactMarker.logMarker(RUN_JS_BUNDLE_START);
    try {
      catalystInstance
          .getReactQueueConfiguration()
          .getJSQueueThread()
          .callOnQueue(
              new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                  reactContext.initializeWithInstance(catalystInstance);

                  Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "runJSBundle");
                  try {
                    catalystInstance.runJSBundle();
                  } finally {
                    Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
                    ReactMarker.logMarker(RUN_JS_BUNDLE_END);
                  }
                  return null;
                }
              })
          .get();
    } catch (InterruptedException e) {
      throw new RuntimeException(e);
    } catch (ExecutionException e) {
      if (e.getCause() instanceof RuntimeException) {
        throw (RuntimeException) e.getCause();
      } else {
        throw new RuntimeException(e);
      }
    }

    return reactContext;
  }