diff --git a/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricContext.java b/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricContext.java index 3a987f02..519e56e6 100644 --- a/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricContext.java +++ b/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricContext.java @@ -14,20 +14,23 @@ public class DoricContext { private static AtomicInteger sCounter = new AtomicInteger(); private final String mContextId; - private DoricContext(String contextId) { + DoricContext(String contextId) { this.mContextId = contextId; } public static DoricContext createContext(String script, String alias) { - String contextId = String.valueOf(sCounter.incrementAndGet()); - DoricDriver.getInstance().createContext(contextId, script, alias); - return new DoricContext(contextId); + return DoricDriver.getInstance().createContext(script, alias); } public DoricSettableFuture callEntity(String methodName, Object... args) { return DoricDriver.getInstance().invokeContextMethod(mContextId, methodName, args); } + + public String getContextId() { + return mContextId; + } + public void teardown() { DoricDriver.getInstance().destroyContext(mContextId); } diff --git a/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricDriver.java b/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricDriver.java index 44ae5feb..1de2f2d2 100644 --- a/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricDriver.java +++ b/Android/doric/src/main/java/com/github/pengfeizhou/doric/DoricDriver.java @@ -4,6 +4,10 @@ import com.github.pengfeizhou.doric.engine.DoricJSEngine; import com.github.pengfeizhou.doric.utils.DoricSettableFuture; import com.github.pengfeizhou.jscore.JSDecoder; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; + /** * @Description: Doric * @Author: pengfei.zhou @@ -11,6 +15,8 @@ import com.github.pengfeizhou.jscore.JSDecoder; */ public class DoricDriver { private final DoricJSEngine doricJSEngine; + private final AtomicInteger counter = new AtomicInteger(); + private final Map doricContextMap = new ConcurrentHashMap<>(); public DoricSettableFuture invokeContextMethod(final String contextId, final String method, final Object... args) { return doricJSEngine.invokeContextEntityMethod(contextId, method, args); @@ -28,13 +34,21 @@ public class DoricDriver { return Inner.sInstance; } - public void createContext(final String contextId, final String script, final String source) { + DoricContext createContext(final String script, final String source) { + String contextId = String.valueOf(counter.incrementAndGet()); doricJSEngine.prepareContext(contextId, script, source); + DoricContext doricContext = new DoricContext(contextId); + doricContextMap.put(contextId, doricContext); + return doricContext; } - public void destroyContext(String contextId) { + void destroyContext(String contextId) { + doricContextMap.remove(contextId); doricJSEngine.destroyContext(contextId); } + public DoricContext getContext(String contextId) { + return doricContextMap.get(contextId); + } } diff --git a/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/DoricBridgeExtension.java b/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/DoricBridgeExtension.java index af7bccb7..73bff8a1 100644 --- a/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/DoricBridgeExtension.java +++ b/Android/doric/src/main/java/com/github/pengfeizhou/doric/extension/DoricBridgeExtension.java @@ -1,5 +1,7 @@ package com.github.pengfeizhou.doric.extension; +import com.github.pengfeizhou.doric.DoricContext; +import com.github.pengfeizhou.doric.DoricDriver; import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JavaValue; @@ -14,6 +16,8 @@ public class DoricBridgeExtension { } public JavaValue callNative(String contextId, String module, String method, String callbackId, JSDecoder jsDecoder) { + DoricContext doricContext = DoricDriver.getInstance().getContext(contextId); + return null; }