Skip to content

Commit 9df0a0f

Browse files
authored
js data ini (#490)
* dev onDataInit * [android] 合并JS改造代码 * 调整build和日志输出 * add logs * fix onDataInit
1 parent f169ff5 commit 9df0a0f

File tree

38 files changed

+1098
-196
lines changed

38 files changed

+1098
-196
lines changed

GaiaXAnalyze/GXAnalyzeAndroid/build.gradle

+3-1
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,13 @@ android {
9797

9898
buildTypes {
9999
debug {
100+
debuggable true
100101
ndk {
101-
abiFilters "x86", "x86_64", 'armeabi-v7a', 'arm64-v8a'
102+
abiFilters 'arm64-v8a', 'armeabi-v7a', "x86", "x86_64"
102103
}
103104
}
104105
release {
106+
debuggable false
105107
ndk {
106108
abiFilters 'armeabi-v7a', 'arm64-v8a'
107109
}

GaiaXAndroid/build.gradle

+10
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ android {
6666
}
6767
}
6868

69+
buildTypes {
70+
debug {
71+
debuggable true
72+
}
73+
release {
74+
minifyEnabled false
75+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
76+
}
77+
}
78+
6979
lintOptions {
7080
abortOnError false
7181
}

GaiaXAndroid/src/main/kotlin/com/alibaba/gaiax/template/GXTemplateInfo.kt

+5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ data class GXTemplateInfo(
4040
val js: String? = null
4141
) {
4242

43+
var preload: Boolean = false
44+
4345
var expVersion: String? = null
4446

4547
var children: MutableList<GXTemplateInfo>? = null
@@ -222,6 +224,8 @@ data class GXTemplateInfo(
222224
cssJson.getJSONObject(template.id)?.put(GXTemplateKey.GAIAX_LAYER_EDGE_INSETS, it)
223225
}
224226

227+
val preload = layerJson.getBooleanValue(GXTemplateKey.GAIAX_LAYER_PRELOAD)
228+
225229
val expVersion: String? = layerJson.getString(GXTemplateKey.GAIAX_LAYER_EXP_VERSION)
226230

227231
val layer = GXLayer.create(layerJson)
@@ -240,6 +244,7 @@ data class GXTemplateInfo(
240244
this.rawLayerJson = layerJson
241245
this.rawConfigJson = configJson
242246
this.expVersion = expVersion
247+
this.preload = preload
243248
}
244249
}
245250

GaiaXAndroid/src/main/kotlin/com/alibaba/gaiax/template/GXTemplateKey.kt

+2
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,8 @@ object GXTemplateKey {
346346

347347
const val GAIAX_LAYER_EXP_VERSION = "exp-version"
348348

349+
const val GAIAX_LAYER_PRELOAD = "preload"
350+
349351
const val GAIAX_PX = "px"
350352

351353
const val GAIAX_PT = "pt"

GaiaXAndroidAdapter/build.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ android {
6565
}
6666

6767
buildTypes {
68+
debug {
69+
debuggable true
70+
}
6871
release {
72+
debuggable false
6973
minifyEnabled false
7074
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
7175
}

GaiaXAndroidClientToStudio/build.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ android {
4343
}
4444

4545
buildTypes {
46+
debug {
47+
debuggable true
48+
}
4649
release {
50+
debuggable false
4751
minifyEnabled false
4852
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
4953
}

GaiaXAndroidClientToStudio/src/main/java/com/alibaba/gaiax/studio/GXSocket.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,7 @@ class GXSocket : SocketListener {
310310
gxSocketListener?.onStudioAddData(templateId, templateJson)
311311
lastTemplateId = templateId
312312
updateTask?.let { uiHandler.removeCallbacks(it) }
313-
updateTask =
314-
Runnable { gxSocketListener?.onStudioUpdate(templateId, templateJson) }
313+
updateTask = Runnable { gxSocketListener?.onStudioUpdate(templateId, templateJson) }
315314
updateTask?.let { uiHandler.postDelayed(it, 200) }
316315
}
317316
// 手动推送

GaiaXAndroidDemo/app/build.gradle

+11-4
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ android {
3333
minSdk 21
3434
targetSdk 33
3535
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
36-
37-
ndk {
38-
abiFilters "armeabi-v7a", "arm64-v8a", "x86"
39-
}
4036
}
4137

4238
sourceSets {
@@ -53,12 +49,23 @@ android {
5349
}
5450

5551
buildTypes {
52+
debug {
53+
debuggable true
54+
ndk {
55+
abiFilters 'arm64-v8a', 'armeabi-v7a', "x86", "x86_64"
56+
}
57+
}
5658
release {
59+
debuggable false
60+
ndk {
61+
abiFilters 'arm64-v8a', 'armeabi-v7a'
62+
}
5763
minifyEnabled true
5864
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
5965
}
6066
}
6167

68+
6269
compileOptions {
6370
sourceCompatibility JavaVersion.VERSION_1_8
6471
targetCompatibility JavaVersion.VERSION_1_8

GaiaXAndroidDemo/app/src/main/kotlin/com/alibaba/gaiax/demo/fastpreview/GXFastPreviewActivity.kt

+19-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.alibaba.gaiax.demo.fastpreview
22

3-
import android.net.Uri
43
import android.os.Bundle
54
import android.text.TextUtils
65
import android.util.Log
@@ -16,6 +15,7 @@ import com.alibaba.gaiax.js.proxy.GXJSEngineProxy
1615
import com.alibaba.gaiax.studio.GXStudioClient
1716
import com.alibaba.gaiax.template.GXSize.Companion.dpToPx
1817
import com.alibaba.gaiax.utils.GXScreenUtils
18+
import kotlin.math.log
1919

2020

2121
/**
@@ -26,7 +26,7 @@ import com.alibaba.gaiax.utils.GXScreenUtils
2626
class GXFastPreviewActivity : AppCompatActivity(), GXStudioClient.IFastPreviewListener {
2727

2828
companion object {
29-
private const val TAG = "[GaiaX]"
29+
private const val TAG = "[GaiaX][FastPreview]"
3030
const val GAIA_STUDIO_URL = "GAIA_STUDIO_URL"
3131
const val GAIA_STUDIO_MODE = "GAIA_STUDIO_MODE"
3232
const val GAIA_STUDIO_MODE_MULTI = "MULTI"
@@ -134,11 +134,7 @@ class GXFastPreviewActivity : AppCompatActivity(), GXStudioClient.IFastPreviewLi
134134
gxView = GXTemplateEngine.instance.createView(gxTemplateItem, gxMeasureSize)
135135

136136
gxView?.let {
137-
// 绑定数据
138-
GXTemplateEngine.instance.bindData(gxView, gxTemplateData)
139137

140-
// 将数据加入页面中
141-
fastPreviewRoot.addView(gxView, 0)
142138

143139
// 获取模板信息
144140
val gxTemplateInfo = GXTemplateEngine.instance.getGXTemplateInfo(gxTemplateItem)
@@ -151,9 +147,24 @@ class GXFastPreviewActivity : AppCompatActivity(), GXStudioClient.IFastPreviewLi
151147
Log.d(TAG, "exception() called with: data = $data")
152148
}
153149
}
150+
GXJSEngineProxy.instance.registerComponent(gxView)
154151

155-
// 注册容器
156-
GXJSEngineProxy.instance.registerComponentAndOnReady(gxView)
152+
if (gxTemplateInfo.preload) {
153+
GXJSEngineProxy.instance.onDataInit(gxView, gxTemplateData.data)?.let {
154+
Log.d(TAG, "create: onDataInit changed data=${it}")
155+
gxTemplateData.data = it
156+
}
157+
}
158+
159+
// 绑定数据
160+
GXTemplateEngine.instance.bindData(gxView, gxTemplateData)
161+
162+
// 将数据加入页面中
163+
fastPreviewRoot.addView(gxView, 0)
164+
165+
GXJSEngineProxy.instance.onReady(gxView)
166+
167+
Log.d(TAG, "create() called end")
157168
}
158169
}
159170
}

GaiaXAndroidJS/build.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ android {
4141
}
4242

4343
buildTypes {
44+
debug {
45+
debuggable true
46+
}
4447
release {
48+
debuggable false
4549
minifyEnabled false
4650
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
4751
}

GaiaXAndroidJS/src/main/assets/bootstrap.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

GaiaXAndroidJS/src/main/kotlin/com/alibaba/gaiax/js/GXJSEngine.kt

+109-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.alibaba.gaiax.js
22

33
import android.content.Context
4+
import android.text.TextUtils
5+
import com.alibaba.fastjson.JSONArray
46
import com.alibaba.fastjson.JSONObject
57
import com.alibaba.gaiax.js.api.GXJSBaseModule
8+
import com.alibaba.gaiax.js.api.IGXPage
9+
import com.alibaba.gaiax.js.engine.GXHostContext
610
import com.alibaba.gaiax.js.engine.GXHostEngine
711
import com.alibaba.gaiax.js.impl.debug.DebugJSContext
812
import com.alibaba.gaiax.js.impl.debug.ISocketBridgeListener
@@ -27,7 +31,7 @@ class GXJSEngine {
2731
}
2832
}
2933

30-
internal enum class EngineType {
34+
enum class EngineType {
3135
QuickJS, DebugJS
3236
}
3337

@@ -96,7 +100,8 @@ class GXJSEngine {
96100
}
97101
if (file.startsWith(MODULE_PREFIX) && file.endsWith(MODULE_SUFFIX)) {
98102
try {
99-
val bizModules = JSONObject.parseObject(assetsOpen("$GAIAX_JS_MODULES/$file").bufferedReader(Charsets.UTF_8).use { it.readText() })
103+
val bizModules = JSONObject.parseObject(
104+
assetsOpen("$GAIAX_JS_MODULES/$file").bufferedReader(Charsets.UTF_8).use { it.readText() })
100105
allModules.putAll(bizModules)
101106
} catch (e: Exception) {
102107
e.printStackTrace()
@@ -106,6 +111,9 @@ class GXJSEngine {
106111
}
107112
}
108113
}
114+
if (Log.isLog()) {
115+
Log.d("registerAssetsModules() called with: allModules = $allModules")
116+
}
109117
allModules.forEach {
110118
try {
111119
Class.forName(it.value.toString())
@@ -127,7 +135,8 @@ class GXJSEngine {
127135

128136
private fun assetsOpen(file: String) = synchronized(context.assets) { context.assets.open(file) }
129137

130-
private fun assetsModules(path: String): Array<out String>? = synchronized(context.assets) { context.assets.list(path) }
138+
private fun assetsModules(path: String): Array<out String>? =
139+
synchronized(context.assets) { context.assets.list(path) }
131140

132141

133142
fun startDefaultEngine(complete: (() -> Unit)? = null) {
@@ -225,14 +234,33 @@ class GXJSEngine {
225234

226235
fun onNativeEvent(componentId: Long, data: JSONObject) {
227236
quickJSEngine?.runtime()?.context()?.getComponent(componentId)?.onNativeEvent(data)
237+
if (data.getBooleanValue("isPage")) {
238+
findPage(componentId, EngineType.QuickJS)?.let {
239+
it.onNativeEvent(data)
240+
}
241+
}
228242
debugEngine?.runtime()?.context()?.getComponent(componentId)?.onNativeEvent(data)
229243
}
230244

245+
fun postAnimationMessage(data: JSONObject) {
246+
quickJSEngine?.runtime()?.context()?.postAnimationMessage(data)
247+
debugEngine?.runtime()?.context()?.postAnimationMessage(data)
248+
}
249+
250+
fun postModalMessage(data: JSONObject) {
251+
quickJSEngine?.runtime()?.context()?.postModalMessage(data)
252+
debugEngine?.runtime()?.context()?.postModalMessage(data)
253+
}
254+
231255
fun onReady(componentId: Long) {
232256
quickJSEngine?.runtime()?.context()?.getComponent(componentId)?.onReady()
233257
debugEngine?.runtime()?.context()?.getComponent(componentId)?.onReady()
234258
}
235259

260+
fun onDataInit(componentId: Long, data: JSONObject): JSONObject? {
261+
return quickJSEngine?.runtime()?.context()?.getComponent(componentId)?.onDataInit(data)
262+
}
263+
236264
fun onReuse(componentId: Long) {
237265
quickJSEngine?.runtime()?.context()?.getComponent(componentId)?.onReuse()
238266
debugEngine?.runtime()?.context()?.getComponent(componentId)?.onReuse()
@@ -258,6 +286,8 @@ class GXJSEngine {
258286
debugEngine?.runtime()?.context()?.getComponent(componentId)?.onLoadMore(data)
259287
}
260288

289+
290+
261291
/**
262292
* 为视图注册JS组件
263293
*/
@@ -269,6 +299,20 @@ class GXJSEngine {
269299
return componentId
270300
}
271301

302+
fun generateUniqueInstanceId(): Long {
303+
return IdGenerator.genLongId()
304+
}
305+
306+
fun registerComponentWithId(
307+
instanceId: Long, bizId: String, templateId: String, templateVersion: String, script: String?
308+
) {
309+
if (script != null) {
310+
quickJSEngine?.runtime()?.context()
311+
?.registerComponent(instanceId, bizId, templateId, templateVersion, script)
312+
debugEngine?.runtime()?.context()?.registerComponent(instanceId, bizId, templateId, templateVersion, script)
313+
}
314+
}
315+
272316
/**
273317
* 为视图解除JS组件
274318
*/
@@ -277,6 +321,68 @@ class GXJSEngine {
277321
debugEngine?.runtime()?.context()?.unregisterComponent(componentId)
278322
}
279323

324+
fun registerPage(
325+
bizId: String,
326+
templateId: String,
327+
templateVersion: String,
328+
script: String,
329+
nativePage: IGXPage
330+
): Long {
331+
// 页面instanceId从50000起
332+
val pageId = IdGenerator.genLongId() + 50000
333+
quickJSEngine?.runtime()?.context()
334+
?.registerPage(pageId, bizId, templateId, templateVersion, script, nativePage)
335+
debugEngine?.runtime()?.context()?.registerPage(pageId, bizId, templateId, templateVersion, script, nativePage)
336+
return pageId
337+
}
338+
339+
fun unregisterPage(id: Long) {
340+
quickJSEngine?.runtime()?.context()?.unregisterPage(id)
341+
debugEngine?.runtime()?.context()?.unregisterPage(id)
342+
}
343+
344+
fun findPage(id: Long, engineType: EngineType): IGXPage? {
345+
return when (engineType) {
346+
EngineType.QuickJS -> quickJSEngine?.runtime()?.context()?.findPage(id)
347+
EngineType.DebugJS -> debugEngine?.runtime()?.context()?.findPage(id)
348+
}
349+
}
350+
351+
fun onPageLoad(id: Long, data: JSONObject) {
352+
quickJSEngine?.runtime()?.context()?.findPage(id)?.onLoad(data)
353+
debugEngine?.runtime()?.context()?.findPage(id)?.onLoad(data)
354+
}
355+
356+
fun onPageUnload(id: Long) {
357+
quickJSEngine?.runtime()?.context()?.findPage(id)?.onUnload()
358+
debugEngine?.runtime()?.context()?.findPage(id)?.onUnload()
359+
}
360+
361+
fun onPageReady(id: Long) {
362+
quickJSEngine?.runtime()?.context()?.findPage(id)?.onReady()
363+
debugEngine?.runtime()?.context()?.findPage(id)?.onReady()
364+
}
365+
366+
fun onPageShow(id: Long) {
367+
quickJSEngine?.runtime()?.context()?.findPage(id)?.onShow()
368+
debugEngine?.runtime()?.context()?.findPage(id)?.onShow()
369+
}
370+
371+
fun onPageHide(id: Long) {
372+
quickJSEngine?.runtime()?.context()?.findPage(id)?.onHide()
373+
debugEngine?.runtime()?.context()?.findPage(id)?.onHide()
374+
}
375+
376+
fun onPageScroll(id: Long, data: JSONObject) {
377+
quickJSEngine?.runtime()?.context()?.findPage(id)?.onPageScroll(data)
378+
debugEngine?.runtime()?.context()?.findPage(id)?.onPageScroll(data)
379+
}
380+
381+
fun onPageReachBottom(id: Long) {
382+
quickJSEngine?.runtime()?.context()?.findPage(id)?.onReachBottom()
383+
debugEngine?.runtime()?.context()?.findPage(id)?.onReachBottom()
384+
}
385+
280386
interface IJsExceptionListener {
281387

282388
/**

0 commit comments

Comments
 (0)