Skip to content

Commit f7ca721

Browse files
author
Ayush Kumar Mishra
committed
Minor refactoring
1 parent 9d46ae8 commit f7ca721

File tree

2 files changed

+12
-30
lines changed

2 files changed

+12
-30
lines changed

examples/weather_report/Cargo.toml

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# You must change these to your own details.
21
[package]
32
name = "rust-webassembly-weather-reports"
43
description = "Weather Information System- Get the mood of your city on one click using Rust and Webassembly"
@@ -11,34 +10,14 @@ edition = "2018"
1110
[lib]
1211
crate-type = ["cdylib"]
1312

14-
[profile.release]
15-
# This makes the compiled code faster and smaller, but it makes compiling slower,
16-
# so it's only enabled in release mode.
17-
lto = true
18-
19-
[features]
20-
# If you uncomment this line, it will enable `wee_alloc`:
21-
#default = ["wee_alloc"]
22-
2313
[dependencies]
2414
chrono = "0.4.11"
2515
reqwest = "0.10.6"
2616
wasm-bindgen-futures = "0.4.1"
2717
json= "*"
28-
# The `wasm-bindgen` crate provides the bare minimum functionality needed
29-
# to interact with JavaScript.
3018
wasm-bindgen = "0.2.63"
3119
gloo = "0.2.1"
3220

33-
# The `web-sys` crate allows you to interact with the various browser APIs,
34-
# like the DOM.
3521
[dependencies.web-sys]
3622
version = "0.3.40"
37-
features = [
38-
'Document',
39-
'Element',
40-
'HtmlElement',
41-
'Node',
42-
'Window',
43-
'console'
44-
]
23+
features = ["Document", "Element", "HtmlElement", "Window"]

examples/weather_report/src/lib.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ extern "C" {
1919
fn initialize(lat: f64, lon: f64);
2020
}
2121

22-
#[wasm_bindgen]
23-
extern "C" {
24-
fn alert(s: &str);
25-
}
26-
2722
// This is like the `main` function, except for JavaScript.
2823
#[wasm_bindgen(start)]
2924
pub fn main_js() -> Result<(), JsValue> {
@@ -36,7 +31,11 @@ pub fn main_js() -> Result<(), JsValue> {
3631
let input_box = create_input_box(&document);
3732
search_div.append_child(&input_box)?;
3833
let submit_box = create_submit_box(&document);
39-
let temp_div = create_div(&document, "tempDetail", "ReportStyles-mainContainer col-md-12 maincontainer");
34+
let temp_div = create_div(
35+
&document,
36+
"tempDetail",
37+
"ReportStyles-mainContainer col-md-12 maincontainer",
38+
);
4039
let first_div = document.create_element("div")?;
4140
let second_div = create_div(&document, "second_div", "col-md-6");
4241
let third_div = create_div(&document, "third_div", "ReportStyles-innerDiv");
@@ -191,11 +190,15 @@ pub fn main_js() -> Result<(), JsValue> {
191190
});
192191
});
193192

193+
// When a Closure is dropped it will invalidate the associated JS closure.
194+
// Here we want JS callback to be alive for the entire duration of the program.
195+
// So we used `forget` leak this instance of Closure.
196+
// It should be used sparingly to ensure the memory leak doesn't affect the program too much.
194197
on_click.forget();
195198
Ok(())
196199
}
197200

198-
fn create_div(document: &Document, id: &str, class:&str) -> Element {
201+
fn create_div(document: &Document, id: &str, class: &str) -> Element {
199202
let div = document.create_element("div").unwrap();
200203
div.set_id(id);
201204
div.set_class_name(class);
@@ -226,7 +229,7 @@ fn create_input_box(document: &Document) -> Element {
226229
// Get response from weather api
227230
async fn get_response(location: &str) -> JsonValue {
228231
let url1 = "http://api.openweathermap.org/data/2.5/weather?q=";
229-
let url2 = "&appid=<apikey>";
232+
let url2 = "&appid=<apiKey>";
230233

231234
let url = [url1, location, url2].concat();
232235

0 commit comments

Comments
 (0)