Skip to content

Commit

Permalink
fix: serialize ScoreState as Object instead of Map (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxOhn authored Apr 16, 2024
1 parent 173de2e commit a13720f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
24 changes: 14 additions & 10 deletions src/score_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,19 @@ impl JsScoreState {

impl From<ScoreState> for JsScoreState {
fn from(state: ScoreState) -> Self {
let map = js_sys::Map::new();
map.set(&util::static_str_to_js("maxCombo"), &state.max_combo.into());
map.set(&util::static_str_to_js("nGeki"), &state.n_geki.into());
map.set(&util::static_str_to_js("nKatu"), &state.n_katu.into());
map.set(&util::static_str_to_js("n300"), &state.n300.into());
map.set(&util::static_str_to_js("n100"), &state.n100.into());
map.set(&util::static_str_to_js("n50"), &state.n50.into());
map.set(&util::static_str_to_js("misses"), &state.misses.into());

JsValue::from(map).into()
let obj = js_sys::Object::new();
let obj_as_ext = obj.unchecked_ref::<util::ObjectExt>();

let set = |key, value: u32| obj_as_ext.set(util::static_str_to_js(key), value.into());

set("maxCombo", state.max_combo);
set("nGeki", state.n_geki);
set("nKatu", state.n_katu);
set("n300", state.n300);
set("n100", state.n100);
set("n50", state.n50);
set("misses", state.misses);

JsValue::from(obj).into()
}
}
2 changes: 1 addition & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extern "C" {
pub fn get_with_ref_key(this: &ObjectExt, key: &JsString) -> JsValue;

#[wasm_bindgen(method, indexing_setter)]
fn set(this: &ObjectExt, key: JsString, value: JsValue);
pub fn set(this: &ObjectExt, key: JsString, value: JsValue);
}

/// Store converted strings and return clones instead of converting them
Expand Down

0 comments on commit a13720f

Please sign in to comment.