1
1
package com.alibaba.gaiax.js
2
2
3
3
import android.content.Context
4
+ import android.text.TextUtils
5
+ import com.alibaba.fastjson.JSONArray
4
6
import com.alibaba.fastjson.JSONObject
5
7
import com.alibaba.gaiax.js.api.GXJSBaseModule
8
+ import com.alibaba.gaiax.js.api.IGXPage
9
+ import com.alibaba.gaiax.js.engine.GXHostContext
6
10
import com.alibaba.gaiax.js.engine.GXHostEngine
7
11
import com.alibaba.gaiax.js.impl.debug.DebugJSContext
8
12
import com.alibaba.gaiax.js.impl.debug.ISocketBridgeListener
@@ -27,7 +31,7 @@ class GXJSEngine {
27
31
}
28
32
}
29
33
30
- internal enum class EngineType {
34
+ enum class EngineType {
31
35
QuickJS , DebugJS
32
36
}
33
37
@@ -96,7 +100,8 @@ class GXJSEngine {
96
100
}
97
101
if (file.startsWith(MODULE_PREFIX ) && file.endsWith(MODULE_SUFFIX )) {
98
102
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() })
100
105
allModules.putAll(bizModules)
101
106
} catch (e: Exception ) {
102
107
e.printStackTrace()
@@ -106,6 +111,9 @@ class GXJSEngine {
106
111
}
107
112
}
108
113
}
114
+ if (Log .isLog()) {
115
+ Log .d(" registerAssetsModules() called with: allModules = $allModules " )
116
+ }
109
117
allModules.forEach {
110
118
try {
111
119
Class .forName(it.value.toString())
@@ -127,7 +135,8 @@ class GXJSEngine {
127
135
128
136
private fun assetsOpen (file : String ) = synchronized(context.assets) { context.assets.open(file) }
129
137
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) }
131
140
132
141
133
142
fun startDefaultEngine (complete : (() -> Unit )? = null) {
@@ -225,14 +234,33 @@ class GXJSEngine {
225
234
226
235
fun onNativeEvent (componentId : Long , data : JSONObject ) {
227
236
quickJSEngine?.runtime()?.context()?.getComponent(componentId)?.onNativeEvent(data)
237
+ if (data.getBooleanValue(" isPage" )) {
238
+ findPage(componentId, EngineType .QuickJS )?.let {
239
+ it.onNativeEvent(data)
240
+ }
241
+ }
228
242
debugEngine?.runtime()?.context()?.getComponent(componentId)?.onNativeEvent(data)
229
243
}
230
244
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
+
231
255
fun onReady (componentId : Long ) {
232
256
quickJSEngine?.runtime()?.context()?.getComponent(componentId)?.onReady()
233
257
debugEngine?.runtime()?.context()?.getComponent(componentId)?.onReady()
234
258
}
235
259
260
+ fun onDataInit (componentId : Long , data : JSONObject ): JSONObject ? {
261
+ return quickJSEngine?.runtime()?.context()?.getComponent(componentId)?.onDataInit(data)
262
+ }
263
+
236
264
fun onReuse (componentId : Long ) {
237
265
quickJSEngine?.runtime()?.context()?.getComponent(componentId)?.onReuse()
238
266
debugEngine?.runtime()?.context()?.getComponent(componentId)?.onReuse()
@@ -258,6 +286,8 @@ class GXJSEngine {
258
286
debugEngine?.runtime()?.context()?.getComponent(componentId)?.onLoadMore(data)
259
287
}
260
288
289
+
290
+
261
291
/* *
262
292
* 为视图注册JS组件
263
293
*/
@@ -269,6 +299,20 @@ class GXJSEngine {
269
299
return componentId
270
300
}
271
301
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
+
272
316
/* *
273
317
* 为视图解除JS组件
274
318
*/
@@ -277,6 +321,68 @@ class GXJSEngine {
277
321
debugEngine?.runtime()?.context()?.unregisterComponent(componentId)
278
322
}
279
323
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
+
280
386
interface IJsExceptionListener {
281
387
282
388
/* *
0 commit comments