abstract Driver

This commit is contained in:
pengfei.zhou 2019-07-19 16:52:49 +08:00
parent 0a4bc77b15
commit 36b478bfb3
6 changed files with 103 additions and 25 deletions

View File

@ -22,14 +22,14 @@ public class DoricContext {
}
public static DoricContext createContext(String script, String alias) {
return DoricDriver.getInstance().createContext(script, alias);
return DoricContextManager.getInstance().createContext(script, alias);
}
public AsyncResult<JSDecoder> callEntity(String methodName, Object... args) {
return getDriver().invokeContextEntityMethod(mContextId, methodName, args);
}
public DoricDriver getDriver() {
public IDoricDriver getDriver() {
return DoricDriver.getInstance();
}
@ -39,7 +39,7 @@ public class DoricContext {
}
public void teardown() {
DoricDriver.getInstance().destroyContext(mContextId).setCallback(new AsyncResult.Callback<Boolean>() {
DoricContextManager.getInstance().destroyContext(this).setCallback(new AsyncResult.Callback<Boolean>() {
@Override
public void onResult(Boolean result) {

View File

@ -0,0 +1,66 @@
package com.github.pengfeizhou.doric;
import com.github.pengfeizhou.doric.async.AsyncCall;
import com.github.pengfeizhou.doric.async.AsyncResult;
import com.github.pengfeizhou.doric.utils.DoricLog;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @Description: com.github.pengfeizhou.doric
* @Author: pengfei.zhou
* @CreateDate: 2019-07-19
*/
public class DoricContextManager {
private final AtomicInteger counter = new AtomicInteger();
private final Map<String, DoricContext> doricContextMap = new ConcurrentHashMap<>();
private static class Inner {
private static final DoricContextManager sInstance = new DoricContextManager();
}
private DoricContextManager() {
}
public static DoricContextManager getInstance() {
return Inner.sInstance;
}
DoricContext createContext(final String script, final String source) {
final String contextId = String.valueOf(counter.incrementAndGet());
final DoricContext doricContext = new DoricContext(contextId);
doricContextMap.put(contextId, doricContext);
doricContext.getDriver().createContext(contextId, script, source);
return doricContext;
}
AsyncResult<Boolean> destroyContext(final DoricContext context) {
final AsyncResult<Boolean> result = new AsyncResult<>();
context.getDriver().destroyContext(context.getContextId()).setCallback(new AsyncResult.Callback<Boolean>() {
@Override
public void onResult(Boolean b) {
result.setResult(b);
}
@Override
public void onError(Throwable t) {
result.setError(t);
}
@Override
public void onFinish() {
doricContextMap.remove(context.getContextId());
}
});
return result;
}
public static DoricContext getContext(String contextId) {
return getInstance().doricContextMap.get(contextId);
}
}

View File

@ -10,22 +10,17 @@ import com.github.pengfeizhou.doric.utils.DoricConstant;
import com.github.pengfeizhou.doric.utils.DoricLog;
import com.github.pengfeizhou.jscore.JSDecoder;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @Description: Doric
* @Author: pengfei.zhou
* @CreateDate: 2019-07-18
*/
public class DoricDriver {
public class DoricDriver implements IDoricDriver {
private final DoricJSEngine doricJSEngine;
private final AtomicInteger counter = new AtomicInteger();
private final Map<String, DoricContext> doricContextMap = new ConcurrentHashMap<>();
private final ExecutorService mBridgeExecutor;
private final Handler mUIHandler;
private final Handler mJSHandler;
@ -81,11 +76,8 @@ public class DoricDriver {
return Inner.sInstance;
}
DoricContext createContext(final String script, final String source) {
final String contextId = String.valueOf(counter.incrementAndGet());
DoricContext doricContext = new DoricContext(contextId);
doricContextMap.put(contextId, doricContext);
AsyncCall.ensureRunInHandler(mJSHandler, new Callable<Boolean>() {
public AsyncResult<Boolean> createContext(final String contextId, final String script, final String source) {
return AsyncCall.ensureRunInHandler(mJSHandler, new Callable<Boolean>() {
@Override
public Boolean call() {
try {
@ -97,17 +89,15 @@ public class DoricDriver {
}
}
});
return doricContext;
}
AsyncResult<Boolean> destroyContext(final String contextId) {
@Override
public AsyncResult<Boolean> destroyContext(final String contextId) {
return AsyncCall.ensureRunInHandler(mJSHandler, new Callable<Boolean>() {
@Override
public Boolean call() {
try {
doricJSEngine.destroyContext(contextId);
doricContextMap.remove(contextId);
return true;
} catch (Exception e) {
DoricLog.e("destroyContext %s error is %s", contextId, e.getLocalizedMessage());
@ -116,9 +106,4 @@ public class DoricDriver {
}
});
}
public DoricContext getContext(String contextId) {
return doricContextMap.get(contextId);
}
}

View File

@ -0,0 +1,26 @@
package com.github.pengfeizhou.doric;
import com.github.pengfeizhou.doric.async.AsyncResult;
import com.github.pengfeizhou.jscore.JSDecoder;
/**
* @Description: com.github.pengfeizhou.doric
* @Author: pengfei.zhou
* @CreateDate: 2019-07-19
*/
public interface IDoricDriver {
AsyncResult<JSDecoder> invokeContextEntityMethod(final String contextId, final String method, final Object... args);
AsyncResult<JSDecoder> invokeDoricMethod(final String method, final Object... args);
void runOnJS(Runnable runnable);
void runOnUI(Runnable runnable);
void runIndependently(Runnable runnable);
AsyncResult<Boolean> createContext(final String contextId, final String script, final String source);
AsyncResult<Boolean> destroyContext(final String contextId);
}

View File

@ -3,6 +3,7 @@ package com.github.pengfeizhou.doric.extension.bridge;
import android.text.TextUtils;
import com.github.pengfeizhou.doric.DoricContext;
import com.github.pengfeizhou.doric.DoricContextManager;
import com.github.pengfeizhou.doric.DoricDriver;
import com.github.pengfeizhou.doric.plugin.DoricNativePlugin;
import com.github.pengfeizhou.doric.plugin.ModalPlugin;
@ -34,7 +35,7 @@ public class DoricBridgeExtension {
}
public JavaValue callNative(String contextId, String module, String methodName, String callbackId, JSDecoder jsDecoder) {
DoricContext context = DoricDriver.getInstance().getContext(contextId);
DoricContext context = DoricContextManager.getContext(contextId);
DoricPluginInfo pluginInfo = pluginInfoMap.get(module);
if (pluginInfo == null) {
DoricLog.e("Cannot find plugin class:%s", module);

View File

@ -41,7 +41,7 @@ public class DoricLog {
}
private static String suffixTag(String suffix) {
return TextUtils.isEmpty(suffix) ? TAG : suffix + TAG;
return TextUtils.isEmpty(suffix) ? TAG : TAG + suffix;
}
private static String format(String message, Object... args) {