Skip to content

Commit

Permalink
fix: more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanceras committed Apr 2, 2024
1 parent 5a49e16 commit e1b9a47
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 84 deletions.
8 changes: 3 additions & 5 deletions crates/core/src/dom/component/stateful_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,22 @@ impl<MSG> PartialEq for StatefulModel<MSG> {
}
}

/*
impl<COMP> Application for COMP
where
COMP: Component<XMSG = ()> + StatefulComponent + 'static,
{
type MSG = COMP::MSG;

fn init(&mut self) -> Cmd<Self> {
fn init(&mut self) -> Cmd<Self::MSG> {
Cmd::from(<Self as Component>::init(self))
}

fn update(&mut self, msg: COMP::MSG) -> Cmd<Self> {
fn update(&mut self, msg: COMP::MSG) -> Cmd<Self::MSG> {
let effects = <Self as Component>::update(self, msg);
Cmd::from(effects)
}

fn view(&self) -> Node<COMP::MSG> {
fn view(&self) -> Node<Self::MSG> {
<Self as Component>::view(self)
}

Expand All @@ -140,7 +139,6 @@ where
<Self as Component>::style(self)
}
}
*/

/// create a stateful component node
pub fn stateful_component<COMP, MSG, MSG2>(
Expand Down
1 change: 1 addition & 0 deletions examples/delay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ console_error_panic_hook = "0.1"
log = "0.4"
console_log = { version = "0.2", features = ["color"] }
wasm-bindgen-futures = "0.4.31"
futures = "=0.3.30"
50 changes: 22 additions & 28 deletions examples/delay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::rc::Rc;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use wasm_bindgen_futures::spawn_local;
use futures::channel::mpsc;

pub enum Msg {
Click,
Expand All @@ -26,33 +27,6 @@ pub struct App {
executed: Rc<AtomicBool>,
}

impl App {
fn execute_delayed(
mut program: Program<Self>,
current_handle: Rc<RefCell<Option<TimeoutCallbackHandle>>>,
executed: Rc<AtomicBool>,
) {
log::info!("in execute delayed...");
if let Some(current_handle) = current_handle.borrow_mut().take() {
log::info!("We cancelled {:?}", current_handle);
drop(current_handle);
}

let handle = sauron::dom::request_timeout_callback(
move || {
log::info!("I'm executing after 5 seconds");
executed.store(true, Ordering::Relaxed);
// have to dispatch something in order to update the view
program.dispatch(Msg::NoOp);
},
5000,
)
.expect("must have a handle");

*current_handle.borrow_mut() = Some(handle);
}
}

impl Application for App {
type MSG = Msg;

Expand Down Expand Up @@ -103,7 +77,27 @@ impl Application for App {
Msg::CancelPrevious => {
let current_handle = Rc::clone(&self.current_handle);
let executed = Rc::clone(&self.executed);
Cmd::new(|program| Self::execute_delayed(program, current_handle, executed))
//Cmd::new(|program| Self::execute_delayed(program, current_handle, executed))
log::info!("in execute delayed...");
if let Some(current_handle) = current_handle.borrow_mut().take() {
log::info!("We cancelled {:?}", current_handle);
drop(current_handle);
}
let (mut tx, rx) = mpsc::unbounded();
let handle = sauron::dom::request_timeout_callback(
move || {
log::info!("I'm executing after 5 seconds");
executed.store(true, Ordering::Relaxed);
tx.start_send(Msg::NoOp).unwrap();
},
5000,
)
.expect("must have a handle");

*current_handle.borrow_mut() = Some(handle);
Cmd::sub(rx, sauron::Closure::new(|_:sauron::web_sys::Event|{
panic!("This is not called!");
}))
}
Msg::NoOp => Cmd::none(),
}
Expand Down
25 changes: 0 additions & 25 deletions examples/experimentals/src/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,6 @@ impl Component for Button {
}
}

impl Application for Button
{
type MSG = Msg;

fn init(&mut self) -> Cmd<Msg> {
Cmd::from(<Self as Component>::init(self))
}

fn update(&mut self, msg: Msg) -> Cmd<Msg> {
let effects = <Self as Component>::update(self, msg);
Cmd::from(effects)
}

fn view(&self) -> Node<Msg> {
<Self as Component>::view(self)
}

fn stylesheet() -> Vec<String> {
<Self as Component>::stylesheet()
}

fn style(&self) -> Vec<String> {
<Self as Component>::style(self)
}
}

impl StatefulComponent for Button {
fn attribute_changed(
Expand Down
25 changes: 0 additions & 25 deletions examples/experimentals/src/date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,31 +180,6 @@ where
}
}

impl Application for DateTimeWidget<()>
{
type MSG = Msg;

fn init(&mut self) -> Cmd<Msg> {
Cmd::from(<Self as Component>::init(self))
}

fn update(&mut self, msg: Msg) -> Cmd<Msg> {
let effects = <Self as Component>::update(self, msg);
Cmd::from(effects)
}

fn view(&self) -> Node<Msg> {
<Self as Component>::view(self)
}

fn stylesheet() -> Vec<String> {
<Self as Component>::stylesheet()
}

fn style(&self) -> Vec<String> {
<Self as Component>::style(self)
}
}

impl StatefulComponent for DateTimeWidget<()> {
/// this is called when the attributes in the mount is changed
Expand Down
2 changes: 1 addition & 1 deletion tests/test_fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct SimpleComponent;
impl Application for SimpleComponent {
type MSG = ();

fn update(&mut self, _msg: ()) -> Cmd<Self> {
fn update(&mut self, _msg: ()) -> Cmd<Self::MSG> {
trace!("updating in SimpleComponent");
Cmd::none()
}
Expand Down

0 comments on commit e1b9a47

Please sign in to comment.