Skip to content

Commit 9ae6c3f

Browse files
committed
Moved spawn_local to lib file. Updated server_integration example to match new fetch API.
1 parent 8d31656 commit 9ae6c3f

File tree

15 files changed

+48
-51
lines changed

15 files changed

+48
-51
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "seed"
3-
version = "0.2.1"
3+
version = "0.2.2"
44
description = "A Rust framework for creating web apps, using WebAssembly"
55
authors = ["DavidOConnor <[email protected]>"]
66
license = "MIT"
@@ -14,7 +14,6 @@ edition = "2018"
1414

1515
[lib]
1616
crate-type = ["cdylib", "rlib"]
17-
#proc-macro = true
1817

1918
[dev-dependencies]
2019
wasm-bindgen-test = "^0.2.29" # NOTE: keep in sync with wasm-bindgen version

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
**A Rust framework for creating web apps**
44

5+
6+
[![Build Status](https://travis-ci.org/rustwasm/wasm-bindgen.svg?branch=master)](https://travis-ci.org/David-OConnor/seed)
57
[![](https://meritbadge.herokuapp.com/seed)](https://crates.io/crates/seed)
68
[![](https://img.shields.io/crates/d/seed.svg)](https://crates.io/crates/seed)
79
[![API Documentation on docs.rs](https://docs.rs/seed/badge.svg)](https://docs.rs/seed)

examples/counter/pkg/counter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ __exports.__wbindgen_cb_drop = function(i) {
511511
return 0;
512512
};
513513

514-
__exports.__wbindgen_closure_wrapper594 = function(a, b, _ignored) {
514+
__exports.__wbindgen_closure_wrapper592 = function(a, b, _ignored) {
515515
const f = wasm.__wbg_function_table.get(2);
516516
const d = wasm.__wbg_function_table.get(3);
517517
const cb = function(arg0) {

examples/counter/pkg/counter_bg.wasm

-329 Bytes
Binary file not shown.

examples/server_integration/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ authors = ["Your Name <[email protected]>"]
55
edition = "2018"
66

77
[dependencies]
8+
futures = "^0.1.20"
89
rocket = "^0.4.0-rc.1"
910
serde_json = "^1.0.33"
1011

examples/server_integration/frontend/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ authors = ["Your Name <[email protected]>"]
55
edition = "2018"
66

77
[lib]
8-
crate-type = ["cdylib"]
8+
crate-type = ["cdylib", "rlib"]
99

1010
[dependencies]
11-
seed = "^0.2.1"
11+
futures = "^0.1.20"
12+
seed = { path = "../../../"}
1213
wasm-bindgen = "^0.2.29"
1314
web-sys = "^0.3.6"
1415
shared = { path = "../shared"}

examples/server_integration/frontend/src/lib.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#[macro_use]
22
extern crate seed;
3+
use seed::{Request, Method, spawn_local};
34
use seed::prelude::*;
45

6+
use futures::Future;
7+
58
use shared::Data;
69

710

11+
812
// Model
913

1014
#[derive(Clone)]
@@ -20,14 +24,15 @@ impl Default for Model {
2024
}
2125
}
2226

23-
fn get_data(state: seed::App<Msg, Model>) {
27+
fn get_data(state: seed::App<Msg, Model>) -> impl Future<Item = (), Error = JsValue> {
2428
let url = "http://localhost:8001/data";
25-
let callback = move |json: JsValue| {
26-
let data: Data = json.into_serde().unwrap();
27-
state.update(Msg::Replace(data));
28-
};
2929

30-
seed::get(url, None, Box::new(callback));
30+
Request::new(url)
31+
.method(Method::Get)
32+
.fetch_json()
33+
.map(move |json| {
34+
state.update(Msg::Replace(json));
35+
})
3136
}
3237

3338

@@ -42,7 +47,7 @@ enum Msg {
4247
fn update(msg: Msg, model: Model) -> Model {
4348
match msg {
4449
Msg::GetData(state) => {
45-
get_data(state);
50+
spawn_local(get_data(state));
4651
model
4752
},
4853
Msg::Replace(data) => Model {data}

examples/server_interaction/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,4 @@ web-sys = "^0.3.6"
1515
serde = { version = "^1.0.83", features = ['derive'] }
1616
serde_json = "^1.0.33"
1717

18-
# todo temp
1918
futures = "^0.1.20"
20-
wasm-bindgen-futures = "0.3.6"

examples/server_interaction/src/lib.rs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,12 @@
44
#[macro_use]
55
extern crate seed;
66
use seed::prelude::*;
7-
use seed::{Request, Method};
8-
use wasm_bindgen_futures::future_to_promise;
7+
use seed::{Request, Method, spawn_local};
98
use serde::{Serialize, Deserialize};
109

11-
// todo
12-
use wasm_bindgen_futures;
1310
use futures::Future;
1411

1512

16-
17-
fn spawn_local<F>(future: F) where F: Future<Item = (), Error = JsValue> + 'static {
18-
future_to_promise(future.map(|_| JsValue::UNDEFINED).map_err(|err| {
19-
web_sys::console::error_1(&err);
20-
err
21-
}));
22-
}
23-
24-
2513
// Model
2614

2715
#[derive(Clone, Serialize, Deserialize)]
@@ -56,14 +44,11 @@ struct ServerResponse {
5644

5745
#[derive(Clone)]
5846
struct Model {
59-
// data: Data,
6047
data: Branch,
6148
}
6249

63-
6450
fn get_data(state: seed::App<Msg, Model>) -> impl Future<Item = (), Error = JsValue> {
6551
let url = "https://api.github.com/repos/david-oconnor/seed/branches/master";
66-
// let url = "https://seed-example.herokuapp.com/data";
6752

6853
Request::new(url)
6954
.method(Method::Get)
@@ -74,7 +59,6 @@ fn get_data(state: seed::App<Msg, Model>) -> impl Future<Item = (), Error = JsVa
7459
}
7560

7661
fn send() -> impl Future<Item = (), Error = JsValue> {
77-
// let url = "http://127.0.0.1:8001/api/contact";
7862
let url = "https://infinitea.herokuapp.com/api/contact";
7963

8064
let message = Message {
@@ -98,7 +82,6 @@ impl Default for Model {
9882
fn default() -> Self {
9983
Self {
10084
data: Branch{ name: "Test".into(), commit: Commit{sha: "123".into()} }
101-
// data: Data {val: 0, text: "".into()}
10285
}
10386
}
10487
}
@@ -108,7 +91,6 @@ impl Default for Model {
10891

10992
#[derive(Clone)]
11093
enum Msg {
111-
// Replace(Data),
11294
Replace(Branch),
11395
GetData(seed::App<Msg, Model>),
11496
Send,
@@ -117,8 +99,10 @@ enum Msg {
11799
fn update(msg: Msg, model: Model) -> Model {
118100
match msg {
119101
Msg::Replace(data) => Model {data},
120-
Msg::GetData(app) => {
121-
spawn_local(get_data(app));
102+
// Msg::GetData is unused in this example, but could be used when
103+
// updating state from an event.
104+
Msg::GetData(state) => {
105+
spawn_local(get_data(state));
122106
model
123107
},
124108
Msg::Send => {

examples/todomvc/pkg/todomvc.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -599,9 +599,9 @@ __exports.__wbindgen_cb_drop = function(i) {
599599
return 0;
600600
};
601601

602-
__exports.__wbindgen_closure_wrapper408 = function(a, b, _ignored) {
603-
const f = wasm.__wbg_function_table.get(20);
604-
const d = wasm.__wbg_function_table.get(21);
602+
__exports.__wbindgen_closure_wrapper353 = function(a, b, _ignored) {
603+
const f = wasm.__wbg_function_table.get(2);
604+
const d = wasm.__wbg_function_table.get(3);
605605
const cb = function(arg0) {
606606
this.cnt++;
607607
let a = this.a;

examples/todomvc/pkg/todomvc_bg.wasm

-362 Bytes
Binary file not shown.

src/dom_types.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -950,8 +950,14 @@ pub fn will_unmount(mut actions: impl FnMut(&web_sys::Element) + 'static) -> Wil
950950

951951
#[cfg(test)]
952952
pub mod tests {
953+
#![feature(custom_attribute)]
954+
use wasm_bindgen_test::wasm_bindgen_test_configure;
955+
wasm_bindgen_test_configure!(run_in_browser);
956+
957+
use wasm_bindgen_test::*;
953958
use super::*;
954959

960+
955961
use crate as seed; // required for macros to work.
956962
use crate::prelude::*;
957963
use crate::{div};
@@ -962,8 +968,7 @@ pub mod tests {
962968
}
963969

964970
// todo 'cannot call wasm-bindgen imported functions on non-wasm targets' error
965-
#[test]
966-
#[wasm_bindgen] // todo ?
971+
#[wasm_bindgen_test]
967972
pub fn single() {
968973
let expected = "<div>\ntest\n</div>";
969974

src/fetch.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use futures::{Future, Poll};
99
use wasm_bindgen::JsValue;
1010
use wasm_bindgen_futures::JsFuture;
11+
use wasm_bindgen_futures::future_to_promise;
1112
use web_sys;
1213

1314
use serde::Serialize;
@@ -224,3 +225,10 @@ impl<A> Drop for AbortFuture<A> {
224225
self.controller.abort();
225226
}
226227
}
228+
229+
pub fn spawn_local<F>(future: F) where F: Future<Item = (), Error = JsValue> + 'static {
230+
future_to_promise(future.map(|_| JsValue::UNDEFINED).map_err(|err| {
231+
web_sys::console::error_1(&err);
232+
err
233+
}));
234+
}

src/lib.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,16 @@ use wasm_bindgen::{closure::Closure, JsCast};
1717

1818
//// todos:
1919
// todo local storage
20-
// todo maybe?? High-level css-grid and flex api?
20+
// todo High-level css-grid and flex api?
2121
// todo Async conflicts with events stepping on each other ?
22-
// todo keyed elements??
23-
// todo: Msg as copy type?
22+
// todo keyed elements?
2423

2524
pub use crate::{
26-
// dom_types::{did_mount, did_update, will_unmount}, // todo: Here or in prelude?
2725
dom_types::{Listener},
28-
fetch::{Method, Request},
26+
fetch::{Method, Request, spawn_local},
2927
websys_bridge::{to_input, to_kbevent, to_mouse_event, to_select, to_textarea, to_html_el},
3028
routing::push_route,
31-
vdom::{App, run} // todo app temp?
29+
vdom::{App, run}
3230
};
3331

3432
/// Create an element flagged in a way that it will not be rendered. Useful

src/vdom.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,6 @@ pub mod tests {
537537

538538
#[wasm_bindgen_test]
539539
fn el_added() {
540-
// todo macros not working here.
541540
let old_vdom: El<Msg> = div![ "text", vec![
542541
li![ "child1" ],
543542
] ];
@@ -568,7 +567,6 @@ pub mod tests {
568567

569568
assert_eq!(2 + 2, 4);
570569
}
571-
<<<<<<< HEAD
572570

573571
#[test]
574572
fn el_removed() {
@@ -578,6 +576,4 @@ pub mod tests {
578576
fn el_changed() {
579577
}
580578
}
581-
=======
582-
}
583-
>>>>>>> d98c94cc9894e19fc257751e6b925d3028a0886c
579+

0 commit comments

Comments
 (0)