Skip to content

Commit

Permalink
fix: correctly load ts_helpers.js in workers
Browse files Browse the repository at this point in the history
  • Loading branch information
farfromrefuge committed Dec 16, 2023
1 parent 643958b commit 5e32ce4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions test-app/app/src/main/java/com/tns/RuntimeHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public static Runtime initRuntime(Context context) {
StaticConfiguration config = new StaticConfiguration(logger, appName, nativeLibDir, rootDir,
appDir, classLoader, dexDir, dexThumb, appConfig, isDebuggable);

runtime = Runtime.initializeRuntimeWithConfiguration(config);
runtime = Runtime.initializeRuntimeWithConfiguration(config, appDir.toString());
if (isDebuggable) {
try {
v8Inspector = new AndroidJsV8Inspector(context.getFilesDir().getAbsolutePath(), context.getPackageName());
Expand Down Expand Up @@ -190,7 +190,7 @@ public static Runtime initRuntime(Context context) {
waitForLiveSync(context);
}

runtime.runScript(new File(appDir, "internal/ts_helpers.js"));
// runtime.runScript(new File(appDir, "internal/ts_helpers.js"));

File javaClassesModule = new File(appDir, "app/tns-java-classes.js");
if (javaClassesModule.exists()) {
Expand Down
18 changes: 11 additions & 7 deletions test-app/runtime/src/main/java/com/tns/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -445,13 +445,15 @@ private static class WorkerThread extends HandlerThread {
private ThreadScheduler mainThreadScheduler;
private String filePath;
private String callingJsDir;
private String appDir;

public WorkerThread(String name, Integer workerId, ThreadScheduler mainThreadScheduler, String callingJsDir) {
public WorkerThread(String name, Integer workerId, ThreadScheduler mainThreadScheduler, String callingJsDir, String appDir) {
super("W" + workerId + ": " + name);
this.filePath = name;
this.workerId = workerId;
this.mainThreadScheduler = mainThreadScheduler;
this.callingJsDir = callingJsDir;
this.appDir = appDir;
}

public void startRuntime() {
Expand All @@ -470,7 +472,7 @@ public void run() {
staticConfiguration.logger.write("Worker (id=" + workerId + ")'s Runtime is initializing!");
}

Runtime runtime = initRuntime(dynamicConfiguration);
Runtime runtime = initRuntime(dynamicConfiguration, appDir);

if (staticConfiguration.logger.isEnabled()) {
staticConfiguration.logger.write("Worker (id=" + workerId + ")'s Runtime initialized!");
Expand Down Expand Up @@ -575,11 +577,11 @@ else if (msg.arg1 == MessageType.BubbleUpException) {
This method initializes the runtime and should always be called first and through the main thread
in order to set static configuration that all following workers can use
*/
public static Runtime initializeRuntimeWithConfiguration(StaticConfiguration config) {
public static Runtime initializeRuntimeWithConfiguration(StaticConfiguration config, String appDir) {
staticConfiguration = config;
WorkThreadScheduler mainThreadScheduler = new WorkThreadScheduler(new MainThreadHandler(Looper.myLooper()));
DynamicConfiguration dynamicConfiguration = new DynamicConfiguration(0, mainThreadScheduler, null);
Runtime runtime = initRuntime(dynamicConfiguration);
Runtime runtime = initRuntime(dynamicConfiguration, appDir);
return runtime;
}

Expand All @@ -591,9 +593,10 @@ public static Runtime initializeRuntimeWithConfiguration(StaticConfiguration con
public static void initWorker(String jsFileName, String callingJsDir, int id) {
// This method will always be called from the Main thread
Runtime runtime = Runtime.getCurrentRuntime();
ThreadScheduler mainThreadScheduler = runtime.getDynamicConfig().myThreadScheduler;

WorkerThread worker = new WorkerThread(jsFileName, id, mainThreadScheduler, callingJsDir);
ThreadScheduler mainThreadScheduler = runtime.getDynamicConfig().myThreadScheduler;
String appDir = runtime.config.appDir.toString();
WorkerThread worker = new WorkerThread(jsFileName, id, mainThreadScheduler, callingJsDir, appDir);
worker.start();
worker.startRuntime();
}
Expand All @@ -602,9 +605,10 @@ public static void initWorker(String jsFileName, String callingJsDir, int id) {
This method deals with initializing the runtime with given configuration
Does it for both workers and for the main thread
*/
private static Runtime initRuntime(DynamicConfiguration dynamicConfiguration) {
private static Runtime initRuntime(DynamicConfiguration dynamicConfiguration, String appDir) {
Runtime runtime = new Runtime(staticConfiguration, dynamicConfiguration);
runtime.init();
runtime.runScript(new File(appDir, "internal/ts_helpers.js"));

return runtime;
}
Expand Down

0 comments on commit 5e32ce4

Please sign in to comment.