WebWorkers: Allow web worker owner to inject globals into worker, supply modified __fbBatchedBridgeConfig

Summary: Now that web workers are just JSCExecutors, we can move forward with adding native module support. The means we need to supply the worker with a correct __fbBatchedBridgeConfig global so it can appropriately set up its JS MessageQueue. Unfortunately, native modules can't support multiple JS execution contexts out-of-the-box, so we need to whitelist those modules that actually can be referenced from a webworker. In order to do that, we add the supportsWebWorkers call in NativeModule and the SupportsWebWorkers annotation for JS modules. These add metadata to __fbBatchedBridgeConfig which allows us to create a new config with only those modules that support web workers.

Reviewed By: lexs

Differential Revision: D2927091

fb-gh-sync-id: 9b47331253b277940b552e7d899198b5f0a3ed8c
shipit-source-id: 9b47331253b277940b552e7d899198b5f0a3ed8c
This commit is contained in:
Andy Street
2016-02-19 08:21:55 -08:00
committed by facebook-github-bot-5
parent febb1fbe13
commit 2b251638a6
7 changed files with 48 additions and 9 deletions

View File

@@ -341,4 +341,9 @@ public abstract class BaseJavaModule implements NativeModule {
public void onCatalystInstanceDestroy() {
// do nothing
}
@Override
public boolean supportsWebWorkers() {
return false;
}
}

View File

@@ -52,6 +52,9 @@ public class JavaScriptModulesConfig {
jg.writeEndObject();
}
jg.writeEndObject();
if (registration.getModuleInterface().isAnnotationPresent(SupportsWebWorkers.class)) {
jg.writeBooleanField("supportsWebWorkers", true);
}
}
public static class Builder {

View File

@@ -72,4 +72,9 @@ public interface NativeModule {
* Called before {CatalystInstance#onHostDestroy}
*/
void onCatalystInstanceDestroy();
/**
* Whether this native module supports calls from web workers. Ignored for now.
*/
boolean supportsWebWorkers();
}

View File

@@ -65,6 +65,7 @@ public class NativeModuleRegistry {
for (ModuleDefinition moduleDef : mModuleTable) {
jg.writeObjectFieldStart(moduleDef.name);
jg.writeNumberField("moduleID", moduleDef.id);
jg.writeBooleanField("supportsWebWorkers", moduleDef.target.supportsWebWorkers());
jg.writeObjectFieldStart("methods");
for (int i = 0; i < moduleDef.methods.size(); i++) {
MethodRegistration method = moduleDef.methods.get(i);

View File

@@ -0,0 +1,15 @@
// Copyright 2004-present Facebook. All Rights Reserved.
package com.facebook.react.bridge;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Annotation indicating that a JS module should be made available to web
* workers spawned by the main JS executor.
*/
@Retention(RUNTIME)
public @interface SupportsWebWorkers {
}