abstract Driver
This commit is contained in:
parent
0a4bc77b15
commit
36b478bfb3
@ -22,14 +22,14 @@ public class DoricContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static DoricContext createContext(String script, String alias) {
|
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) {
|
public AsyncResult<JSDecoder> callEntity(String methodName, Object... args) {
|
||||||
return getDriver().invokeContextEntityMethod(mContextId, methodName, args);
|
return getDriver().invokeContextEntityMethod(mContextId, methodName, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public DoricDriver getDriver() {
|
public IDoricDriver getDriver() {
|
||||||
return DoricDriver.getInstance();
|
return DoricDriver.getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,7 +39,7 @@ public class DoricContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void teardown() {
|
public void teardown() {
|
||||||
DoricDriver.getInstance().destroyContext(mContextId).setCallback(new AsyncResult.Callback<Boolean>() {
|
DoricContextManager.getInstance().destroyContext(this).setCallback(new AsyncResult.Callback<Boolean>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResult(Boolean result) {
|
public void onResult(Boolean result) {
|
||||||
|
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -10,22 +10,17 @@ import com.github.pengfeizhou.doric.utils.DoricConstant;
|
|||||||
import com.github.pengfeizhou.doric.utils.DoricLog;
|
import com.github.pengfeizhou.doric.utils.DoricLog;
|
||||||
import com.github.pengfeizhou.jscore.JSDecoder;
|
import com.github.pengfeizhou.jscore.JSDecoder;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Description: Doric
|
* @Description: Doric
|
||||||
* @Author: pengfei.zhou
|
* @Author: pengfei.zhou
|
||||||
* @CreateDate: 2019-07-18
|
* @CreateDate: 2019-07-18
|
||||||
*/
|
*/
|
||||||
public class DoricDriver {
|
public class DoricDriver implements IDoricDriver {
|
||||||
private final DoricJSEngine doricJSEngine;
|
private final DoricJSEngine doricJSEngine;
|
||||||
private final AtomicInteger counter = new AtomicInteger();
|
|
||||||
private final Map<String, DoricContext> doricContextMap = new ConcurrentHashMap<>();
|
|
||||||
private final ExecutorService mBridgeExecutor;
|
private final ExecutorService mBridgeExecutor;
|
||||||
private final Handler mUIHandler;
|
private final Handler mUIHandler;
|
||||||
private final Handler mJSHandler;
|
private final Handler mJSHandler;
|
||||||
@ -81,11 +76,8 @@ public class DoricDriver {
|
|||||||
return Inner.sInstance;
|
return Inner.sInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
DoricContext createContext(final String script, final String source) {
|
public AsyncResult<Boolean> createContext(final String contextId, final String script, final String source) {
|
||||||
final String contextId = String.valueOf(counter.incrementAndGet());
|
return AsyncCall.ensureRunInHandler(mJSHandler, new Callable<Boolean>() {
|
||||||
DoricContext doricContext = new DoricContext(contextId);
|
|
||||||
doricContextMap.put(contextId, doricContext);
|
|
||||||
AsyncCall.ensureRunInHandler(mJSHandler, new Callable<Boolean>() {
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean call() {
|
public Boolean call() {
|
||||||
try {
|
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>() {
|
return AsyncCall.ensureRunInHandler(mJSHandler, new Callable<Boolean>() {
|
||||||
@Override
|
@Override
|
||||||
public Boolean call() {
|
public Boolean call() {
|
||||||
try {
|
try {
|
||||||
doricJSEngine.destroyContext(contextId);
|
doricJSEngine.destroyContext(contextId);
|
||||||
doricContextMap.remove(contextId);
|
|
||||||
return true;
|
return true;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
DoricLog.e("destroyContext %s error is %s", contextId, e.getLocalizedMessage());
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
}
|
@ -3,6 +3,7 @@ package com.github.pengfeizhou.doric.extension.bridge;
|
|||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
|
||||||
import com.github.pengfeizhou.doric.DoricContext;
|
import com.github.pengfeizhou.doric.DoricContext;
|
||||||
|
import com.github.pengfeizhou.doric.DoricContextManager;
|
||||||
import com.github.pengfeizhou.doric.DoricDriver;
|
import com.github.pengfeizhou.doric.DoricDriver;
|
||||||
import com.github.pengfeizhou.doric.plugin.DoricNativePlugin;
|
import com.github.pengfeizhou.doric.plugin.DoricNativePlugin;
|
||||||
import com.github.pengfeizhou.doric.plugin.ModalPlugin;
|
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) {
|
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);
|
DoricPluginInfo pluginInfo = pluginInfoMap.get(module);
|
||||||
if (pluginInfo == null) {
|
if (pluginInfo == null) {
|
||||||
DoricLog.e("Cannot find plugin class:%s", module);
|
DoricLog.e("Cannot find plugin class:%s", module);
|
||||||
|
@ -41,7 +41,7 @@ public class DoricLog {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static String suffixTag(String suffix) {
|
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) {
|
private static String format(String message, Object... args) {
|
||||||
|
Reference in New Issue
Block a user