private void recreateReactContextInBackground( JavaScriptExecutor.Factory jsExecutorFactory, JSBundleLoader jsBundleLoader) { UiThreadUtil.assertOnUiThread(); ReactContextInitParams initParams = new ReactContextInitParams(jsExecutorFactory, jsBundleLoader); if (mReactContextInitAsyncTask == null) { // No background task to create react context is currently running, create and execute one. mReactContextInitAsyncTask = new ReactContextInitAsyncTask(); mReactContextInitAsyncTask.execute(initParams); } else { // Background task is currently running, queue up most recent init params to recreate context // once task completes. mPendingReactContextInitParams = initParams; } }
@Override public CatalystInstance build() { return new CatalystInstanceImpl.Builder() .setReactQueueConfigurationSpec(ReactQueueConfigurationSpec.createDefault()) .setJSExecutor(new JSCJavaScriptExecutor(new WritableNativeMap())) .setRegistry(mNativeModuleRegistryBuilder.build()) .setJSModulesConfig(mJSModulesConfigBuilder.build()) .setJSBundleLoader(JSBundleLoader.createFileLoader( mContext, "assets://AndroidTestBundle.js")) .setNativeModuleCallExceptionHandler( new NativeModuleCallExceptionHandler() { @Override public void handleException(Exception e) { throw new RuntimeException(e); } }) .build(); }
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"); } }
@ReactMethod public void startThread(final String jsFileName, final Promise promise) { Log.d(TAG, "Starting web thread - " + jsFileName); String jsFileSlug = jsFileName.contains("/") ? jsFileName.replaceAll("/", "_") : 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); } }
private JSBundleLoader createDevBundleLoader(String jsFileName, String jsFileSlug) { String bundleUrl = bundleUrlForFile(jsFileName); String bundleOut = getReactApplicationContext().getFilesDir().getAbsolutePath() + "/" + jsFileSlug; Log.d(TAG, "createDevBundleLoader - download web thread to - " + bundleOut); downloadScriptToFileSync(bundleUrl, bundleOut); return JSBundleLoader.createCachedBundleFromNetworkLoader(bundleUrl, bundleOut); }
private void onReloadWithJSDebugger(JavaJSExecutor.Factory jsExecutorFactory) { recreateReactContextInBackground( new ProxyJavaScriptExecutor.Factory(jsExecutorFactory), JSBundleLoader.createRemoteDebuggerBundleLoader( mDevSupportManager.getJSBundleURLForRemoteDebugging(), mDevSupportManager.getSourceUrl())); }
private void onJSBundleLoadedFromServer() { recreateReactContextInBackground( new JSCJavaScriptExecutor.Factory(), JSBundleLoader.createCachedBundleFromNetworkLoader( mDevSupportManager.getSourceUrl(), mDevSupportManager.getDownloadedJSBundleFile())); }
@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); } }
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); }
public ReactContextBuilder setJSBundleLoader(JSBundleLoader jsBundleLoader) { this.jsBundleLoader = jsBundleLoader; return this; }
private JSBundleLoader createReleaseBundleLoader(String jsFileName, String jsFileSlug) { Log.d(TAG, "createReleaseBundleLoader - reading file from assets"); return JSBundleLoader.createAssetLoader(reactApplicationContext, "assets://threads/" + jsFileSlug + ".bundle", false); }
public ReactContextInitParams( JavaScriptExecutor.Factory jsExecutorFactory, JSBundleLoader jsBundleLoader) { mJsExecutorFactory = Assertions.assertNotNull(jsExecutorFactory); mJsBundleLoader = Assertions.assertNotNull(jsBundleLoader); }
public JSBundleLoader getJsBundleLoader() { return mJsBundleLoader; }
private void recreateReactContextInBackgroundFromBundleFile() { recreateReactContextInBackground( new JSCJavaScriptExecutor.Factory(), JSBundleLoader.createFileLoader(mApplicationContext, mJSBundleFile)); }
private JSBundleLoader createReleaseBundleLoader(String jsFileName, String jsFileSlug) { Log.d(TAG, "createReleaseBundleLoader - reading file from assets"); return JSBundleLoader.createFileLoader(getReactApplicationContext(), "assets://workers/" + jsFileSlug + ".bundle"); }