Skip to content

Commit

Permalink
refactor: some initialization error handling cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lukexor committed May 24, 2024
1 parent e94f668 commit 507d9a0
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 24 deletions.
3 changes: 0 additions & 3 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
[build]
rustflags = ["-Z", "threads=8"]

[target.'cfg(target_arch = "wasm32")']
rustflags = ["-Z", "threads=8", "--cfg=web_sys_unstable_apis"]
2 changes: 1 addition & 1 deletion tetanes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ rust_2021_compatibility = "warn"
unused = "warn"

[package.metadata.docs.rs]
rustc-args = ["--cfg=web_sys_unstable_apis"]
targets = ["wasm32-unknown-unknown"]

[features]
Expand Down Expand Up @@ -112,6 +111,7 @@ web-sys = { version = "0.3", features = [
"HtmlCanvasElement",
"HtmlElement",
"HtmlInputElement",
"HtmlDivElement",
"Window",
] }
wasm-bindgen = "0.2"
Expand Down
12 changes: 12 additions & 0 deletions tetanes/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
margin-bottom: 100px;
color: var(--color);
font-family: "Pixeloid Sans", "Courier New", Courier, monospace;
display: flex;
flex-direction: column;
align-items: center;
}

h1 {
Expand Down Expand Up @@ -134,6 +137,11 @@
animation: spin 2s linear infinite;
}

#error {
color: #ff3333;
text-align: center;
}

@keyframes spin {
0% {
transform: rotate(0deg);
Expand Down Expand Up @@ -175,6 +183,10 @@ <h3 id="loading-status">
<div id="loader"></div>
Loading...
</h3>
<p id="error" class="hidden">
An internal error occurred. Try refreshing the page or filing a
<a href="https://github.com/lukexor/tetanes/issues/new">bug report</a>.
</p>

<div id="content">
<p>
Expand Down
12 changes: 10 additions & 2 deletions tetanes/initializer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
export default function () {
const loading = document.getElementById("loading-status");
const error = document.getElementById("error");
return {
onStart: () => {
console.log("Loading...");
console.time("initializer");
loading.classList.remove("hidden");
error.classList.add("hidden");
},
onProgress: ({ current, total }) => {
if (!total) {
Expand All @@ -14,13 +18,17 @@ export default function () {
onComplete: () => {
console.log("Loading... done!");
console.timeEnd("initializer");
loading.classList.add("hidden");
},
onSuccess: (wasm) => {
onSuccess: () => {
console.log("Loading... successful!");
console.log("WebAssembly: ", wasm);
error.classList.add("hidden");
},
onFailure: (error) => {
console.error(`Loading... failed! ${error}`);
loading.classList.add("hidden");
error.classList.remove("hidden");
error.innerText = `Loading... failed! ${error}`;
},
};
}
18 changes: 9 additions & 9 deletions tetanes/src/nes/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,12 +627,12 @@ impl Running {
if let Some(action) = self.input_bindings.get(&input).copied() {
trace!("action: {action:?}, state: {state:?}, repeat: {repeat:?}");
let released = state == ElementState::Released;
let root_window = Some(window_id) == self.renderer.root_window_id();
let is_root_window = Some(window_id) == self.renderer.root_window_id();
match action {
Action::Ui(ui_state) if released => match ui_state {
Ui::Quit => self.tx.nes_event(UiEvent::Terminate),
Ui::TogglePause => {
if root_window && self.renderer.rom_loaded() {
if is_root_window && self.renderer.rom_loaded() {
self.paused = !self.paused;
self.nes_event(EmulationEvent::Pause(self.paused));
}
Expand Down Expand Up @@ -664,7 +664,7 @@ impl Running {
}
},
Action::Menu(menu) if released => self.nes_event(RendererEvent::Menu(menu)),
Action::Feature(feature) if root_window => match feature {
Action::Feature(feature) if is_root_window => match feature {
Feature::ToggleReplayRecording if released => {
if platform::supports(platform::Feature::Filesystem) {
if self.renderer.rom_loaded() {
Expand Down Expand Up @@ -719,7 +719,7 @@ impl Running {
_ => (),
},
Action::Setting(setting) => match setting {
Setting::ToggleFullscreen if released && root_window => {
Setting::ToggleFullscreen if released && is_root_window => {
self.cfg.renderer.fullscreen = !self.cfg.renderer.fullscreen;
self.renderer.set_fullscreen(self.cfg.renderer.fullscreen);
}
Expand Down Expand Up @@ -768,7 +768,7 @@ impl Running {
}
}
Setting::FastForward
if !repeat && root_window && self.renderer.rom_loaded() =>
if !repeat && is_root_window && self.renderer.rom_loaded() =>
{
let new_speed = if released { 1.0 } else { 2.0 };
let speed = self.cfg.emulation.speed;
Expand All @@ -787,7 +787,7 @@ impl Running {
DeckAction::Reset(kind) if released => {
self.nes_event(EmulationEvent::Reset(kind));
}
DeckAction::Joypad((player, button)) if !repeat && root_window => {
DeckAction::Joypad((player, button)) if !repeat && is_root_window => {
self.nes_event(EmulationEvent::Joypad((player, button, state)));
}
// Handled by `gui` module
Expand All @@ -810,7 +810,7 @@ impl Running {
);
}
}
DeckAction::SaveState if released && root_window => {
DeckAction::SaveState if released && is_root_window => {
if platform::supports(platform::Feature::Filesystem) {
self.nes_event(EmulationEvent::SaveState(self.cfg.emulation.save_slot));
} else {
Expand All @@ -820,7 +820,7 @@ impl Running {
);
}
}
DeckAction::LoadState if released && root_window => {
DeckAction::LoadState if released && is_root_window => {
if platform::supports(platform::Feature::Filesystem) {
self.nes_event(EmulationEvent::LoadState(self.cfg.emulation.save_slot));
} else {
Expand Down Expand Up @@ -874,7 +874,7 @@ impl Running {
format!("{kind:?} is not implemented yet"),
);
}
Debug::Step(step) if (released | repeat) && root_window => {
Debug::Step(step) if (released | repeat) && is_root_window => {
self.nes_event(EmulationEvent::DebugStep(step));
}
_ => (),
Expand Down
11 changes: 8 additions & 3 deletions tetanes/src/nes/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,8 +675,12 @@ impl Renderer {
let adapter_info = painter.render_state().map(|state| state.adapter.get_info());
if let Some(info) = adapter_info {
debug!(
"created new painter for {}. Backend: {}",
info.name,
"created new painter for adapter: `{}`. backend: `{}`",
if info.name.is_empty() {
"unknown"
} else {
&info.name
},
info.backend.to_str()
);
} else {
Expand Down Expand Up @@ -1125,14 +1129,15 @@ impl Renderer {
let Viewport {
window: Some(window),
egui_state: Some(egui_state),
screenshot_requested,
..
} = viewport
else {
return Ok(());
};

let clipped_primitives = self.ctx.tessellate(output.shapes, output.pixels_per_point);
let screenshot_requested = std::mem::take(&mut viewport.screenshot_requested);
let screenshot_requested = std::mem::take(screenshot_requested);
painter.borrow_mut().paint_and_update_textures(
viewport_id,
output.pixels_per_point,
Expand Down
14 changes: 13 additions & 1 deletion tetanes/src/sys/logging/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::panic;
use tracing_subscriber::{
fmt::{self, format::Pretty},
layer::SubscriberExt,
Expand All @@ -12,7 +13,18 @@ pub fn init_impl<S>(registry: S) -> (impl SubscriberInitExt, Log)
where
S: SubscriberExt + for<'a> LookupSpan<'a> + Sync + Send,
{
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
panic::set_hook(Box::new(|info: &panic::PanicInfo<'_>| {
let error_div = web_sys::window()
.and_then(|window| window.document())
.and_then(|document| document.get_element_by_id("error"));
if let Some(error_div) = error_div {
if let Err(err) = error_div.class_list().remove_1("hidden") {
tracing::error!("{err:?}")
}
}

console_error_panic_hook::hook(info);
}));

let console_layer = fmt::layer()
.compact()
Expand Down
6 changes: 1 addition & 5 deletions tetanes/src/sys/platform/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,8 @@ impl Initialize for Running {
on_resize.forget();

if let Some(status) = document.get_element_by_id(html_ids::LOADING_STATUS) {
tracing::info!(
"removing hidden class from loading status: {}",
html_ids::LOADING_STATUS
);
if let Err(err) = status.class_list().add_1("hidden") {
tracing::info!("{err:?}");
tracing::error!("{err:?}");
on_error(&self.tx, err);
}
}
Expand Down

0 comments on commit 507d9a0

Please sign in to comment.