Skip to content

Fix android backgrounding re-init issue #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ android {
}

dependencies {
compile 'com.facebook.react:react-native:+'
implementation 'com.facebook.react:react-native:+'
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.facebook.react.modules.systeminfo.AndroidInfoHelpers;
import com.facebook.react.packagerconnection.PackagerConnectionSettings;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.react.uimanager.UIImplementationProvider;
import static com.facebook.infer.annotation.ThreadConfined.UI;

import java.util.ArrayList;
Expand Down Expand Up @@ -164,11 +163,6 @@ protected List<ReactPackage> getPackages() {
allPackages.add(0, new MainReactPackage());
return allPackages;
}

@Override
protected UIImplementationProvider getUIImplementationProvider() {
return null;
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.module.annotations.ReactModule;

import javax.annotation.Nullable;

@ReactModule(name="WorkersInstanceManager")
public class WorkersInstanceManager extends ReactContextBaseJavaModule {

private final ReactContext context;
Expand Down
63 changes: 36 additions & 27 deletions android/src/main/java/com/staltz/react/workers/WorkersManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,32 @@
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.modules.systeminfo.AndroidInfoHelpers;
import com.facebook.react.module.annotations.ReactModule;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Nullable;

@ReactModule(name = "WorkersManager")
public class WorkersManager extends ReactContextBaseJavaModule {

private final ReactApplicationContext context;
private final ReactPackage packages[];
private Integer key;
private String bundleRoot;
private String bundleResource;
private Integer bundlerPort;
private Promise promise;

private final HashMap<Integer, WorkersInstance> workers = new HashMap<>();
private final List<Integer> bundlerPorts = new ArrayList<>();


public WorkersManager(
final ReactApplicationContext context,
final ReactPackage packages[]
final ReactApplicationContext context,
final ReactPackage packages[]
) {
super(context);
this.context = context;
Expand All @@ -49,24 +56,30 @@ public String getName() {

@ReactMethod
public void startWorker(
final Integer key,
final String bundleRoot,
final String bundleResource,
final Integer bundlerPort,
final Promise promise
final Integer key,
final String bundleRoot,
final String bundleResource,
final Integer bundlerPort,
final Promise promise
) {
Assertions.assertCondition(!this.workers.containsKey(key), "Key already in use");

this.key = key;
this.bundleRoot = bundleRoot;
this.bundleResource = bundleResource;
this.bundlerPort = bundlerPort;
this.promise = promise;

final boolean hasBundlerPort = this.allocateBundlerPort(bundlerPort);

final WorkersInstance worker = new WorkersInstance(
key,
context,
this.packages,
bundleRoot,
bundleResource,
hasBundlerPort ? bundlerPort : null,
promise
key,
context,
this.packages,
bundleRoot,
bundleResource,
hasBundlerPort ? bundlerPort : null,
promise
);

this.workers.put(key, worker);
Expand All @@ -81,20 +94,16 @@ public void run() {

@ReactMethod
public void stopWorker(final Integer key) {
final WorkersInstance worker = this.workers.remove(key);
Assertions.assertNotNull(worker);

new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
worker.stop();
}
});
this.workers.remove(key);
}

@ReactMethod
public void postMessage(final Integer key, final String message) {
final WorkersInstance worker = this.workers.get(key);
WorkersInstance worker = this.workers.get(key);
if (worker == null) {
startWorker(this.key, this.bundleRoot, this.bundleResource, this.bundlerPort, this.promise);
worker = this.workers.get(key);
}
Assertions.assertNotNull(worker).postMessage(message);
}

Expand All @@ -108,8 +117,8 @@ private interface RCTDeviceEventEmitter extends JavaScriptModule {

protected void emit(final String name, final Object body) {
this.context
.getJSModule(WorkersManager.RCTDeviceEventEmitter.class)
.emit(name, body);
.getJSModule(WorkersManager.RCTDeviceEventEmitter.class)
.emit(name, body);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-workers",
"version": "0.6.0",
"version": "0.6.6-gl-007",
"description": "React Native web workers",
"author": "Garrett Mitchell",
"license": "MIT",
Expand Down