Generate module definition on demand
Reviewed By: astreet Differential Revision: D2707977 fb-gh-sync-id: fb2baa464a23df82e8b48a91a84c98370517d311
This commit is contained in:
parent
c25c98c00c
commit
30a5eb51f8
|
@ -361,16 +361,15 @@ public class CatalystInstanceImpl implements CatalystInstance {
|
|||
private String buildModulesConfigJSONProperty(
|
||||
NativeModuleRegistry nativeModuleRegistry,
|
||||
JavaScriptModulesConfig jsModulesConfig) {
|
||||
// TODO(5300733): Serialize config using single json generator
|
||||
JsonFactory jsonFactory = new JsonFactory();
|
||||
StringWriter writer = new StringWriter();
|
||||
try {
|
||||
JsonGenerator jg = jsonFactory.createGenerator(writer);
|
||||
jg.writeStartObject();
|
||||
jg.writeFieldName("remoteModuleConfig");
|
||||
jg.writeRawValue(nativeModuleRegistry.moduleDescriptions());
|
||||
nativeModuleRegistry.writeModuleDescriptions(jg);
|
||||
jg.writeFieldName("localModulesConfig");
|
||||
jg.writeRawValue(jsModulesConfig.moduleDescriptions());
|
||||
jsModulesConfig.writeModuleDescriptions(jg);
|
||||
jg.writeEndObject();
|
||||
jg.close();
|
||||
} catch (IOException ioe) {
|
||||
|
|
|
@ -10,12 +10,10 @@
|
|||
package com.facebook.react.bridge;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonFactory;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
|
||||
/**
|
||||
|
@ -33,11 +31,7 @@ public class JavaScriptModulesConfig {
|
|||
return mModules;
|
||||
}
|
||||
|
||||
/*package*/ String moduleDescriptions() {
|
||||
JsonFactory jsonFactory = new JsonFactory();
|
||||
StringWriter writer = new StringWriter();
|
||||
try {
|
||||
JsonGenerator jg = jsonFactory.createGenerator(writer);
|
||||
/*package*/ void writeModuleDescriptions(JsonGenerator jg) throws IOException {
|
||||
jg.writeStartObject();
|
||||
for (JavaScriptModuleRegistration registration : mModules) {
|
||||
jg.writeObjectFieldStart(registration.getName());
|
||||
|
@ -45,11 +39,6 @@ public class JavaScriptModulesConfig {
|
|||
jg.writeEndObject();
|
||||
}
|
||||
jg.writeEndObject();
|
||||
jg.close();
|
||||
} catch (IOException ioe) {
|
||||
throw new RuntimeException("Unable to serialize JavaScript module declaration", ioe);
|
||||
}
|
||||
return writer.getBuffer().toString();
|
||||
}
|
||||
|
||||
private void appendJSModuleToJSONObject(
|
||||
|
|
|
@ -10,17 +10,16 @@
|
|||
package com.facebook.react.bridge;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.facebook.react.common.MapBuilder;
|
||||
import com.facebook.infer.annotation.Assertions;
|
||||
import com.facebook.systrace.Systrace;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonFactory;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
|
||||
/**
|
||||
|
@ -28,18 +27,15 @@ import com.fasterxml.jackson.core.JsonGenerator;
|
|||
*/
|
||||
public class NativeModuleRegistry {
|
||||
|
||||
private final ArrayList<ModuleDefinition> mModuleTable;
|
||||
private final List<ModuleDefinition> mModuleTable;
|
||||
private final Map<Class<? extends NativeModule>, NativeModule> mModuleInstances;
|
||||
private final String mModuleDescriptions;
|
||||
private final ArrayList<OnBatchCompleteListener> mBatchCompleteListenerModules;
|
||||
|
||||
private NativeModuleRegistry(
|
||||
ArrayList<ModuleDefinition> moduleTable,
|
||||
Map<Class<? extends NativeModule>, NativeModule> moduleInstances,
|
||||
String moduleDescriptions) {
|
||||
List<ModuleDefinition> moduleTable,
|
||||
Map<Class<? extends NativeModule>, NativeModule> moduleInstances) {
|
||||
mModuleTable = moduleTable;
|
||||
mModuleInstances = moduleInstances;
|
||||
mModuleDescriptions = moduleDescriptions;
|
||||
|
||||
mBatchCompleteListenerModules = new ArrayList<OnBatchCompleteListener>(mModuleTable.size());
|
||||
for (int i = 0; i < mModuleTable.size(); i++) {
|
||||
|
@ -62,8 +58,29 @@ public class NativeModuleRegistry {
|
|||
definition.call(catalystInstance, methodId, parameters);
|
||||
}
|
||||
|
||||
/* package */ String moduleDescriptions() {
|
||||
return mModuleDescriptions;
|
||||
/* package */ void writeModuleDescriptions(JsonGenerator jg) throws IOException {
|
||||
Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "CreateJSON");
|
||||
try {
|
||||
jg.writeStartObject();
|
||||
for (ModuleDefinition moduleDef : mModuleTable) {
|
||||
jg.writeObjectFieldStart(moduleDef.name);
|
||||
jg.writeNumberField("moduleID", moduleDef.id);
|
||||
jg.writeObjectFieldStart("methods");
|
||||
for (int i = 0; i < moduleDef.methods.size(); i++) {
|
||||
MethodRegistration method = moduleDef.methods.get(i);
|
||||
jg.writeObjectFieldStart(method.name);
|
||||
jg.writeNumberField("methodID", i);
|
||||
jg.writeStringField("type", method.method.getType());
|
||||
jg.writeEndObject();
|
||||
}
|
||||
jg.writeEndObject();
|
||||
moduleDef.target.writeConstantsField(jg, "constants");
|
||||
jg.writeEndObject();
|
||||
}
|
||||
jg.writeEndObject();
|
||||
} finally {
|
||||
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
|
||||
}
|
||||
}
|
||||
|
||||
/* package */ void notifyCatalystInstanceDestroy() {
|
||||
|
@ -174,45 +191,16 @@ public class NativeModuleRegistry {
|
|||
}
|
||||
|
||||
public NativeModuleRegistry build() {
|
||||
Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "CreateJSON");
|
||||
ArrayList<ModuleDefinition> moduleTable = new ArrayList<>();
|
||||
Map<Class<? extends NativeModule>, NativeModule> moduleInstances = MapBuilder.newHashMap();
|
||||
String moduleDefinitionJson;
|
||||
try {
|
||||
JsonFactory jsonFactory = new JsonFactory();
|
||||
StringWriter writer = new StringWriter();
|
||||
try {
|
||||
JsonGenerator jg = jsonFactory.createGenerator(writer);
|
||||
jg.writeStartObject();
|
||||
List<ModuleDefinition> moduleTable = new ArrayList<>();
|
||||
Map<Class<? extends NativeModule>, NativeModule> moduleInstances = new HashMap<>();
|
||||
|
||||
int idx = 0;
|
||||
for (NativeModule module : mModules.values()) {
|
||||
ModuleDefinition moduleDef = new ModuleDefinition(idx++, module.getName(), module);
|
||||
moduleTable.add(moduleDef);
|
||||
moduleInstances.put(module.getClass(), module);
|
||||
jg.writeObjectFieldStart(moduleDef.name);
|
||||
jg.writeNumberField("moduleID", moduleDef.id);
|
||||
jg.writeObjectFieldStart("methods");
|
||||
for (int i = 0; i < moduleDef.methods.size(); i++) {
|
||||
MethodRegistration method = moduleDef.methods.get(i);
|
||||
jg.writeObjectFieldStart(method.name);
|
||||
jg.writeNumberField("methodID", i);
|
||||
jg.writeStringField("type", method.method.getType());
|
||||
jg.writeEndObject();
|
||||
}
|
||||
jg.writeEndObject();
|
||||
moduleDef.target.writeConstantsField(jg, "constants");
|
||||
jg.writeEndObject();
|
||||
}
|
||||
jg.writeEndObject();
|
||||
jg.close();
|
||||
} catch (IOException ioe) {
|
||||
throw new RuntimeException("Unable to serialize Java module configuration", ioe);
|
||||
}
|
||||
moduleDefinitionJson = writer.getBuffer().toString();
|
||||
} finally {
|
||||
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
|
||||
}
|
||||
return new NativeModuleRegistry(moduleTable, moduleInstances, moduleDefinitionJson);
|
||||
return new NativeModuleRegistry(moduleTable, moduleInstances);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue