Skip to content
This repository was archived by the owner on Sep 4, 2021. It is now read-only.

Commit 3d39521

Browse files
author
devfd
committed
Android - load web worker from assets when release build
1 parent 9023eea commit 3d39521

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

android/src/main/java/co/apptailor/Worker/WorkerModule.java

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,30 @@ public String getName() {
4545

4646
@ReactMethod
4747
public void startWorker(final String jsFileName, final Promise promise) {
48-
final String jsFileSlug = jsFileName.contains("/") ? jsFileName.replaceAll("/", "_") : jsFileName;
49-
final String bundleUrl = bundleUrlForFile(jsFileName);
50-
final String bundleOut = getReactApplicationContext().getFilesDir().getAbsolutePath() + "/" + jsFileSlug;
48+
Log.d(TAG, "Starting web worker - " + jsFileName);
5149

52-
//TODO handle when release build
53-
downloadScriptToFileSync(bundleUrl, bundleOut);
50+
String jsFileSlug = jsFileName.contains("/") ? jsFileName.replaceAll("/", "_") : jsFileName;
51+
52+
JSBundleLoader bundleLoader = getDevSupportManager().getDevSupportEnabled()
53+
? createDevBundleLoader(jsFileName, jsFileSlug)
54+
: createReleaseBundleLoader(jsFileName, jsFileSlug);
5455

5556
try {
57+
ReactContextBuilder workerContextBuilder = new ReactContextBuilder(getReactApplicationContext())
58+
.setJSBundleLoader(bundleLoader)
59+
.setDevSupportManager(getDevSupportManager())
60+
.setReactPackage(new BaseReactPackage(getReactInstanceManager()));
61+
5662
JSWorker worker = new JSWorker(jsFileSlug);
5763
worker.runFromContext(
5864
getReactApplicationContext(),
59-
createCatalystBuilder(bundleUrl, bundleOut)
65+
workerContextBuilder
6066
);
6167
workers.put(worker.getWorkerId(), worker);
6268
promise.resolve(worker.getWorkerId());
6369
} catch (Exception e) {
64-
e.printStackTrace();
6570
promise.reject(e);
71+
getDevSupportManager().handleException(e);
6672
}
6773
}
6874

@@ -150,6 +156,21 @@ public void onCatalystInstanceDestroy() {
150156
* Helper methods
151157
*/
152158

159+
private JSBundleLoader createDevBundleLoader(String jsFileName, String jsFileSlug) {
160+
String bundleUrl = bundleUrlForFile(jsFileName);
161+
String bundleOut = getReactApplicationContext().getFilesDir().getAbsolutePath() + "/" + jsFileSlug;
162+
163+
Log.d(TAG, "createDevBundleLoader - download web worker to - " + bundleOut);
164+
downloadScriptToFileSync(bundleUrl, bundleOut);
165+
166+
return JSBundleLoader.createCachedBundleFromNetworkLoader(bundleUrl, bundleOut);
167+
}
168+
169+
private JSBundleLoader createReleaseBundleLoader(String jsFileName, String jsFileSlug) {
170+
Log.d(TAG, "createReleaseBundleLoader - reading file from assets");
171+
return JSBundleLoader.createFileLoader(getReactApplicationContext(), "assets://workers/" + jsFileSlug + ".bundle");
172+
}
173+
153174
private ReactInstanceManager getReactInstanceManager() {
154175
ReactApplication reactApplication = (ReactApplication)getCurrentActivity().getApplication();
155176
return reactApplication.getReactNativeHost().getReactInstanceManager();
@@ -169,15 +190,6 @@ private String bundleUrlForFile(final String fileName) {
169190
+ ".bundle?platform=android&dev=true&hot=false&minify=false";
170191
}
171192

172-
private ReactContextBuilder createCatalystBuilder(String bundleUrl, String bundleOut) {
173-
JSBundleLoader bundleLoader = JSBundleLoader.createCachedBundleFromNetworkLoader(bundleUrl, bundleOut);
174-
175-
return new ReactContextBuilder(getReactApplicationContext())
176-
.setJSBundleLoader(bundleLoader)
177-
.setDevSupportManager(getDevSupportManager())
178-
.setReactPackage(new BaseReactPackage(getReactInstanceManager()));
179-
}
180-
181193
private void downloadScriptToFileSync(String bundleUrl, String bundleOut) {
182194
OkHttpClient client = new OkHttpClient();
183195
final File out = new File(bundleOut);

0 commit comments

Comments
 (0)