Skip to content

Commit

Permalink
Generate module definition on demand
Browse files Browse the repository at this point in the history
Reviewed By: astreet

Differential Revision: D2707977

fb-gh-sync-id: fb2baa464a23df82e8b48a91a84c98370517d311
  • Loading branch information
lexs authored and facebook-github-bot-3 committed Dec 2, 2015
1 parent c25c98c commit 30a5eb5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -361,16 +361,15 @@ public void stopProfiler(String title, String filename) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -33,23 +31,14 @@ private JavaScriptModulesConfig(List<JavaScriptModuleRegistration> modules) {
return mModules;
}

/*package*/ String moduleDescriptions() {
JsonFactory jsonFactory = new JsonFactory();
StringWriter writer = new StringWriter();
try {
JsonGenerator jg = jsonFactory.createGenerator(writer);
jg.writeStartObject();
for (JavaScriptModuleRegistration registration : mModules) {
jg.writeObjectFieldStart(registration.getName());
appendJSModuleToJSONObject(jg, registration);
jg.writeEndObject();
}
/*package*/ void writeModuleDescriptions(JsonGenerator jg) throws IOException {
jg.writeStartObject();
for (JavaScriptModuleRegistration registration : mModules) {
jg.writeObjectFieldStart(registration.getName());
appendJSModuleToJSONObject(jg, registration);
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,32 @@
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;

/**
* A set of Java APIs to expose to a particular JavaScript instance.
*/
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++) {
Expand All @@ -62,8 +58,29 @@ private 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() {
Expand Down Expand Up @@ -174,45 +191,16 @@ public Builder add(NativeModule module) {
}

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();
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);
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);
}
return new NativeModuleRegistry(moduleTable, moduleInstances, moduleDefinitionJson);
return new NativeModuleRegistry(moduleTable, moduleInstances);
}
}
}

0 comments on commit 30a5eb5

Please sign in to comment.