Skip to content

Commit 45884c5

Browse files
authored
refactor(web): iron_init/setup logic (#763)
- `iron_init` is renamed to `setup`, so that - `init` is the function for initializing the WASM module. - `setup` is the function taking parameters for logger and performing other setting up operations. - Since `setup` may be called after `init` is called, it’s possible to use some functions of the WASM module before calling `setup` if necessity arises. - Common initializion code is moved to an hidden "internal" module of iron-remote-desktop crate, that is thus not part of the public API. - Restored the "IronRDP is ready" debug log when `setup` is called.
1 parent 712da42 commit 45884c5

6 files changed

Lines changed: 54 additions & 55 deletions

File tree

crates/iron-remote-desktop/src/lib.rs

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,41 +25,6 @@ impl DesktopSize {
2525
}
2626
}
2727

28-
pub fn iron_init(log_level: &str) {
29-
// When the `console_error_panic_hook` feature is enabled, we can call the
30-
// `set_panic_hook` function at least once during initialization, and then
31-
// we will get better error messages if our code ever panics.
32-
//
33-
// For more details see
34-
// https://github.com/rustwasm/console_error_panic_hook#readme
35-
#[cfg(feature = "panic_hook")]
36-
console_error_panic_hook::set_once();
37-
38-
if let Ok(level) = log_level.parse::<tracing::Level>() {
39-
set_logger_once(level);
40-
}
41-
}
42-
43-
fn set_logger_once(level: tracing::Level) {
44-
use tracing_subscriber::filter::LevelFilter;
45-
use tracing_subscriber::fmt::time::UtcTime;
46-
use tracing_subscriber::prelude::*;
47-
use tracing_web::MakeConsoleWriter;
48-
49-
static INIT: std::sync::Once = std::sync::Once::new();
50-
51-
INIT.call_once(|| {
52-
let fmt_layer = tracing_subscriber::fmt::layer()
53-
.with_ansi(false)
54-
.with_timer(UtcTime::rfc_3339()) // std::time is not available in browsers
55-
.with_writer(MakeConsoleWriter);
56-
57-
let level_filter = LevelFilter::from_level(level);
58-
59-
tracing_subscriber::registry().with(fmt_layer).with(level_filter).init();
60-
})
61-
}
62-
6328
pub trait RemoteDesktopApi {
6429
type Session: Session;
6530
type SessionBuilder: SessionBuilder;
@@ -71,10 +36,10 @@ pub trait RemoteDesktopApi {
7136
type Error: IronError;
7237

7338
/// Called before the logger is set.
74-
fn pre_init() {}
39+
fn pre_setup() {}
7540

7641
/// Called after the logger is set.
77-
fn post_init() {}
42+
fn post_setup() {}
7843
}
7944

8045
#[macro_export]
@@ -89,10 +54,10 @@ macro_rules! export {
8954
};
9055

9156
#[wasm_bindgen]
92-
pub fn iron_init(log_level: &str) {
93-
<$api as RemoteDesktopApi>::pre_init();
94-
$crate::iron_init(log_level);
95-
<$api as RemoteDesktopApi>::post_init();
57+
pub fn setup(log_level: &str) {
58+
<$api as RemoteDesktopApi>::pre_setup();
59+
$crate::internal::setup(log_level);
60+
<$api as RemoteDesktopApi>::post_setup();
9661
}
9762

9863
#[wasm_bindgen]
@@ -364,3 +329,42 @@ macro_rules! export {
364329
}
365330
};
366331
}
332+
333+
#[doc(hidden)]
334+
pub mod internal {
335+
#[doc(hidden)]
336+
pub fn setup(log_level: &str) {
337+
// When the `console_error_panic_hook` feature is enabled, we can call the
338+
// `set_panic_hook` function at least once during initialization, and then
339+
// we will get better error messages if our code ever panics.
340+
//
341+
// For more details see
342+
// https://github.com/rustwasm/console_error_panic_hook#readme
343+
#[cfg(feature = "panic_hook")]
344+
console_error_panic_hook::set_once();
345+
346+
if let Ok(level) = log_level.parse::<tracing::Level>() {
347+
set_logger_once(level);
348+
}
349+
}
350+
351+
fn set_logger_once(level: tracing::Level) {
352+
use tracing_subscriber::filter::LevelFilter;
353+
use tracing_subscriber::fmt::time::UtcTime;
354+
use tracing_subscriber::prelude::*;
355+
use tracing_web::MakeConsoleWriter;
356+
357+
static INIT: std::sync::Once = std::sync::Once::new();
358+
359+
INIT.call_once(|| {
360+
let fmt_layer = tracing_subscriber::fmt::layer()
361+
.with_ansi(false)
362+
.with_timer(UtcTime::rfc_3339()) // std::time is not available in browsers
363+
.with_writer(MakeConsoleWriter);
364+
365+
let level_filter = LevelFilter::from_level(level);
366+
367+
tracing_subscriber::registry().with(fmt_layer).with(level_filter).init();
368+
})
369+
}
370+
}

crates/ironrdp-web/src/lib.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,8 @@ impl RemoteDesktopApi for Api {
3333
type ClipboardTransaction = clipboard::RdpClipboardTransaction;
3434
type ClipboardContent = clipboard::RdpClipboardContent;
3535
type Error = error::IronError;
36-
}
37-
38-
#[doc(hidden)]
39-
pub mod internal {
40-
#[allow(dead_code)]
41-
fn iron_init(log_level: &str) {
42-
iron_remote_desktop::iron_init(log_level);
4336

37+
fn post_setup() {
4438
debug!("IronRDP is ready");
4539
}
4640
}

web-client/iron-remote-desktop-rdp/src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import init, {
2-
iron_init,
2+
setup,
33
DesktopSize,
44
DeviceEvent,
55
InputTransaction,
@@ -14,7 +14,7 @@ import { Extension } from './interfaces/Extension';
1414

1515
export default {
1616
init,
17-
iron_init,
17+
setup,
1818
DesktopSize,
1919
DeviceEvent,
2020
InputTransaction,

web-client/iron-remote-desktop/src/interfaces/RemoteDesktopModule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { Extension } from './Extension';
1111

1212
export interface RemoteDesktopModule {
1313
init: () => Promise<unknown>;
14-
iron_init: (logLevel: string) => void;
14+
setup: (logLevel: string) => void;
1515
DesktopSize: DesktopSize;
1616
DeviceEvent: DeviceEvent;
1717
InputTransaction: InputTransaction;

web-client/iron-remote-desktop/src/iron-remote-desktop.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@
655655
}
656656
657657
async function initcanvas() {
658-
loggingService.info('Start canvas initialization.');
658+
loggingService.info('Start canvas initialization...');
659659
660660
// Set a default canvas size. Need more test to know if i can remove it.
661661
canvas.width = 800;
@@ -671,6 +671,7 @@
671671
672672
loggingService.info('Component ready');
673673
loggingService.info('Dispatching ready event');
674+
674675
// bubbles:true is significant here, all our consumer code expect this specific event
675676
// but they only listen to the event on the custom element itself, not on the inner div
676677
// in Svelte 3, we had direct access to the customelement, but now in Svelte5, we have to

web-client/iron-remote-desktop/src/services/remote-desktop.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ export class RemoteDesktopService {
7878
}
7979

8080
async init(debug: LogType) {
81-
loggingService.info('Loading wasm file.');
81+
loggingService.info('Load wasm file...');
8282
await this.module.init();
83-
loggingService.info('Initializing IronRDP.');
84-
this.module.iron_init(LogType[debug]);
83+
loggingService.info('Initialize the remote desktop module...');
84+
this.module.setup(LogType[debug]);
8585
}
8686

8787
// If set to false, the clipboard will not be enabled and the callbacks will not be registered to the Rust side

0 commit comments

Comments
 (0)