Skip to content

Commit

Permalink
Timer trait: add current_split_index
Browse files Browse the repository at this point in the history
WebEventSink: current_split_index

web-sys, js-sys: 0.3.65

0.3.65 is when web_sys starts re-exporting js_sys, which web_event_sink relies on for use web_sys::js_sys
  • Loading branch information
AlexKnauth committed Jun 20, 2024
1 parent 4ebb4bb commit 45fb99d
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ log = { version = "0.4.14", default-features = false, optional = true }

[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dependencies]
# WebAssembly in the Web
js-sys = { version = "0.3.55", optional = true }
js-sys = { version = "0.3.65", optional = true }
wasm-bindgen = { version = "0.2.78", optional = true }
wasm-bindgen-futures = { version = "0.4.28", optional = true }
web-sys = { version = "0.3.28", default-features = false, features = [
web-sys = { version = "0.3.65", default-features = false, features = [
"Document",
"Performance",
"VisibilityState",
Expand Down
2 changes: 1 addition & 1 deletion capi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ time = { version = "0.3.4", default-features = false, features = ["formatting"]
simdutf8 = { git = "https://github.com/CryZe/simdutf8", branch = "wasm-ub-panic", default-features = false }

wasm-bindgen = { version = "0.2.78", optional = true }
web-sys = { version = "0.3.28", optional = true }
web-sys = { version = "0.3.65", optional = true }

[features]
default = ["image-shrinking"]
Expand Down
15 changes: 15 additions & 0 deletions capi/src/web_event_sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub struct WebEventSink {
resume_game_time: Option<Function>,
set_custom_variable: Option<Function>,
current_phase: Function,
current_split_index: Option<Function>,
}

#[wasm_bindgen]
Expand All @@ -64,6 +65,7 @@ impl WebEventSink {
resume_game_time: get_func(&obj, "resumeGameTime"),
set_custom_variable: get_func(&obj, "setCustomVariable"),
current_phase: get_func(&obj, "currentPhase").unwrap(),
current_split_index: get_func(&obj, "currentSplitIndex"),
obj,
}
}
Expand Down Expand Up @@ -216,4 +218,17 @@ impl TimerQuery for WebEventSink {
_ => panic!("Unknown TimerPhase"),
}
}
fn current_split_index(&self) -> Option<usize> {
let i = self
.current_split_index
.as_ref()?
.call0(&self.obj)
.ok()?
.as_f64()?;
if i >= 0.0 {
Some(i as usize)
} else {
None
}
}
}
6 changes: 6 additions & 0 deletions crates/livesplit-auto-splitting/src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ pub trait Timer {
fn undo_split(&mut self);
/// Resets the timer.
fn reset(&mut self);
/// Accesses the index of the split the attempt is currently on. If there's
/// no attempt in progress, `None` is returned instead. This returns an
/// index that is equal to the amount of segments when the attempt is
/// finished, but has not been reset. So you need to be careful when using
/// this value for indexing.
fn current_split_index(&self) -> Option<usize>;
/// Sets the game time.
fn set_game_time(&mut self, time: time::Duration);
/// Pauses the game time. This does not pause the timer, only the automatic
Expand Down
3 changes: 3 additions & 0 deletions crates/livesplit-auto-splitting/tests/sandboxing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ impl Timer for DummyTimer {
fn skip_split(&mut self) {}
fn undo_split(&mut self) {}
fn reset(&mut self) {}
fn current_split_index(&self) -> Option<usize> {
None
}
fn set_game_time(&mut self, _time: time::Duration) {}
fn pause_game_time(&mut self) {}
fn resume_game_time(&mut self) {}
Expand Down
4 changes: 2 additions & 2 deletions crates/livesplit-hotkey/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ x11-dl = { version = "2.20.0", optional = true }

[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dependencies]
wasm-bindgen = { version = "0.2.54", optional = true }
web-sys = { version = "0.3.28", default-features = false, features = [
web-sys = { version = "0.3.65", default-features = false, features = [
"EventTarget",
"Gamepad",
"GamepadButton",
"KeyboardEvent",
"Navigator",
"Window",
], optional = true }
js-sys = { version = "0.3.28", default-features = false, optional = true }
js-sys = { version = "0.3.65", default-features = false, optional = true }

[dependencies]
cfg-if = "1.0.0"
Expand Down
4 changes: 4 additions & 0 deletions src/auto_splitting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,10 @@ impl<E: event::Sink + TimerQuery> AutoSplitTimer for Timer<E> {
self.0.reset(None)
}

fn current_split_index(&self) -> Option<usize> {
self.0.current_split_index()
}

fn set_game_time(&mut self, time: time::Duration) {
self.0.set_game_time(time.into());
}
Expand Down
12 changes: 12 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ pub trait Sink {
pub trait TimerQuery {
/// Returns the current Timer Phase.
fn current_phase(&self) -> TimerPhase;
/// Accesses the index of the split the attempt is currently on. If there's
/// no attempt in progress, `None` is returned instead. This returns an
/// index that is equal to the amount of segments when the attempt is
/// finished, but has not been reset. So you need to be careful when using
/// this value for indexing.
fn current_split_index(&self) -> Option<usize>;
}

#[cfg(feature = "std")]
Expand Down Expand Up @@ -166,6 +172,9 @@ impl TimerQuery for crate::SharedTimer {
fn current_phase(&self) -> TimerPhase {
self.read().unwrap().current_phase()
}
fn current_split_index(&self) -> Option<usize> {
self.read().unwrap().current_split_index()
}
}

impl<T: Sink + ?Sized> Sink for Arc<T> {
Expand Down Expand Up @@ -242,4 +251,7 @@ impl<T: TimerQuery + ?Sized> TimerQuery for Arc<T> {
fn current_phase(&self) -> TimerPhase {
TimerQuery::current_phase(&**self)
}
fn current_split_index(&self) -> Option<usize> {
TimerQuery::current_split_index(&**self)
}
}

0 comments on commit 45fb99d

Please sign in to comment.