Generate module definition on demand

Reviewed By: astreet

Differential Revision: D2707977

fb-gh-sync-id: fb2baa464a23df82e8b48a91a84c98370517d311
This commit is contained in:
Alexander Blom 2015-12-02 05:30:33 -08:00 committed by facebook-github-bot-3
parent c25c98c00c
commit 30a5eb51f8
3 changed files with 44 additions and 68 deletions

View File

@ -361,16 +361,15 @@ public class CatalystInstanceImpl implements CatalystInstance {
private String buildModulesConfigJSONProperty( private String buildModulesConfigJSONProperty(
NativeModuleRegistry nativeModuleRegistry, NativeModuleRegistry nativeModuleRegistry,
JavaScriptModulesConfig jsModulesConfig) { JavaScriptModulesConfig jsModulesConfig) {
// TODO(5300733): Serialize config using single json generator
JsonFactory jsonFactory = new JsonFactory(); JsonFactory jsonFactory = new JsonFactory();
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
try { try {
JsonGenerator jg = jsonFactory.createGenerator(writer); JsonGenerator jg = jsonFactory.createGenerator(writer);
jg.writeStartObject(); jg.writeStartObject();
jg.writeFieldName("remoteModuleConfig"); jg.writeFieldName("remoteModuleConfig");
jg.writeRawValue(nativeModuleRegistry.moduleDescriptions()); nativeModuleRegistry.writeModuleDescriptions(jg);
jg.writeFieldName("localModulesConfig"); jg.writeFieldName("localModulesConfig");
jg.writeRawValue(jsModulesConfig.moduleDescriptions()); jsModulesConfig.writeModuleDescriptions(jg);
jg.writeEndObject(); jg.writeEndObject();
jg.close(); jg.close();
} catch (IOException ioe) { } catch (IOException ioe) {

View File

@ -10,12 +10,10 @@
package com.facebook.react.bridge; package com.facebook.react.bridge;
import java.io.IOException; import java.io.IOException;
import java.io.StringWriter;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
/** /**
@ -33,23 +31,14 @@ public class JavaScriptModulesConfig {
return mModules; return mModules;
} }
/*package*/ String moduleDescriptions() { /*package*/ void writeModuleDescriptions(JsonGenerator jg) throws IOException {
JsonFactory jsonFactory = new JsonFactory(); jg.writeStartObject();
StringWriter writer = new StringWriter(); for (JavaScriptModuleRegistration registration : mModules) {
try { jg.writeObjectFieldStart(registration.getName());
JsonGenerator jg = jsonFactory.createGenerator(writer); appendJSModuleToJSONObject(jg, registration);
jg.writeStartObject();
for (JavaScriptModuleRegistration registration : mModules) {
jg.writeObjectFieldStart(registration.getName());
appendJSModuleToJSONObject(jg, registration);
jg.writeEndObject();
}
jg.writeEndObject(); jg.writeEndObject();
jg.close();
} catch (IOException ioe) {
throw new RuntimeException("Unable to serialize JavaScript module declaration", ioe);
} }
return writer.getBuffer().toString(); jg.writeEndObject();
} }
private void appendJSModuleToJSONObject( private void appendJSModuleToJSONObject(

View File

@ -10,17 +10,16 @@
package com.facebook.react.bridge; package com.facebook.react.bridge;
import java.io.IOException; import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import com.facebook.react.common.MapBuilder; import com.facebook.react.common.MapBuilder;
import com.facebook.infer.annotation.Assertions; import com.facebook.infer.annotation.Assertions;
import com.facebook.systrace.Systrace; import com.facebook.systrace.Systrace;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
/** /**
@ -28,18 +27,15 @@ import com.fasterxml.jackson.core.JsonGenerator;
*/ */
public class NativeModuleRegistry { public class NativeModuleRegistry {
private final ArrayList<ModuleDefinition> mModuleTable; private final List<ModuleDefinition> mModuleTable;
private final Map<Class<? extends NativeModule>, NativeModule> mModuleInstances; private final Map<Class<? extends NativeModule>, NativeModule> mModuleInstances;
private final String mModuleDescriptions;
private final ArrayList<OnBatchCompleteListener> mBatchCompleteListenerModules; private final ArrayList<OnBatchCompleteListener> mBatchCompleteListenerModules;
private NativeModuleRegistry( private NativeModuleRegistry(
ArrayList<ModuleDefinition> moduleTable, List<ModuleDefinition> moduleTable,
Map<Class<? extends NativeModule>, NativeModule> moduleInstances, Map<Class<? extends NativeModule>, NativeModule> moduleInstances) {
String moduleDescriptions) {
mModuleTable = moduleTable; mModuleTable = moduleTable;
mModuleInstances = moduleInstances; mModuleInstances = moduleInstances;
mModuleDescriptions = moduleDescriptions;
mBatchCompleteListenerModules = new ArrayList<OnBatchCompleteListener>(mModuleTable.size()); mBatchCompleteListenerModules = new ArrayList<OnBatchCompleteListener>(mModuleTable.size());
for (int i = 0; i < mModuleTable.size(); i++) { for (int i = 0; i < mModuleTable.size(); i++) {
@ -62,8 +58,29 @@ public class NativeModuleRegistry {
definition.call(catalystInstance, methodId, parameters); definition.call(catalystInstance, methodId, parameters);
} }
/* package */ String moduleDescriptions() { /* package */ void writeModuleDescriptions(JsonGenerator jg) throws IOException {
return mModuleDescriptions; 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() { /* package */ void notifyCatalystInstanceDestroy() {
@ -174,45 +191,16 @@ public class NativeModuleRegistry {
} }
public NativeModuleRegistry build() { public NativeModuleRegistry build() {
Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "CreateJSON"); List<ModuleDefinition> moduleTable = new ArrayList<>();
ArrayList<ModuleDefinition> moduleTable = new ArrayList<>(); Map<Class<? extends NativeModule>, NativeModule> moduleInstances = new HashMap<>();
Map<Class<? extends NativeModule>, NativeModule> moduleInstances = MapBuilder.newHashMap();
String moduleDefinitionJson; int idx = 0;
try { for (NativeModule module : mModules.values()) {
JsonFactory jsonFactory = new JsonFactory(); ModuleDefinition moduleDef = new ModuleDefinition(idx++, module.getName(), module);
StringWriter writer = new StringWriter(); moduleTable.add(moduleDef);
try { moduleInstances.put(module.getClass(), module);
JsonGenerator jg = jsonFactory.createGenerator(writer);
jg.writeStartObject();
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);
} }
} }
} }