Skip to content

Commit

Permalink
add ConsoleService
Browse files Browse the repository at this point in the history
  • Loading branch information
雷东升 committed Dec 29, 2017
1 parent 0df9414 commit 259e45f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
7 changes: 3 additions & 4 deletions examples/timer/src/main.rs
Expand Up @@ -3,7 +3,7 @@ extern crate yew;

use std::time::Duration;
use yew::html::*;
use yew::services::Task;
use yew::services::{Task, Level};
use yew::services::timeout::TimeoutService;
use yew::services::interval::IntervalService;
use yew::services::console::ConsoleService;
Expand All @@ -28,14 +28,13 @@ fn update(context: &mut Context<Msg>, model: &mut Model, msg: Msg) {
model.job = Some(Box::new(handle));
model.messages.clear();
model.messages.push("Timer started!!");
context.console_log("Timer started!!");
}
Msg::StartInterval => {
let handle = context.interval(Duration::from_secs(1), || Msg::Tick);
model.job = Some(Box::new(handle));
model.messages.clear();
model.messages.push("Interval started!");
context.console_log("Interval started!");
model.messages.push("Inerval started!");
context.console(Level::Log, "Interval started!");
}
Msg::Cancel => {
if let Some(mut task) = model.job.take() {
Expand Down
12 changes: 9 additions & 3 deletions src/services/console.rs
@@ -1,11 +1,17 @@
use html::Context;
use super::{Level};

pub trait ConsoleService {
fn console_log(&mut self, message: &str);
fn console(&mut self, level: Level, message: &str);
}

impl<MSG: 'static> ConsoleService for Context<MSG> {
fn console_log(&mut self, message: &str) {
js! { console.log(@{message}); }
fn console(&mut self, level: Level, message: &str) {
match level {
Level::Log => { js! { console.log(@{message}); } },
Level::Warn => { js! { console.warn(@{message}); } },
Level::Error => { js! { console.error(@{message}); } },
_ => { println!("unknown Level."); },
}
}
}
6 changes: 6 additions & 0 deletions src/services/mod.rs
Expand Up @@ -6,6 +6,12 @@ pub mod console;

use std::time::Duration;

pub enum Level {
Log,
Warn,
Error,
}

pub trait Task {
fn cancel(&mut self);
}
Expand Down

0 comments on commit 259e45f

Please sign in to comment.