com.facebook.react.bridge.JSBundleLoader Java Examples

The following examples show how to use com.facebook.react.bridge.JSBundleLoader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RCTUpdate.java    From react-native-update with MIT License 6 votes vote down vote up
private void setJSBundle(ReactInstanceManager instanceManager, String latestJSBundleFile) throws IllegalAccessException {
    try {
        JSBundleLoader latestJSBundleLoader;
        if (latestJSBundleFile.toLowerCase().startsWith("assets://")) {
            latestJSBundleLoader = JSBundleLoader.createAssetLoader(getReactApplicationContext(), latestJSBundleFile, false);
        } else {
            latestJSBundleLoader = JSBundleLoader.createFileLoader(latestJSBundleFile);
        }

        Field bundleLoaderField = instanceManager.getClass().getDeclaredField("mBundleLoader");
        bundleLoaderField.setAccessible(true);
        bundleLoaderField.set(instanceManager, latestJSBundleLoader);
    } catch (Exception e) {
        throw new IllegalAccessException("Could not setJSBundle");
    }
}
 
Example #2
Source File: ReactInstanceManager.java    From react-native-GPay with MIT License 6 votes vote down vote up
@ThreadConfined(UI)
private void recreateReactContextInBackground(
  JavaScriptExecutorFactory jsExecutorFactory,
  JSBundleLoader jsBundleLoader) {
  Log.d(ReactConstants.TAG, "ReactInstanceManager.recreateReactContextInBackground()");
  UiThreadUtil.assertOnUiThread();

  final ReactContextInitParams initParams = new ReactContextInitParams(
    jsExecutorFactory,
    jsBundleLoader);
  if (mCreateReactContextThread == null) {
    runCreateReactContextOnNewThread(initParams);
  } else {
    mPendingReactContextInitParams = initParams;
  }
}
 
Example #3
Source File: ReactInstanceManager.java    From react-native-GPay with MIT License 5 votes vote down vote up
@ThreadConfined(UI)
private void onReloadWithJSDebugger(JavaJSExecutor.Factory jsExecutorFactory) {
  Log.d(ReactConstants.TAG, "ReactInstanceManager.onReloadWithJSDebugger()");
  recreateReactContextInBackground(
      new ProxyJavaScriptExecutor.Factory(jsExecutorFactory),
      JSBundleLoader.createRemoteDebuggerBundleLoader(
          mDevSupportManager.getJSBundleURLForRemoteDebugging(),
          mDevSupportManager.getSourceUrl()));
}
 
Example #4
Source File: ReactInstanceManager.java    From react-native-GPay with MIT License 5 votes vote down vote up
@ThreadConfined(UI)
private void onJSBundleLoadedFromServer(@Nullable NativeDeltaClient nativeDeltaClient) {
  Log.d(ReactConstants.TAG, "ReactInstanceManager.onJSBundleLoadedFromServer()");

  JSBundleLoader bundleLoader = nativeDeltaClient == null
      ? JSBundleLoader.createCachedBundleFromNetworkLoader(
          mDevSupportManager.getSourceUrl(),
          mDevSupportManager.getDownloadedJSBundleFile())
      : JSBundleLoader.createDeltaFromNetworkLoader(
          mDevSupportManager.getSourceUrl(), nativeDeltaClient);

  recreateReactContextInBackground(mJavaScriptExecutorFactory, bundleLoader);
}
 
Example #5
Source File: ReactInstanceManagerBuilder.java    From react-native-GPay with MIT License 5 votes vote down vote up
/**
 * Path to the JS bundle file to be loaded from the file system.
 *
 * Example: {@code "assets://index.android.js" or "/sdcard/main.jsbundle"}
 */
public ReactInstanceManagerBuilder setJSBundleFile(String jsBundleFile) {
  if (jsBundleFile.startsWith("assets://")) {
    mJSBundleAssetUrl = jsBundleFile;
    mJSBundleLoader = null;
    return this;
  }
  return setJSBundleLoader(JSBundleLoader.createFileLoader(jsBundleFile));
}
 
Example #6
Source File: WorkerModule.java    From react-native-workers with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private JSBundleLoader createDevBundleLoader(String jsFileName, String jsFileSlug) {
    String bundleUrl = bundleUrlForFile(jsFileName);
    String bundleOut = getReactApplicationContext().getFilesDir().getAbsolutePath() + "/" + jsFileSlug;

    Log.d(TAG, "createDevBundleLoader - download web worker to - " + bundleOut);
    downloadScriptToFileSync(bundleUrl, bundleOut);

    return JSBundleLoader.createCachedBundleFromNetworkLoader(bundleUrl, bundleOut);
}
 
Example #7
Source File: WorkerModule.java    From react-native-workers with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@ReactMethod
public void startWorker(final String jsFileName, final Promise promise) {
    Log.d(TAG, "Starting web worker - " + jsFileName);

    String jsFileSlug = jsFileName.contains("/") ? jsFileName.replaceAll("/", "_") : jsFileName;

    JSBundleLoader bundleLoader = getDevSupportManager().getDevSupportEnabled()
            ? createDevBundleLoader(jsFileName, jsFileSlug)
            : createReleaseBundleLoader(jsFileName, jsFileSlug);

    try {
        ArrayList<ReactPackage> workerPackages = new ArrayList<ReactPackage>(Arrays.asList(additionalWorkerPackages));
        workerPackages.add(0, new BaseReactPackage(getReactInstanceManager()));

        ReactContextBuilder workerContextBuilder = new ReactContextBuilder(getReactApplicationContext())
                .setJSBundleLoader(bundleLoader)
                .setDevSupportManager(getDevSupportManager())
                .setReactPackages(workerPackages);

        JSWorker worker = new JSWorker(jsFileSlug);
        worker.runFromContext(
                getReactApplicationContext(),
                workerContextBuilder
        );
        workers.put(worker.getWorkerId(), worker);
        promise.resolve(worker.getWorkerId());
    } catch (Exception e) {
        promise.reject(e);
        getDevSupportManager().handleException(e);
    }
}
 
Example #8
Source File: RNThreadModule.java    From react-native-threads with MIT License 5 votes vote down vote up
@ReactMethod
public void startThread(final String jsFileName, final Promise promise) {
  Log.d(TAG, "Starting web thread - " + jsFileName);

  // When we create the absolute file path later, a "./" will break it.
  // Remove the leading "./" if it exists.
  String jsFileSlug = jsFileName.contains("./")
    ? jsFileName.replace("./", "")
    : jsFileName;

  JSBundleLoader bundleLoader = getDevSupportManager().getDevSupportEnabled()
          ? createDevBundleLoader(jsFileName, jsFileSlug)
          : createReleaseBundleLoader(jsFileName, jsFileSlug);

  try {
    ArrayList<ReactPackage> threadPackages = new ArrayList<ReactPackage>(Arrays.asList(additionalThreadPackages));
    threadPackages.add(0, new ThreadBaseReactPackage(getReactInstanceManager()));

    ReactContextBuilder threadContextBuilder = new ReactContextBuilder(getReactApplicationContext())
            .setJSBundleLoader(bundleLoader)
            .setDevSupportManager(getDevSupportManager())
            .setReactInstanceManager(getReactInstanceManager())
            .setReactPackages(threadPackages);

    JSThread thread = new JSThread(jsFileSlug);
    thread.runFromContext(
            getReactApplicationContext(),
            threadContextBuilder
    );
    threads.put(thread.getThreadId(), thread);
    promise.resolve(thread.getThreadId());
  } catch (Exception e) {
    promise.reject(e);
    getDevSupportManager().handleException(e);
  }
}
 
Example #9
Source File: RNThreadModule.java    From react-native-threads with MIT License 5 votes vote down vote up
private JSBundleLoader createDevBundleLoader(String jsFileName, String jsFileSlug) {
  String bundleUrl = bundleUrlForFile(jsFileName);
  // nested file directory will not exist in the files dir during development,
  // so remove any leading directory paths to simply download a flat file into
  // the root of the files directory.
  String[] splitFileSlug = jsFileSlug.split("/");
  String bundleOut = getReactApplicationContext().getFilesDir().getAbsolutePath() + "/" + splitFileSlug[splitFileSlug.length - 1];

  Log.d(TAG, "createDevBundleLoader - download web thread to - " + bundleOut);
  downloadScriptToFileSync(bundleUrl, bundleOut);

  return JSBundleLoader.createCachedBundleFromNetworkLoader(bundleUrl, bundleOut);
}
 
Example #10
Source File: ReactInstanceManagerBuilder.java    From react-native-GPay with MIT License 4 votes vote down vote up
/**
 * Instantiates a new {@link ReactInstanceManager}.
 * Before calling {@code build}, the following must be called:
 * <ul>
 * <li> {@link #setApplication}
 * <li> {@link #setCurrentActivity} if the activity has already resumed
 * <li> {@link #setDefaultHardwareBackBtnHandler} if the activity has already resumed
 * <li> {@link #setJSBundleFile} or {@link #setJSMainModulePath}
 * </ul>
 */
public ReactInstanceManager build() {
  Assertions.assertNotNull(
    mApplication,
    "Application property has not been set with this builder");

  Assertions.assertCondition(
    mUseDeveloperSupport || mJSBundleAssetUrl != null || mJSBundleLoader != null,
    "JS Bundle File or Asset URL has to be provided when dev support is disabled");

  Assertions.assertCondition(
    mJSMainModulePath != null || mJSBundleAssetUrl != null || mJSBundleLoader != null,
    "Either MainModulePath or JS Bundle File needs to be provided");

  if (mUIImplementationProvider == null) {
    // create default UIImplementationProvider if the provided one is null.
    mUIImplementationProvider = new UIImplementationProvider();
  }

  // We use the name of the device and the app for debugging & metrics
  String appName = mApplication.getPackageName();
  String deviceName = getFriendlyDeviceName();

  return new ReactInstanceManager(
      mApplication,
      mCurrentActivity,
      mDefaultHardwareBackBtnHandler,
      mJavaScriptExecutorFactory == null
          ? new JSCJavaScriptExecutorFactory(appName, deviceName)
          : mJavaScriptExecutorFactory,
      (mJSBundleLoader == null && mJSBundleAssetUrl != null)
          ? JSBundleLoader.createAssetLoader(
              mApplication, mJSBundleAssetUrl, false /*Asynchronous*/)
          : mJSBundleLoader,
      mJSMainModulePath,
      mPackages,
      mUseDeveloperSupport,
      mBridgeIdleDebugListener,
      Assertions.assertNotNull(mInitialLifecycleState, "Initial lifecycle state was not set"),
      mUIImplementationProvider,
      mNativeModuleCallExceptionHandler,
      mRedBoxHandler,
      mLazyViewManagersEnabled,
      mDevBundleDownloadListener,
      mMinNumShakes,
      mMinTimeLeftInFrameForNonBatchedOperationMs,
      mJSIModulesPackage,
      mCustomPackagerCommandHandlers);
}
 
Example #11
Source File: ReactContextBuilder.java    From react-native-workers with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public ReactContextBuilder setJSBundleLoader(JSBundleLoader jsBundleLoader) {
    this.jsBundleLoader = jsBundleLoader;
    return this;
}
 
Example #12
Source File: WorkerModule.java    From react-native-workers with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private JSBundleLoader createReleaseBundleLoader(String jsFileName, String jsFileSlug) {
    Log.d(TAG, "createReleaseBundleLoader - reading file from assets");
    return JSBundleLoader.createFileLoader(getReactApplicationContext(), "assets://workers/" + jsFileSlug + ".bundle");
}
 
Example #13
Source File: RNThreadModule.java    From react-native-threads with MIT License 4 votes vote down vote up
private JSBundleLoader createReleaseBundleLoader(String jsFileName, String jsFileSlug) {
  Log.d(TAG, "createReleaseBundleLoader - reading file from assets");
  return JSBundleLoader.createAssetLoader(reactApplicationContext, "assets://threads/" + jsFileSlug + ".bundle", false);
}
 
Example #14
Source File: ReactContextBuilder.java    From react-native-threads with MIT License 4 votes vote down vote up
public ReactContextBuilder setJSBundleLoader(JSBundleLoader jsBundleLoader) {
    this.jsBundleLoader = jsBundleLoader;
    return this;
}
 
Example #15
Source File: ReactInstanceManager.java    From react-native-GPay with MIT License 4 votes vote down vote up
public ReactContextInitParams(
    JavaScriptExecutorFactory jsExecutorFactory,
    JSBundleLoader jsBundleLoader) {
  mJsExecutorFactory = Assertions.assertNotNull(jsExecutorFactory);
  mJsBundleLoader = Assertions.assertNotNull(jsBundleLoader);
}
 
Example #16
Source File: ReactInstanceManager.java    From react-native-GPay with MIT License 4 votes vote down vote up
/**
 * @return instance of {@link ReactContext} configured a {@link CatalystInstance} set
 */
private ReactApplicationContext createReactContext(
    JavaScriptExecutor jsExecutor,
    JSBundleLoader jsBundleLoader) {
  Log.d(ReactConstants.TAG, "ReactInstanceManager.createReactContext()");
  ReactMarker.logMarker(CREATE_REACT_CONTEXT_START, jsExecutor.getName());
  final ReactApplicationContext reactContext = new ReactApplicationContext(mApplicationContext);

  NativeModuleCallExceptionHandler exceptionHandler = mNativeModuleCallExceptionHandler != null
      ? mNativeModuleCallExceptionHandler
      : mDevSupportManager;
  reactContext.setNativeModuleCallExceptionHandler(exceptionHandler);

  NativeModuleRegistry nativeModuleRegistry = processPackages(reactContext, mPackages, false);

  CatalystInstanceImpl.Builder catalystInstanceBuilder = new CatalystInstanceImpl.Builder()
    .setReactQueueConfigurationSpec(ReactQueueConfigurationSpec.createDefault())
    .setJSExecutor(jsExecutor)
    .setRegistry(nativeModuleRegistry)
    .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 (mJSIModulePackage != null) {
    catalystInstance.addJSIModules(mJSIModulePackage
      .getJSIModules(reactContext, catalystInstance.getJavaScriptContextHolder()));
  }

  if (mBridgeIdleDebugListener != null) {
    catalystInstance.addBridgeIdleDebugListener(mBridgeIdleDebugListener);
  }
  if (Systrace.isTracing(TRACE_TAG_REACT_APPS | TRACE_TAG_REACT_JS_VM_CALLS)) {
    catalystInstance.setGlobalVariable("__RCTProfileIsProfiling", "true");
  }
  ReactMarker.logMarker(ReactMarkerConstants.PRE_RUN_JS_BUNDLE_START);
  catalystInstance.runJSBundle();
  reactContext.initializeWithInstance(catalystInstance);

  return reactContext;
}
 
Example #17
Source File: ReactInstanceManager.java    From react-native-GPay with MIT License 4 votes vote down vote up
ReactInstanceManager(
  Context applicationContext,
  @Nullable Activity currentActivity,
  @Nullable DefaultHardwareBackBtnHandler defaultHardwareBackBtnHandler,
  JavaScriptExecutorFactory javaScriptExecutorFactory,
  @Nullable JSBundleLoader bundleLoader,
  @Nullable String jsMainModulePath,
  List<ReactPackage> packages,
  boolean useDeveloperSupport,
  @Nullable NotThreadSafeBridgeIdleDebugListener bridgeIdleDebugListener,
  LifecycleState initialLifecycleState,
  @Nullable UIImplementationProvider mUIImplementationProvider,
  NativeModuleCallExceptionHandler nativeModuleCallExceptionHandler,
  @Nullable RedBoxHandler redBoxHandler,
  boolean lazyViewManagersEnabled,
  @Nullable DevBundleDownloadListener devBundleDownloadListener,
  int minNumShakes,
  int minTimeLeftInFrameForNonBatchedOperationMs,
  @Nullable JSIModulePackage jsiModulePackage,
  @Nullable Map<String, RequestHandler> customPackagerCommandHandlers) {
  Log.d(ReactConstants.TAG, "ReactInstanceManager.ctor()");
  initializeSoLoaderIfNecessary(applicationContext);

  DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(applicationContext);

  mApplicationContext = applicationContext;
  mCurrentActivity = currentActivity;
  mDefaultBackButtonImpl = defaultHardwareBackBtnHandler;
  mJavaScriptExecutorFactory = javaScriptExecutorFactory;
  mBundleLoader = bundleLoader;
  mJSMainModulePath = jsMainModulePath;
  mPackages = new ArrayList<>();
  mUseDeveloperSupport = useDeveloperSupport;
  mDevSupportManager =
      DevSupportManagerFactory.create(
          applicationContext,
          createDevHelperInterface(),
          mJSMainModulePath,
          useDeveloperSupport,
          redBoxHandler,
          devBundleDownloadListener,
          minNumShakes,
          customPackagerCommandHandlers);
  mBridgeIdleDebugListener = bridgeIdleDebugListener;
  mLifecycleState = initialLifecycleState;
  mMemoryPressureRouter = new MemoryPressureRouter(applicationContext);
  mNativeModuleCallExceptionHandler = nativeModuleCallExceptionHandler;
  synchronized (mPackages) {
    PrinterHolder.getPrinter()
        .logMessage(ReactDebugOverlayTags.RN_CORE, "RNCore: Use Split Packages");
    mPackages.add(
        new CoreModulesPackage(
            this,
            new DefaultHardwareBackBtnHandler() {
              @Override
              public void invokeDefaultOnBackPressed() {
                ReactInstanceManager.this.invokeDefaultOnBackPressed();
              }
            },
            mUIImplementationProvider,
            lazyViewManagersEnabled,
            minTimeLeftInFrameForNonBatchedOperationMs));
    if (mUseDeveloperSupport) {
      mPackages.add(new DebugCorePackage());
    }
    mPackages.addAll(packages);
  }
  mJSIModulePackage = jsiModulePackage;

  // Instantiate ReactChoreographer in UI thread.
  ReactChoreographer.initialize();
  if (mUseDeveloperSupport) {
    mDevSupportManager.startInspector();
  }
}
 
Example #18
Source File: ReactInstanceManager.java    From react-native-GPay with MIT License 4 votes vote down vote up
public JSBundleLoader getJsBundleLoader() {
  return mJsBundleLoader;
}
 
Example #19
Source File: ReactInstanceManagerBuilder.java    From react-native-GPay with MIT License 2 votes vote down vote up
/**
 * Bundle loader to use when setting up JS environment. This supersedes
 * prior invocations of {@link setJSBundleFile} and {@link setBundleAssetName}.
 *
 * Example: {@code JSBundleLoader.createFileLoader(application, bundleFile)}
 */
public ReactInstanceManagerBuilder setJSBundleLoader(JSBundleLoader jsBundleLoader) {
  mJSBundleLoader = jsBundleLoader;
  mJSBundleAssetUrl = null;
  return this;
}