Skip to content

Commit f37651d

Browse files
author
blagoev
committed
cache module file paths
1 parent f4d437e commit f37651d

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

src/src/com/tns/Require.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.File;
44
import java.io.IOException;
5+
import java.util.HashMap;
56

67
import org.json.JSONException;
78
import org.json.JSONObject;
@@ -22,7 +23,8 @@ public class Require
2223
private static final String ModuleContent_Part3 = "\";" + "function require(moduleName){ return __global.require(moduleName, __filename); }" + "module.filename = __filename; this.__extends = __global.__extends; \n";
2324
private static final String ModuleContent_Part4 ="\n return module.exports; \n})";
2425
private static final StringBuffer ModuleContent = new StringBuffer(65536);
25-
26+
private static HashMap<String, String> modulePathCache = new HashMap<String, String>();
27+
2628
public static void init(Context context)
2729
{
2830
if (initialized)
@@ -103,9 +105,16 @@ public static String getModuleContent(String modulePath)
103105
return "";
104106
}
105107
}
106-
108+
107109
public static String getModulePath(String moduleName, String callingModuleName)
108110
{
111+
String cachedPath = modulePathCache.get(moduleName);
112+
if (cachedPath != null)
113+
{
114+
return cachedPath;
115+
}
116+
117+
109118
// This method is called my the NativeScriptRuntime.cpp RequireCallback
110119
// method.
111120
// The currentModuleName is the fully-qualified path of the previously
@@ -136,7 +145,9 @@ public static String getModulePath(String moduleName, String callingModuleName)
136145
}
137146
else
138147
{
139-
return file.getPath();
148+
String result = file.getPath();
149+
modulePathCache.put(moduleName, result);
150+
return result;
140151
}
141152
}
142153

@@ -164,6 +175,7 @@ private static boolean isFileExternal(File source, File target)
164175

165176
private static File findModuleFile(String moduleName, String currentDirectory)
166177
{
178+
167179
File directory = null;
168180
File jsFile = null;
169181
boolean isJSFile = moduleName.endsWith(".js");

0 commit comments

Comments
 (0)