Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
Add register cache feature
Browse files Browse the repository at this point in the history
  • Loading branch information
lucky-chen authored and Darin726 committed Feb 20, 2019
1 parent 7659a59 commit c10333c
Show file tree
Hide file tree
Showing 13 changed files with 293 additions and 8 deletions.
Binary file modified android/sdk/libs/armeabi-v7a/libweexcore.so
Binary file not shown.
Binary file modified android/sdk/libs/armeabi-v7a/libweexjss.so
Binary file not shown.
Binary file modified android/sdk/libs/armeabi/libweexcore.so
Binary file not shown.
Binary file modified android/sdk/libs/armeabi/libweexjss.so
Binary file not shown.
Binary file modified android/sdk/libs/x86/libweexcore.so
Binary file not shown.
Binary file modified android/sdk/libs/x86/libweexjss.so
Binary file not shown.
7 changes: 6 additions & 1 deletion android/sdk/src/main/java/com/taobao/weex/WXSDKEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
import com.taobao.weex.utils.WXLogUtils;
import com.taobao.weex.utils.WXSoInstallMgrSdk;
import com.taobao.weex.utils.batch.BatchOperationHelper;
import com.taobao.weex.utils.cache.RegisterCache;

import java.io.Serializable;
import java.util.HashMap;
Expand Down Expand Up @@ -377,7 +378,11 @@ private static void register() {
} catch (WXException e) {
WXLogUtils.e("[WXSDKEngine] register:", e);
}
AutoScanConfigRegister.doScanConfig();

if(RegisterCache.getInstance().enableAutoScan()) {
AutoScanConfigRegister.doScanConfig();
}

batchHelper.flush();
}

Expand Down
3 changes: 3 additions & 0 deletions android/sdk/src/main/java/com/taobao/weex/WXSDKInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@
import com.taobao.weex.utils.WXReflectionUtils;
import com.taobao.weex.utils.WXUtils;
import com.taobao.weex.utils.WXViewUtils;
import com.taobao.weex.utils.cache.RegisterCache;

import java.io.Serializable;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
Expand Down Expand Up @@ -459,6 +461,7 @@ public void removeOnInstanceVisibleListener(OnInstanceVisibleListener l){
}

public void init(Context context) {
RegisterCache.getInstance().idle(true);
mContext = context;
mContainerInfo = new HashMap<>(4);
mNativeInvokeHelper = new NativeInvokeHelper(mInstanceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private void execute(@Nullable final Runnable runnable) {
mExecutorService = Executors.newSingleThreadExecutor();
}

if(runnable != null && !mExecutorService.isShutdown()) {
if(runnable != null && !mExecutorService.isShutdown() && !mExecutorService.isTerminated()) {
mExecutorService.execute(WXThread.secure(runnable));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2201,7 +2201,7 @@ private void invokeRegisterModules(Map<String, Object> modules, List<Map<String,
String module = iter.next();
if (module != null) {
WXModuleManager.resetModuleState(module, true);
WXLogUtils.e("[WXBridgeManager]invokeRegisterModules METHOD_REGISTER_MODULES success module:" + module);
//WXLogUtils.e("[WXBridgeManager]invokeRegisterModules METHOD_REGISTER_MODULES success module:" + module);
}
}
} catch (Throwable e) {
Expand All @@ -2226,7 +2226,7 @@ private void invokeRegisterComponents(List<Map<String, Object>> components, List
throw new RuntimeException("Fail receiver should not use source.");
}
if (!isJSFrameworkInit()) {
WXLogUtils.e("[WXBridgeManager] invokeRegisterComponents: framework.js uninitialized.");
//WXLogUtils.e("[WXBridgeManager] invokeRegisterComponents: framework.js uninitialized.");

for (Map<String, Object> comp : components) {
failReceiver.add(comp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.taobao.weex.ui.module.WXTimerModule;
import com.taobao.weex.utils.WXExceptionUtils;
import com.taobao.weex.utils.WXLogUtils;
import com.taobao.weex.utils.cache.RegisterCache;

import java.io.Serializable;
import java.util.HashMap;
Expand Down Expand Up @@ -66,6 +67,60 @@ public class WXModuleManager {
*/
private static Map<String, Map<String, WXModule>> sInstanceModuleMap = new ConcurrentHashMap<>();


public static boolean registerModule(Map<String, RegisterCache.ModuleCache> moduleCacheMap) {
if (moduleCacheMap.isEmpty())
return true;

final Iterator<Entry<String, RegisterCache.ModuleCache>> iterator = moduleCacheMap.entrySet().iterator();
WXBridgeManager.getInstance()
.post(new Runnable() {
@Override
public void run() {
Map<String, Object> modules = new HashMap<>();

while (iterator.hasNext()) {
Entry<String, RegisterCache.ModuleCache> next = iterator.next();
RegisterCache.ModuleCache value = next.getValue();
String moduleName = value.name;
if (TextUtils.equals(moduleName, WXDomModule.WXDOM)) {
WXLogUtils.e("Cannot registered module with name 'dom'.");
continue;
}

if (sModuleFactoryMap != null && sModuleFactoryMap.containsKey(moduleName)) {
WXLogUtils.w("WXComponentRegistry Duplicate the Module name: " + moduleName);
}
ModuleFactory factory = value.factory;
try {
registerNativeModule(moduleName, factory);
} catch (WXException e) {
WXLogUtils.e("registerNativeModule" + e);
}

if (value.global) {
try {
WXModule wxModule = factory.buildInstance();
wxModule.setModuleName(moduleName);
sGlobalModuleMap.put(moduleName, wxModule);
} catch (Exception e) {
WXLogUtils.e(moduleName + " class must have a default constructor without params. ", e);
}
}

try {
sModuleFactoryMap.put(moduleName, new ModuleFactoryImpl(factory));
} catch (Throwable e) {

}
modules.put(moduleName, factory.getMethods());
}
WXSDKManager.getInstance().registerModules(modules);
}
});
return true;
}

/**
* Register module to JavaScript and Android
*/
Expand All @@ -79,10 +134,8 @@ public static boolean registerModule(final String moduleName, final ModuleFactor
return false;
}

try {
sModuleFactoryMap.put(moduleName, new ModuleFactoryImpl(factory));
} catch (Throwable e) {

if(RegisterCache.getInstance().cacheModule(moduleName,factory,global)) {
return true;
}

//execute task in js thread to make sure register order is same as the order invoke register method.
Expand Down Expand Up @@ -110,6 +163,12 @@ public void run() {
}

registerJSModule(moduleName, factory);

try {
sModuleFactoryMap.put(moduleName, new ModuleFactoryImpl(factory));
} catch (Throwable e) {

}
}
});
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import com.taobao.weex.common.WXException;
import com.taobao.weex.ui.config.AutoScanConfigRegister;
import com.taobao.weex.utils.WXLogUtils;
import com.taobao.weex.utils.cache.RegisterCache;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -41,11 +43,46 @@ public class WXComponentRegistry {
private static Map<String, IFComponentHolder> sTypeComponentMap = new ConcurrentHashMap<>();
private static ArrayList<Map<String, Object>> sComponentInfos=new ArrayList<>();

public static synchronized boolean registerComponent(Map<String, RegisterCache.ComponentCache> componentCacheMap) {
if (componentCacheMap.isEmpty())
return true;
final Iterator<Map.Entry<String, RegisterCache.ComponentCache>> iterator = componentCacheMap.entrySet().iterator();
WXBridgeManager.getInstance().post(new Runnable() {
@Override
public void run() {
ArrayList<Map<String, Object>> coms = new ArrayList<>();
while (iterator.hasNext()) {
Map.Entry<String, RegisterCache.ComponentCache> next = iterator.next();
try {
RegisterCache.ComponentCache value = next.getValue();
Map<String, Object> registerInfo = value.componentInfo;
if (registerInfo == null) {
registerInfo = new HashMap<>();
}
registerInfo.put("type", value.type);
registerInfo.put("methods", value.holder.getMethods());
registerNativeComponent(value.type, value.holder);
sComponentInfos.add(registerInfo);
coms.add(registerInfo);
} catch (WXException e) {
e.printStackTrace();
}
}
WXSDKManager.getInstance().registerComponents(coms);
}
});
return true;
}

public static synchronized boolean registerComponent(final String type, final IFComponentHolder holder, final Map<String, Object> componentInfo) throws WXException {
if (holder == null || TextUtils.isEmpty(type)) {
return false;
}

if(RegisterCache.getInstance().cacheComponent(type,holder,componentInfo)) {
return true;
}

//execute task in js thread to make sure register order is same as the order invoke register method.
WXBridgeManager.getInstance()
.post(new Runnable() {
Expand Down
Loading

0 comments on commit c10333c

Please sign in to comment.