add doric context map

This commit is contained in:
pengfei.zhou 2019-07-19 11:41:09 +08:00
parent ac8d111f7b
commit 15b5bebcf0
3 changed files with 27 additions and 6 deletions

View File

@ -14,20 +14,23 @@ public class DoricContext {
private static AtomicInteger sCounter = new AtomicInteger(); private static AtomicInteger sCounter = new AtomicInteger();
private final String mContextId; private final String mContextId;
private DoricContext(String contextId) { DoricContext(String contextId) {
this.mContextId = contextId; this.mContextId = contextId;
} }
public static DoricContext createContext(String script, String alias) { public static DoricContext createContext(String script, String alias) {
String contextId = String.valueOf(sCounter.incrementAndGet()); return DoricDriver.getInstance().createContext(script, alias);
DoricDriver.getInstance().createContext(contextId, script, alias);
return new DoricContext(contextId);
} }
public DoricSettableFuture<JSDecoder> callEntity(String methodName, Object... args) { public DoricSettableFuture<JSDecoder> callEntity(String methodName, Object... args) {
return DoricDriver.getInstance().invokeContextMethod(mContextId, methodName, args); return DoricDriver.getInstance().invokeContextMethod(mContextId, methodName, args);
} }
public String getContextId() {
return mContextId;
}
public void teardown() { public void teardown() {
DoricDriver.getInstance().destroyContext(mContextId); DoricDriver.getInstance().destroyContext(mContextId);
} }

View File

@ -4,6 +4,10 @@ import com.github.pengfeizhou.doric.engine.DoricJSEngine;
import com.github.pengfeizhou.doric.utils.DoricSettableFuture; import com.github.pengfeizhou.doric.utils.DoricSettableFuture;
import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSDecoder;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
/** /**
* @Description: Doric * @Description: Doric
* @Author: pengfei.zhou * @Author: pengfei.zhou
@ -11,6 +15,8 @@ import com.github.pengfeizhou.jscore.JSDecoder;
*/ */
public class DoricDriver { public class DoricDriver {
private final DoricJSEngine doricJSEngine; private final DoricJSEngine doricJSEngine;
private final AtomicInteger counter = new AtomicInteger();
private final Map<String, DoricContext> doricContextMap = new ConcurrentHashMap<>();
public DoricSettableFuture<JSDecoder> invokeContextMethod(final String contextId, final String method, final Object... args) { public DoricSettableFuture<JSDecoder> invokeContextMethod(final String contextId, final String method, final Object... args) {
return doricJSEngine.invokeContextEntityMethod(contextId, method, args); return doricJSEngine.invokeContextEntityMethod(contextId, method, args);
@ -28,13 +34,21 @@ public class DoricDriver {
return Inner.sInstance; 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); 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); doricJSEngine.destroyContext(contextId);
} }
public DoricContext getContext(String contextId) {
return doricContextMap.get(contextId);
}
} }

View File

@ -1,5 +1,7 @@
package com.github.pengfeizhou.doric.extension; 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.JSDecoder;
import com.github.pengfeizhou.jscore.JavaValue; 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) { public JavaValue callNative(String contextId, String module, String method, String callbackId, JSDecoder jsDecoder) {
DoricContext doricContext = DoricDriver.getInstance().getContext(contextId);
return null; return null;
} }