Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/DenisKolodin/yew into typ…
Browse files Browse the repository at this point in the history
…ed-fetch
  • Loading branch information
gabisurita committed Jan 9, 2018
2 parents 3f5a0a7 + c109360 commit 8d2ea56
Show file tree
Hide file tree
Showing 26 changed files with 364 additions and 145 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ language: rust

dist: trusty
sudo: false
addons:
chrome: stable

rust:
- stable
Expand Down
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "yew"
version = "0.2.0"
version = "0.3.0"
authors = ["Denis Kolodin <deniskolodin@gmail.com>"]
repository = "https://github.com/DenisKolodin/yew"
homepage = "https://github.com/DenisKolodin/yew"
Expand All @@ -16,3 +16,7 @@ http = "0.1"
serde = "1"
serde_json = "1"
stdweb = "0.3"

[features]
default = []
web_test = []
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ fn update(context: &mut Context, model: &mut Model, msg: Msg) {

### Easy-to-use data conversion and destructuring

You could simply choose and use a format of data to store/send and resore/receive it.
You could simply choose and use a format of data to store/send and restore/receive it.

Supported: `JSON`

Expand Down Expand Up @@ -164,7 +164,7 @@ fn update(context: &mut Context, model: &mut Model, msg: Msg) {
Clone or download this repository.

There are seven examples to check how it works:
[counter], [timer], [todomvc], [game_of_life], [crm], [dashboard], [npm_and_rest].
[counter], [timer], [todomvc], [game_of_life], [crm], [dashboard] and [npm_and_rest].

To run them you need to have [cargo-web] installed as well as a suitable target
for the Rust compiler to generate web output. By default cargo-web uses
Expand All @@ -182,7 +182,6 @@ To run an optimised build instead of a debug build use:

$ cargo web start --release


[counter]: examples/counter
[timer]: examples/timer
[todomvc]: examples/todomvc
Expand Down
5 changes: 3 additions & 2 deletions ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,21 @@ cargo install cargo-web -f
if [ "$TARGET" = "asmjs-unknown-emscripten" ]; then
rustup target add asmjs-unknown-emscripten
export CARGO_WEB_ARGS="--target-asmjs-emscripten"
cargo web test --features web_test $CARGO_WEB_ARGS
fi

if [ "$TARGET" = "wasm32-unknown-emscripten" ]; then
rustup target add wasm32-unknown-emscripten
export CARGO_WEB_ARGS="--target-webasm-emscripten"
cargo web test --features web_test $CARGO_WEB_ARGS
fi

if [ "$TARGET" = "wasm32-unknown-unknown" ]; then
rustup target add wasm32-unknown-unknown
export CARGO_WEB_ARGS="--target-webasm"
cargo web test --nodejs $CARGO_WEB_ARGS
fi

cargo web test --nodejs $CARGO_WEB_ARGS

check_example() {
echo "Checking example [$1]"
cd $1
Expand Down
6 changes: 4 additions & 2 deletions examples/counter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn update(context: &mut Context, model: &mut Model, msg: Msg) {
}
Msg::Decrement => {
model.value = model.value - 1;
context.console.log("munis one");
context.console.log("minus one");
}
Msg::Bulk(list) => {
for msg in list {
Expand All @@ -53,12 +53,14 @@ fn view(model: &Model) -> Html<Msg> {
}

fn main() {
yew::initialize();
let mut app = App::new();
let context = Context {
console: ConsoleService,
};
let model = Model {
value: 0,
};
app.run(context, model, update, view);
app.mount(context, model, update, view);
yew::run_loop();
}
6 changes: 4 additions & 2 deletions examples/crm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn update(context: &mut Context, model: &mut Model, msg: Msg) {
new_scene = Some(Scene::ClientsList);
}
unexpected => {
panic!("Unexpected message diring new client editing: {:?}", unexpected);
panic!("Unexpected message during new client editing: {:?}", unexpected);
}
}
}
Expand Down Expand Up @@ -200,6 +200,7 @@ fn view_last_name_input(client: &Client) -> Html<Msg> {
}

fn main() {
yew::initialize();
let mut app = App::new();
let mut context = Context {
storage: StorageService::new(Scope::Local),
Expand All @@ -209,5 +210,6 @@ fn main() {
let scene = Scene::Initialization;
let model = Model { database, scene };
app.sender().send(Msg::SwitchTo(Scene::ClientsList));
app.run(context, model, update, view);
app.mount(context, model, update, view);
yew::run_loop();
}
2 changes: 1 addition & 1 deletion examples/dashboard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Build and run the client

Enter to `client` folder and run:
Enter the `client` folder and run:

```sh
cargo web start
Expand Down
4 changes: 3 additions & 1 deletion examples/dashboard/client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ fn view_data(model: &Model) -> Html<Msg> {
}

fn main() {
yew::initialize();
let mut app = App::new();
let context = Context {
web: FetchService::new(app.sender()),
Expand All @@ -159,5 +160,6 @@ fn main() {
data: None,
ws: None,
};
app.run(context, model, update, view);
app.mount(context, model, update, view);
yew::run_loop();
}
4 changes: 3 additions & 1 deletion examples/game_of_life/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ fn view_cellule((idx, cellule): (usize, &Cellule)) -> Html<Msg> {
}

fn main() {
yew::initialize();
let mut app = App::new();
let context = Context {
interval: IntervalService::new(app.sender()),
Expand All @@ -225,5 +226,6 @@ fn main() {
cellules_height: 40,
job : None
};
app.run(context, gof, update, view);
app.mount(context, gof, update, view);
yew::run_loop();
}
8 changes: 8 additions & 0 deletions examples/mount_point/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "mount_point"
version = "0.1.0"
authors = ["Ben Berman <ben@standardbots.com>"]

[dependencies]
yew = { path = "../.." }
stdweb = "0.3"
67 changes: 67 additions & 0 deletions examples/mount_point/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#[macro_use]
extern crate yew;
#[macro_use]
extern crate stdweb;

use yew::html::{App, Html, InputData};
use stdweb::web::{IElement, document, INode};

struct Context {}

struct Model {
name: String,
}

enum Msg {
UpdateName(String),
}

fn update(_: &mut Context, model: &mut Model, msg: Msg) {
match msg {
Msg::UpdateName(new_name) => {
model.name = new_name;
}
}
}

fn view(model: &Model) -> Html<Msg> {
html! {
<div>
<input value=&model.name, oninput=|e: InputData| Msg::UpdateName(e.value), />
<p>{ model.name.chars().rev().collect::<String>() }</p>
</div>
}
}

fn main() {
yew::initialize();
let body = document().query_selector("body").unwrap();

// This canvas won't be overwritten by yew!
let canvas = document().create_element("canvas");
body.append_child(&canvas);

js! {
const canvas = document.querySelector("canvas");
canvas.width = 100;
canvas.height = 100;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "green";
ctx.fillRect(10, 10, 50, 50);
};

let mount_class = "mount-point";
let mount_point = document().create_element("div");
mount_point.class_list().add(mount_class);
body.append_child(&mount_point);

let mut app = App::new();
let context = Context {};
let model = Model {
name: "Reversed".to_owned(),
};

let mount_point = format!(".{}", mount_class);
app.mount_to(&mount_point, context, model, update, view);
yew::run_loop();
}
2 changes: 1 addition & 1 deletion examples/npm_and_rest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ authors = ["Denis Kolodin <deniskolodin@gmail.com>"]
serde = "1"
serde_derive = "1"
yew = { path = "../.." }
stdweb = "0.2"
stdweb = "0.3"
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ pub struct Entry {
hash: String,
request_hash: String,
profile_url: String,
preffered_username: String,
preferred_username: String,
}

pub struct GavatarService<MSG> {
pub struct GravatarService<MSG> {
web: FetchService<MSG>,
}

impl<MSG: 'static> GavatarService<MSG> {
impl<MSG: 'static> GravatarService<MSG> {
pub fn new(sender: AppSender<MSG>) -> Self {
Self {
web: FetchService::new(sender),
Expand All @@ -34,8 +34,7 @@ impl<MSG: 'static> GavatarService<MSG> {
{
let url = format!("https://gravatar.com/{}", hash);
self.web.fetch(
Request::get(url.as_str()).body(Nothing)
.unwrap(),
Request::get(url.as_str()).body(Nothing).unwrap(),
move |response| {
let (_, Json(data)) = response.into_parts();
listener(data)
Expand Down
28 changes: 15 additions & 13 deletions examples/npm_and_rest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ extern crate yew;
use yew::html::*;

// Own services implementation
mod gavatar;
use gavatar::{GavatarService, Profile};
mod gravatar;
use gravatar::{GravatarService, Profile};
mod ccxt;
use ccxt::CcxtService;

struct Context {
gavatar: GavatarService<Msg>,
gravatar: GravatarService<Msg>,
ccxt: CcxtService,
}

Expand All @@ -24,21 +24,21 @@ struct Model {
}

enum Msg {
Gavatar,
GavatarReady(Result<Profile, ()>),
Gravatar,
GravatarReady(Result<Profile, ()>),
Exchanges,
}

fn update(context: &mut Context, model: &mut Model, msg: Msg) {
match msg {
Msg::Gavatar => {
context.gavatar.profile("205e460b479e2e5b48aec07710c08d50", Msg::GavatarReady);
Msg::Gravatar => {
context.gravatar.profile("205e460b479e2e5b48aec07710c08d50", Msg::GravatarReady);
}
Msg::GavatarReady(Ok(profile)) => {
Msg::GravatarReady(Ok(profile)) => {
model.profile = Some(profile);
}
Msg::GavatarReady(Err(_)) => {
// Can't load gavatar profile
Msg::GravatarReady(Err(_)) => {
// Can't load gravatar profile
}
Msg::Exchanges => {
model.exchanges = context.ccxt.exchanges();
Expand All @@ -53,7 +53,7 @@ fn view(model: &Model) -> Html<Msg> {
html! {
<div>
<button onclick=|_| Msg::Exchanges,>{ "Get Exchanges" }</button>
<button onclick=|_| Msg::Gavatar,>{ "Get Gavatar" }</button>
<button onclick=|_| Msg::Gravatar,>{ "Get Gravatar" }</button>
<ul>
{ for model.exchanges.iter().map(view_exchange) }
</ul>
Expand All @@ -62,14 +62,16 @@ fn view(model: &Model) -> Html<Msg> {
}

fn main() {
yew::initialize();
let mut app = App::new();
let context = Context {
gavatar: GavatarService::new(app.sender()),
gravatar: GravatarService::new(app.sender()),
ccxt: CcxtService::new(),
};
let model = Model {
profile: None,
exchanges: Vec::new(),
};
app.run(context, model, update, view);
app.mount(context, model, update, view);
yew::run_loop();
}
6 changes: 4 additions & 2 deletions examples/timer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn update(context: &mut Context, model: &mut Model, msg: Msg) {
model.job = Some(Box::new(handle));
model.messages.clear();
context.console.clear();
model.messages.push("Timer started!!");
model.messages.push("Timer started!");
context.console.time_named("Timer");
}
Msg::StartInterval => {
Expand Down Expand Up @@ -86,6 +86,7 @@ fn view(model: &Model) -> Html<Msg> {
}

fn main() {
yew::initialize();
let mut app = App::new();
let context = Context {
interval: IntervalService::new(app.sender()),
Expand All @@ -96,5 +97,6 @@ fn main() {
job: None,
messages: Vec::new(),
};
app.run(context, model, update, view);
app.mount(context, model, update, view);
yew::run_loop();
}
2 changes: 2 additions & 0 deletions examples/todomvc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ authors = ["Denis Kolodin <deniskolodin@gmail.com>"]
[dependencies]
strum = "0.8.0"
strum_macros = "0.8.0"
serde = "1"
serde_derive = "1"
yew = { path = "../.." }
6 changes: 6 additions & 0 deletions examples/todomvc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## Yew TodoMVC Demo

This it an implementationt of [TodoMVC](http://todomvc.com/) app.

Unlike other implementations, this stores the full state of the model in the storage,
including: all entries, entered text and choosed filter.

0 comments on commit 8d2ea56

Please sign in to comment.