diff --git a/GaiaXAndroid/src/main/kotlin/com/alibaba/gaiax/context/GXTemplateContext.kt b/GaiaXAndroid/src/main/kotlin/com/alibaba/gaiax/context/GXTemplateContext.kt index d057db5e4..0dac0000a 100644 --- a/GaiaXAndroid/src/main/kotlin/com/alibaba/gaiax/context/GXTemplateContext.kt +++ b/GaiaXAndroid/src/main/kotlin/com/alibaba/gaiax/context/GXTemplateContext.kt @@ -67,14 +67,9 @@ class GXTemplateContext private constructor( var isAppear: Boolean? = null /** - * 用于存储JS组件ID + * 用于存储额外参数 */ - var jsComponentIds: MutableSet? = null - - /** - * 根节点JS组件ID - */ - var rootJSComponentId: Long? = null + var extArg: Any? = null /** * 用于追踪日志 diff --git a/GaiaXAndroidJSProxy/src/main/java/com/alibaba/gaiax/js/proxy/GXJSEngineProxy.kt b/GaiaXAndroidJSProxy/src/main/java/com/alibaba/gaiax/js/proxy/GXJSEngineProxy.kt index 8b8af6f10..9d1214ac3 100644 --- a/GaiaXAndroidJSProxy/src/main/java/com/alibaba/gaiax/js/proxy/GXJSEngineProxy.kt +++ b/GaiaXAndroidJSProxy/src/main/java/com/alibaba/gaiax/js/proxy/GXJSEngineProxy.kt @@ -45,6 +45,18 @@ import com.alibaba.gaiax.template.GXTemplateInfo */ class GXJSEngineProxy { + class GXTemplateContextExtArg { + /** + * 用于存储JS组件ID + */ + var jsComponentIds: MutableSet = mutableSetOf() + + /** + * 根节点JS组件ID + */ + var rootJSComponentId: Long? = null + } + companion object { private const val TAG = "[GaiaX][JS]" @@ -210,8 +222,8 @@ class GXJSEngineProxy { if (Log.isLog()) { Log.d("onReady() called with: gxView = $gxView") } - GXTemplateContext.getContext(gxView)?.let { - it.jsComponentIds?.forEach { jsComponentId -> + GXTemplateContext.getContext(gxView)?.let { gxTemplateContext -> + gxExtJSArg(gxTemplateContext)?.jsComponentIds?.forEach { jsComponentId -> GXJSEngine.instance.onReady(jsComponentId) } } @@ -224,8 +236,8 @@ class GXJSEngineProxy { if (Log.isLog()) { Log.d("onReuse() called with: gxView = $gxView") } - GXTemplateContext.getContext(gxView)?.let { - it.jsComponentIds?.forEach { jsComponentId -> + GXTemplateContext.getContext(gxView)?.let { gxTemplateContext -> + gxExtJSArg(gxTemplateContext)?.jsComponentIds?.forEach { jsComponentId -> GXJSEngine.instance.onReuse(jsComponentId) } } @@ -241,7 +253,7 @@ class GXJSEngineProxy { GXTemplateContext.getContext(gxView)?.let { gxTemplateContext -> // 通知JS组件显示 - gxTemplateContext.jsComponentIds?.forEach { jsComponentId -> + gxExtJSArg(gxTemplateContext)?.jsComponentIds?.forEach { jsComponentId -> GXJSEngine.instance.onShow(jsComponentId) } @@ -262,7 +274,7 @@ class GXJSEngineProxy { GXTemplateContext.getContext(gxView)?.let { gxTemplateContext -> // 通知JS组件隐藏 - gxTemplateContext.jsComponentIds?.forEach { jsComponentId -> + gxExtJSArg(gxTemplateContext)?.jsComponentIds?.forEach { jsComponentId -> GXJSEngine.instance.onHide(jsComponentId) } @@ -283,7 +295,7 @@ class GXJSEngineProxy { GXTemplateContext.getContext(gxView)?.let { gxTemplateContext -> // 通知JS组件销毁 - gxTemplateContext.jsComponentIds?.forEach { jsComponentId -> + gxExtJSArg(gxTemplateContext)?.jsComponentIds?.forEach { jsComponentId -> GXJSEngine.instance.onDestroy(jsComponentId) } @@ -298,8 +310,8 @@ class GXJSEngineProxy { if (Log.isLog()) { Log.d("onLoadMore() called with: gxView = $gxView") } - GXTemplateContext.getContext(gxView)?.let { - it.jsComponentIds?.forEach { jsComponentId -> + GXTemplateContext.getContext(gxView)?.let { gxTemplateContext -> + gxExtJSArg(gxTemplateContext)?.jsComponentIds?.forEach { jsComponentId -> GXJSEngine.instance.onLoadMore(jsComponentId, data) } } @@ -328,15 +340,15 @@ class GXJSEngineProxy { GXTemplateContext.getContext(gxView)?.let { gxTemplateContext -> // 在GaiaX中存储JS组件ID - if (gxTemplateContext.jsComponentIds == null) { - gxTemplateContext.jsComponentIds = mutableSetOf() + if (gxTemplateContext.extArg == null) { + gxTemplateContext.extArg = GXTemplateContextExtArg() } // 寻找可注册的JS组件 registerTemplateTree(gxTemplateContext, gxTemplateContext.templateInfo) // 将注册组件ID都和跟视图做全局映射 - gxTemplateContext.jsComponentIds?.forEach { jsComponentId -> + gxExtJSArg(gxTemplateContext)?.jsComponentIds?.forEach { jsComponentId -> GXJSRenderProxy.instance.jsGlobalComponentMap[jsComponentId] = gxView } } @@ -352,13 +364,13 @@ class GXJSEngineProxy { GXTemplateContext.getContext(gxView)?.let { gxTemplateContext -> // 解除JS组件ID和视图的全局映射 - gxTemplateContext.jsComponentIds?.forEach { jsComponentId -> + gxExtJSArg(gxTemplateContext)?.jsComponentIds?.forEach { jsComponentId -> GXJSRenderProxy.instance.jsGlobalComponentMap.remove(jsComponentId) } - gxTemplateContext.jsComponentIds?.forEach { jsComponentId -> + gxExtJSArg(gxTemplateContext)?.jsComponentIds?.forEach { jsComponentId -> GXJSEngine.instance.unregisterComponent(jsComponentId) } - gxTemplateContext.jsComponentIds?.clear() + gxExtJSArg(gxTemplateContext)?.jsComponentIds?.clear() // 遍历容器子视图,通知坑位解除注册 GXContainerUtils.notifyView(gxTemplateContext) { gxView: View -> @@ -379,10 +391,13 @@ class GXJSEngineProxy { val jsComponentId = GXJSEngine.instance.registerComponent( templateBiz, templateId, templateVersion, script ) - if (gxTemplateContext.rootJSComponentId == null) { - gxTemplateContext.rootJSComponentId = jsComponentId + gxExtJSArg(gxTemplateContext)?.let { + if (it.rootJSComponentId == null) { + it.rootJSComponentId = jsComponentId + } + it.jsComponentIds.add(jsComponentId) } - gxTemplateContext.jsComponentIds?.add(jsComponentId) + } else { if (Log.isLog()) { Log.d("registerTemplateTree() called with: $templateId script is null") @@ -402,6 +417,8 @@ class GXJSEngineProxy { } } + private fun gxExtJSArg(gxTemplateContext: GXTemplateContext) = (gxTemplateContext.extArg as? GXTemplateContextExtArg) + fun onEvent(componentId: Long, type: String, data: JSONObject) { GXJSEngine.instance.onEvent(componentId, type, data) } diff --git a/GaiaXAndroidJSProxy/src/main/java/com/alibaba/gaiax/js/proxy/GXJSRenderProxy.kt b/GaiaXAndroidJSProxy/src/main/java/com/alibaba/gaiax/js/proxy/GXJSRenderProxy.kt index ec805095a..8f13ad9a9 100644 --- a/GaiaXAndroidJSProxy/src/main/java/com/alibaba/gaiax/js/proxy/GXJSRenderProxy.kt +++ b/GaiaXAndroidJSProxy/src/main/java/com/alibaba/gaiax/js/proxy/GXJSRenderProxy.kt @@ -58,9 +58,7 @@ internal class GXJSRenderProxy { } fun getNodeInfo(targetId: String, templateId: String, componentId: Long): JSONObject { - val nodeInfo: GXNode? = GXTemplateEngine.instance.getGXNodeById( - jsGlobalComponentMap[componentId], targetId - ) + val nodeInfo: GXNode? = GXTemplateEngine.instance.getGXNodeById(jsGlobalComponentMap[componentId], targetId) return if (nodeInfo != null) { val targetNode = JSONObject() targetNode["targetType"] = nodeInfo.templateNode.layer.type