feat:add set environment value api

This commit is contained in:
pengfei.zhou
2021-07-07 17:30:08 +08:00
committed by osborn
parent 0c10b513b9
commit e6595d5c51
18 changed files with 157 additions and 30 deletions

View File

@@ -73,6 +73,7 @@ public class DoricRegistry {
private static final Map<String, String> bundles = new ConcurrentHashMap<>();
private static final Set<DoricLibrary> doricLibraries = new HashSet<>();
private static final List<WeakReference<DoricRegistry>> registries = new ArrayList<>();
private static final Map<String, Object> envMap = new ConcurrentHashMap<>();
private final Map<String, DoricMetaInfo<DoricJavaPlugin>> pluginInfoMap = new HashMap<>();
private final Map<String, DoricMetaInfo<ViewNode>> nodeInfoMap = new HashMap<>();
@@ -98,6 +99,16 @@ public class DoricRegistry {
}
}
public static void setEnvironmentValue(Map<String, Object> value) {
envMap.putAll(value);
for (WeakReference<DoricRegistry> registryWeakReference : registries) {
DoricRegistry registry = registryWeakReference.get();
if (registry != null) {
registry.innerSetEnvironmentValue(value);
}
}
}
private final WeakReference<DoricJSEngine> doricJSEngineWeakReference;
public DoricRegistry(DoricJSEngine doricJSEngine) {
@@ -136,6 +147,7 @@ public class DoricRegistry {
this.registerViewNode(SwitchNode.class);
this.registerViewNode(FlexNode.class);
initRegistry(this);
doricJSEngine.setEnvironmentValue(envMap);
registries.add(new WeakReference<>(this));
}
@@ -169,12 +181,12 @@ public class DoricRegistry {
return bundles.get(name);
}
public void setEnvironmentVariable(String key, Object val) {
private void innerSetEnvironmentValue(Map<String, Object> value) {
DoricJSEngine doricJSEngine = doricJSEngineWeakReference.get();
if (doricJSEngine == null) {
return;
}
doricJSEngine.setEnvironmentVariable(key, val);
doricJSEngine.setEnvironmentValue(value);
}
public void registerMonitor(IDoricMonitor monitor) {

View File

@@ -54,17 +54,17 @@ import pub.doric.utils.DoricUtils;
* @CreateDate: 2019-07-18
*/
public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.TimerCallback, IDoricMonitor {
private final HandlerThread handlerThread;
private final Handler mJSHandler;
private final DoricBridgeExtension mDoricBridgeExtension = new DoricBridgeExtension();
protected IDoricJSE mDoricJSE;
private final DoricTimerExtension mTimerExtension;
private final DoricRegistry mDoricRegistry = new DoricRegistry(this);
private final Map<String, Object> mEnvironmentMap = new ConcurrentHashMap<>();
private boolean initialized = false;
private final DoricRegistry mDoricRegistry;
private final Map<String, Object> mEnvironmentMap = new ConcurrentHashMap<>();
public DoricJSEngine() {
mDoricRegistry = new DoricRegistry(this);
handlerThread = new HandlerThread(this.getClass().getSimpleName());
handlerThread.start();
Looper looper = handlerThread.getLooper();
@@ -90,8 +90,8 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
mDoricJSE = new DoricNativeJSExecutor();
}
public void setEnvironmentVariable(String key, Object v) {
mEnvironmentMap.put(key, v);
public void setEnvironmentValue(Map<String, Object> values) {
mEnvironmentMap.putAll(values);
if (initialized) {
final JSONBuilder jsonBuilder = new JSONBuilder();
for (String k : mEnvironmentMap.keySet()) {
@@ -104,7 +104,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
new JavaValue(jsonBuilder.toJSONObject()));
}
});
for(DoricContext context:DoricContextManager.aliveContexts()){
for (DoricContext context : DoricContextManager.aliveContexts()) {
context.onEnvChanged();
}
}