lingxia-lxapp 0.4.3

LxApp (lightweight application) container and runtime for LingXia framework
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
use super::bridge::{
    BRIDGE_CANCELED, BRIDGE_INTERNAL_ERROR, BRIDGE_METHOD_NOT_FOUND, Bridge, JsonPatchOp,
    MessageHandler, MessageTransport, RpcError, ServiceType,
};
use crate::PageServiceEvent;
use crate::error;
use crate::error::LxAppError;
use crate::host::get_host;
use crate::lxapp::LxApp;
use crate::page::Page;
use rong::{
    Class, JSContext, JSFunc, JSObject, JSResult, JSValue, JsonToJSValue, RongJSError, Source,
    error::HostError, function::Optional, js_class, js_export, js_method,
};
use rong_event::EventEmitter;
use serde::Deserialize;
use serde_json::{Value, json};
use std::collections::HashMap;
use std::rc::Rc;
use tokio::sync::Mutex;

#[js_export]
pub struct PageSvc {
    functions: HashMap<String, JSFunc>,
    this: JSObject,

    pub(crate) page: Page,
    bridge: Bridge,

    event_emitter: EventEmitter,

    // state of PageSvc
    state: Rc<Mutex<PageSvcState>>,
}

struct PageSvcState {
    callback: HashMap<String, JSFunc>,
    state_callback: HashMap<u64, JSFunc>,
    state_rev: u64,
    init_data: Option<JSObject>,
}

fn rpc_error_from_lxapp_error(err: &LxAppError) -> RpcError {
    if let LxAppError::RongJSHost {
        code,
        message,
        data,
    } = err
    {
        return RpcError {
            code: code.clone(),
            message: Some(message.clone()),
            data: data.clone(),
        };
    }
    RpcError::new(BRIDGE_INTERNAL_ERROR, Some(err.to_string()))
}

fn rpc_error_from_rong(err: RongJSError) -> RpcError {
    let lxapp_error: LxAppError = err.into();
    rpc_error_from_lxapp_error(&lxapp_error)
}

impl MessageTransport for PageSvc {
    fn post_message_to_view(&self, message_json: String) -> Result<(), LxAppError> {
        if let Some(controller) = self.page.webview_controller() {
            controller
                .post_message(&message_json)
                .map_err(|e| LxAppError::WebView(e.to_string()))
        } else {
            Err(LxAppError::WebView("WebView not ready".to_string()))
        }
    }
}

impl MessageHandler for PageSvc {
    fn get_service_type(&self, service_name: &str) -> ServiceType {
        if let Some(api_name) = service_name.strip_prefix("host.")
            && let Some(handler) = get_host(api_name)
        {
            return ServiceType::HostAPI(handler);
        }

        if let Some(js_func) = self.functions.get(service_name) {
            return ServiceType::JSFunc(js_func.clone());
        }

        ServiceType::None
    }

    async fn get_state_snapshot(&self, _scope: Option<&str>) -> Result<Value, LxAppError> {
        let data_obj = self
            .this
            .get::<_, JSObject>("data")
            .map_err(|e| LxAppError::Bridge(e.to_string()))?;
        let data_json = data_obj
            .json_stringify()
            .map_err(|e| LxAppError::Bridge(e.to_string()))?;
        let state: Value =
            serde_json::from_str(&data_json).map_err(|e| LxAppError::Bridge(e.to_string()))?;
        let rev = self.state.try_lock().map(|s| s.state_rev).unwrap_or(0);
        Ok(json!({ "rev": rev, "state": state }))
    }

    async fn handle_req(
        &self,
        method: &str,
        params_json: Option<&str>,
        service_type: ServiceType,
        mut cancel_rx: tokio::sync::oneshot::Receiver<()>,
    ) -> Result<Value, RpcError> {
        let ctx = self.get_ctx();

        let build_call_arg = |json: Option<&str>| -> Option<JSValue> {
            let json = json?;
            if json == "null" {
                return None;
            }
            json.json_to_js_value(&ctx).ok()
        };

        let js_value_to_json = |v: JSValue| -> Result<Value, RpcError> {
            if v.is_undefined() || v.is_null() {
                return Ok(Value::Null);
            }
            if v.is_boolean() {
                let b: bool = v
                    .into_value()
                    .try_into()
                    .map_err(|e: RongJSError| rpc_error_from_rong(e))?;
                return Ok(Value::Bool(b));
            }
            if v.is_number() {
                let n: f64 = v
                    .into_value()
                    .try_into()
                    .map_err(|e: RongJSError| rpc_error_from_rong(e))?;
                return Ok(Value::Number(serde_json::Number::from_f64(n).ok_or_else(
                    || RpcError::new(BRIDGE_INTERNAL_ERROR, Some("Invalid number".to_string())),
                )?));
            }
            if v.is_string() {
                let s: String = v
                    .into_value()
                    .try_into()
                    .map_err(|e: RongJSError| rpc_error_from_rong(e))?;
                return Ok(Value::String(s));
            }
            if let Some(obj) = v.into_object() {
                let s = obj
                    .json_stringify()
                    .map_err(|e| RpcError::new(BRIDGE_INTERNAL_ERROR, Some(e.to_string())))?;
                return serde_json::from_str(&s)
                    .map_err(|e| RpcError::new(BRIDGE_INTERNAL_ERROR, Some(e.to_string())));
            }

            Err(RpcError::new(
                BRIDGE_INTERNAL_ERROR,
                Some("Unsupported JS return type".to_string()),
            ))
        };

        match service_type {
            ServiceType::JSFunc(js_func) => {
                let call_arg = build_call_arg(params_json);
                let fut = async {
                    match call_arg {
                        Some(val) => {
                            js_func
                                .call_async::<_, JSValue>(Some(self.this.clone()), (val,))
                                .await
                        }
                        None => {
                            js_func
                                .call_async::<_, JSValue>(Some(self.this.clone()), ())
                                .await
                        }
                    }
                };

                tokio::select! {
                    _ = &mut cancel_rx => {
                        Err(RpcError::new(BRIDGE_CANCELED, None))
                    }
                    res = fut => {
                        match res {
                            Ok(v) => js_value_to_json(v),
                            Err(e) => Err(rpc_error_from_rong(e)),
                        }
                    }
                }
            }
            ServiceType::HostAPI(handler) => {
                let lxapp = LxApp::from_ctx(&ctx)
                    .map_err(|e| RpcError::new(BRIDGE_INTERNAL_ERROR, Some(e.to_string())))?;
                let input = params_json.map(|s| s.to_string());

                // Allow immediate cancel response while still forwarding cancellation to Host.
                // NOTE: This runs inside a per-request task (bridge spawns Req handling), so
                // awaiting here will not block the bridge message pump.
                let start = std::time::Instant::now();
                let (host_cancel_tx, host_cancel_rx) = tokio::sync::oneshot::channel();
                let mut host_fut = handler.call(lxapp, input, host_cancel_rx);

                let json_result: Result<String, RpcError> = tokio::select! {
                    biased;
                    res = &mut host_fut => {
                        match res {
                            Ok(json) => Ok(json),
                            Err(e) => {
                                // Host handlers are expected to return a cancel error when they
                                // observe `cancel`. Map that to BRIDGE_CANCELED so view callers
                                // can handle it consistently.
                                if matches!(&e, LxAppError::Bridge(msg) if msg == "Canceled") {
                                    Err(RpcError::new(BRIDGE_CANCELED, None))
                                } else {
                                    Err(rpc_error_from_lxapp_error(&e))
                                }
                            }
                        }
                    }
                    _ = &mut cancel_rx => {
                        let _ = host_cancel_tx.send(());
                        Err(RpcError::new(BRIDGE_CANCELED, None))
                    }
                };

                let elapsed = start.elapsed();
                if elapsed > std::time::Duration::from_secs(3) {
                    crate::warn!(
                        "[{}] host req '{}' slow: {:?}",
                        self.page.path(),
                        method,
                        elapsed
                    );
                }

                let json = json_result?;
                serde_json::from_str(&json)
                    .map_err(|e| RpcError::new(BRIDGE_INTERNAL_ERROR, Some(e.to_string())))
            }
            ServiceType::None => Err(RpcError::new(
                BRIDGE_METHOD_NOT_FOUND,
                Some(format!("Method not found: {}", method)),
            )),
        }
    }

    async fn handle_notify(
        &self,
        method: &str,
        params_json: Option<&str>,
        service_type: ServiceType,
    ) {
        let ctx = self.get_ctx();

        let call_arg = params_json.and_then(|json| {
            if json == "null" {
                return None;
            }
            json.json_to_js_value(&ctx).ok()
        });

        match service_type {
            ServiceType::JSFunc(js_func) => {
                let this_obj = self.this.clone();
                let method_name = method.to_string();
                let page_path = self.page.path().to_string();
                let task = async move {
                    let result = match call_arg {
                        Some(val) => js_func.call_async::<_, ()>(Some(this_obj), (val,)).await,
                        None => js_func.call_async::<_, ()>(Some(this_obj), ()).await,
                    };
                    if let Err(e) = result {
                        error!("[{}] notify '{}' failed: {}", page_path, method_name, e);
                    }
                };
                rong::spawn(task);
            }
            ServiceType::HostAPI(handler) => {
                let lxapp = match LxApp::from_ctx(&ctx) {
                    Ok(app) => app,
                    Err(e) => {
                        error!("notify '{}' missing LxApp: {}", method, e);
                        return;
                    }
                };
                let input = params_json.map(|s| s.to_string());
                let method_name = method.to_string();

                // Notify calls are fire-and-forget but must not spuriously "cancel" themselves.
                let (cancel_tx, cancel_rx) = tokio::sync::oneshot::channel::<()>();
                rong::spawn(async move {
                    let _keep_alive = cancel_tx;
                    if let Err(e) = handler.call(lxapp, input, cancel_rx).await {
                        error!("notify '{}' failed: {}", method_name, e);
                    }
                });
            }
            ServiceType::None => {}
        }
    }

    async fn handle_bridge_ready(&self) {
        let mut page_svc_clone = self.clone();
        let _ = page_svc_clone.handle_bridge_ready_internal().await;
    }

    fn expected_bridge_nonce(&self) -> Option<String> {
        self.page.bridge_nonce()
    }

    fn bridge_page_path(&self) -> Option<String> {
        Some(self.page.path())
    }

    fn is_cap_allowed(&self, _cap: &str) -> bool {
        // Capability control is delegated to host app, not lxapp config.
        // Host app should handle permission checks in its HostHandler implementations.
        true
    }

    async fn handle_state_ack(&self, _scope: Option<String>, rev: u64) {
        let mut state = self.state.lock().await;
        if let Some(cb) = state.state_callback.remove(&rev) {
            drop(state);
            let _ = cb.call::<_, ()>(None, ());
        }
    }
}

#[js_class]
impl PageSvc {
    #[js_method(constructor)]
    fn _new(ctx: JSContext, config: JSObject, path: String) -> JSResult<JSObject> {
        let lxapp = LxApp::from_ctx(&ctx)?;

        let page = lxapp.get_page(&path).ok_or_else(|| {
            RongJSError::from(HostError::new(
                rong::error::E_NOT_FOUND,
                format!("Page not found: {}", path),
            ))
        })?;

        let init_data = JSObject::new(&ctx);

        if let Ok(original_data) = config.get::<_, JSObject>("data") {
            init_data.set("data", original_data)?;
        } else {
            init_data.set("data", JSObject::new(&ctx))?;
        }

        // Cache capabilities
        let mut page_svc = PageSvc {
            functions: HashMap::new(),
            this: config.clone(),
            page,
            bridge: Bridge::new(),
            event_emitter: EventEmitter::default(),
            state: Rc::new(Mutex::new(PageSvcState {
                callback: HashMap::new(),
                state_callback: HashMap::new(),
                state_rev: 0,
                init_data: None,
            })),
        };

        page_svc.register_functions(&config)?;

        {
            let mut state = page_svc.state.try_lock().unwrap();
            state.init_data = Some(init_data);
        }

        let class = Class::get::<PageSvc>(&ctx).unwrap();
        let instance = class.instance(page_svc);

        let binding = instance.clone();
        let mut page_svc = binding.borrow_mut::<PageSvc>().unwrap();
        page_svc.this = instance.clone();

        super::with_page_svc_map(&ctx, |page_svc_map| {
            page_svc_map.borrow_mut().insert(path, page_svc.clone());
            Ok(())
        })?;

        Ok(instance)
    }

    #[js_method(rename = "_setData")]
    async fn set_data(&self, ops_json: String, callback: Optional<JSFunc>) -> JSResult<()> {
        let mut state = self.state.lock().await;

        if !self.bridge.is_ready() {
            return Err(RongJSError::from(HostError::new(
                rong::error::E_INTERNAL,
                format!("Bridge of {} is not ready", self.page.path()),
            )));
        }

        let base_rev = state.state_rev;
        let new_rev = base_rev + 1;
        state.state_rev = new_rev;

        // Parse { ops: [...] } format from Page.js
        #[derive(Deserialize)]
        struct OpsWrapper {
            ops: Vec<JsonPatchOp>,
        }
        let wrapper: OpsWrapper = serde_json::from_str(&ops_json).map_err(|e| {
            RongJSError::from(HostError::new(rong::error::E_INTERNAL, e.to_string()))
        })?;
        let ops = wrapper.ops;

        let ack = if let Some(cb) = callback.0 {
            state.state_callback.insert(new_rev, cb);
            Some(true)
        } else {
            None
        };

        drop(state);

        self.bridge
            .send_state_patch(self, None, base_rev, new_rev, ops, ack)
            .map_err(|e| {
                RongJSError::from(HostError::new(rong::error::E_INTERNAL, e.to_string()))
            })?;

        Ok(())
    }

    #[js_method(rename = "getEventEmitter")]
    pub fn get_event_emitter(&self) -> EventEmitter {
        self.event_emitter.clone()
    }

    #[js_method(gc_mark)]
    pub fn gc_mark_with<F>(&self, mut mark_fn: F)
    where
        F: FnMut(&JSValue),
    {
        for (_, func) in self.functions.iter() {
            mark_fn(func.as_js_value());
        }
        mark_fn(self.this.as_js_value());

        if let Ok(state) = self.state.try_lock() {
            for (_, func) in state.callback.iter() {
                mark_fn(func.as_js_value());
            }
            for (_, func) in state.state_callback.iter() {
                mark_fn(func.as_js_value());
            }
        }
    }
}

impl PageSvc {
    fn register_functions(&mut self, obj: &JSObject) -> JSResult<()> {
        for key_value in obj.keys()? {
            if let Ok(function_name) = key_value.try_into::<String>() {
                if function_name.starts_with('_') {
                    continue;
                }
                if let Ok(func) = obj.get::<_, JSFunc>(function_name.as_str()) {
                    self.functions.insert(function_name.clone(), func);
                }
            }
        }
        Ok(())
    }

    pub(crate) async fn call_or_event_from_native(
        &self,
        ctx: &JSContext,
        func_name: &str,
        args: Option<&str>,
    ) -> JSResult<()> {
        if let Some(func) = self.functions.get(func_name) {
            let args_obj = args.and_then(|json| rong::JSObject::from_json_string(ctx, json).ok());
            return match args_obj {
                Some(obj) => {
                    func.call_async::<_, ()>(Some(self.this.clone()), (obj,))
                        .await
                }
                None => func.call_async::<_, ()>(Some(self.this.clone()), ()).await,
            };
        }
        Err(RongJSError::from(HostError::new(
            rong::error::E_INTERNAL,
            format!("No service: {}", func_name),
        )))
    }

    pub(crate) async fn call_page_event(
        &self,
        ctx: &JSContext,
        event: PageServiceEvent,
        args: Option<&str>,
    ) -> JSResult<()> {
        if let Some(js_func) = self.functions.get(event.as_str()) {
            let args_obj = args.and_then(|json| rong::JSObject::from_json_string(ctx, json).ok());
            rong::enqueue_js_invoke(
                ctx,
                js_func.clone(),
                Some(self.this.clone()),
                args_obj,
                rong::JsInvokePriority::Normal,
                None,
                false,
            )
            .await
        } else {
            // Page lifecycle handlers are optional by design.
            Ok(())
        }
    }

    async fn handle_bridge_ready_internal(&mut self) -> JSResult<()> {
        let mut state = self.state.lock().await;

        if let Some(init_data) = state.init_data.take() {
            // Extract the "data" field - this is the actual page data
            let page_data = init_data
                .get::<_, JSObject>("data")
                .unwrap_or_else(|_| JSObject::new(&self.this.get_ctx()));
            let data_json = page_data.json_stringify()?;
            let data_value: Value = serde_json::from_str(&data_json).map_err(|e| {
                RongJSError::from(HostError::new(rong::error::E_INTERNAL, e.to_string()))
            })?;

            state.state_rev = 1;
            drop(state);

            self.bridge
                .send_state_snapshot(self, None, 1, data_value)
                .map_err(|e| {
                    RongJSError::from(HostError::new(rong::error::E_INTERNAL, e.to_string()))
                })?;
        } else {
            drop(state);
        }

        self.page
            .dispatch_lifecycle_event(crate::PageLifecycleEvent::OnLoad);
        Ok(())
    }

    pub(crate) fn as_bridge(&self) -> &Bridge {
        &self.bridge
    }

    pub(crate) fn get_ctx(&self) -> JSContext {
        self.this.get_ctx()
    }
}

impl PageSvc {
    pub async fn create_in_ctx(ctx: &JSContext, path: &str) -> JSResult<()> {
        super::plugin::ensure_plugin_logic_loaded_for_page_path(ctx, path).await?;

        let create_page = ctx
            .global()
            .get::<_, JSFunc>("__CREATE_PAGE__")
            .map_err(|e| {
                RongJSError::from(HostError::new(rong::error::E_INTERNAL, e.to_string()))
            })?;

        create_page
            .call::<_, ()>(None, (path.to_string(),))
            .map_err(|e: RongJSError| {
                RongJSError::from(HostError::new(rong::error::E_INTERNAL, e.to_string()))
            })
    }

    pub fn get_page(&self) -> Page {
        self.page.clone()
    }
}

impl LxApp {
    pub async fn get_or_create_page_in_ctx(&self, ctx: &JSContext, url: &str) -> JSResult<PageSvc> {
        let page = self.get_or_create_page(url);

        page.wait_webview_ready()
            .await
            .map_err(|e| RongJSError::from(HostError::new(rong::error::E_INTERNAL, e)))?;

        let path = page.path();

        super::with_page_svc_map(ctx, |page_svc_map| {
            page_svc_map
                .borrow()
                .get(path.as_str())
                .cloned()
                .ok_or_else(|| {
                    RongJSError::from(HostError::new(
                        rong::error::E_INTERNAL,
                        "Page service not found",
                    ))
                })
        })
    }
}

fn get_current_pages(ctx: JSContext) -> JSResult<Vec<JSObject>> {
    let registry = ctx.global().get::<_, JSObject>("__PAGE_REGISTRY__")?;
    let lxapp = LxApp::from_ctx(&ctx)?;
    let paths = lxapp.get_page_stack();
    let mut pages = Vec::new();
    for p in paths {
        let obj = registry.get::<_, JSObject>(p.as_str())?;
        pages.push(obj);
    }
    Ok(pages)
}

pub(crate) fn init(ctx: &JSContext) -> JSResult<()> {
    ctx.register_class::<PageSvc>()?;

    let page_js = Source::from_bytes(include_str!("scripts/Page.js"));
    ctx.eval::<()>(page_js)?;

    let get_current_pages = rong::JSFunc::new(ctx, get_current_pages)?;
    ctx.global().set("getCurrentPages", get_current_pages)?;

    Ok(())
}