Skip to content

Commit

Permalink
refactor: **breaking** remove the async variant of effects, since the…
Browse files Browse the repository at this point in the history
… Cmd will do just fine
  • Loading branch information
ivanceras committed Apr 2, 2024
1 parent 08ca4e6 commit 8e96fe0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 54 deletions.
43 changes: 0 additions & 43 deletions crates/core/src/dom/effects.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::dom::Modifier;
use crate::dom::Cmd;
use std::future::ready;
use std::future::Future;

/// Effects is a convenient way to group Msg for component to execute subsequent updates based on certain conditions.
/// This can be used for doing animation and incremental changes to the view to provide an effect
Expand Down Expand Up @@ -41,23 +40,6 @@ where
}
}

/// Create a new Effects with local and external futures that can resolve to MSG and XMSG
/// respectively
pub fn with_async<F, FX>(
local: impl IntoIterator<Item = F>,
external: impl IntoIterator<Item = FX>,
) -> Self
where
F: Future<Output = MSG> + 'static,
FX: Future<Output = XMSG> + 'static,
XMSG: 'static,
{
Self {
local: local.into_iter().map(Cmd::single).collect(),
external: external.into_iter().map(Cmd::single).collect(),
modifier: Modifier::default(),
}
}

/// Create an Effects with local messages that will be executed on the next update loop on this Component
pub fn with_local(local: impl IntoIterator<Item = MSG>) -> Self {
Expand All @@ -68,18 +50,6 @@ where
}
}

/// Create an Effects with local message that will can resolved into MSG, it will be executed
/// on the next update loop on the component it is being returned
pub fn with_local_async<F>(local: impl IntoIterator<Item = F>) -> Self
where
F: Future<Output = MSG> + 'static,
{
Self {
local: local.into_iter().map(Cmd::single).collect(),
external: vec![],
modifier: Modifier::default(),
}
}

/// Create an Effects with extern messages that will be executed on the parent Component
pub fn with_external(external: impl IntoIterator<Item = XMSG>) -> Self
Expand All @@ -96,19 +66,6 @@ where
}
}

/// Create an Effects with external messages that will can be resolved into an XMSG, this will
/// be dispatch in the next Component update
pub fn with_external_async<F>(external: impl IntoIterator<Item = F>) -> Self
where
F: Future<Output = XMSG> + 'static,
XMSG: 'static,
{
Self {
local: vec![],
external: external.into_iter().map(Cmd::single).collect(),
modifier: Modifier::default(),
}
}

/// Create and empty Effects
pub fn none() -> Self {
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/dom/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ where
app,
current_vdom: Rc::new(RefCell::new(app_view)),
pending_msgs: Rc::new(RefCell::new(VecDeque::new())),
pending_cmds: Rc::new(RefCell::new(VecDeque::new())),
pending_dispatches: Rc::new(RefCell::new(VecDeque::new())),
},
root_node: Rc::new(RefCell::new(None)),
mount_node: Rc::new(RefCell::new(None)),
Expand Down Expand Up @@ -708,7 +708,7 @@ where
);
}

// execute this `cmd` batched pending_cmds that may have resulted from updating the app
// execute this `cmd` batched pending_dispatches that may have resulted from updating the app
cmd.emit(self.clone());
}

Expand Down
18 changes: 9 additions & 9 deletions crates/core/src/dom/program/app_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ where
pub(crate) pending_msgs: Rc<RefCell<VecDeque<APP::MSG>>>,

/// pending cmds that hasn't been emited yet
pub(crate) pending_cmds: Rc<RefCell<VecDeque<Dispatch<APP>>>>,
pub(crate) pending_dispatches: Rc<RefCell<VecDeque<Dispatch<APP>>>>,
}

pub(crate) struct WeakContext<APP>
Expand All @@ -39,7 +39,7 @@ where
pub(crate) app: Weak<RefCell<APP>>,
pub(crate) current_vdom: Weak<RefCell<vdom::Node<APP::MSG>>>,
pub(crate) pending_msgs: Weak<RefCell<VecDeque<APP::MSG>>>,
pub(crate) pending_cmds: Weak<RefCell<VecDeque<Dispatch<APP>>>>,
pub(crate) pending_dispatches: Weak<RefCell<VecDeque<Dispatch<APP>>>>,
}

impl<APP> WeakContext<APP>
Expand All @@ -50,12 +50,12 @@ where
let app = self.app.upgrade()?;
let current_vdom = self.current_vdom.upgrade()?;
let pending_msgs = self.pending_msgs.upgrade()?;
let pending_cmds = self.pending_cmds.upgrade()?;
let pending_dispatches = self.pending_dispatches.upgrade()?;
Some(AppContext {
app,
current_vdom,
pending_msgs,
pending_cmds,
pending_dispatches,
})
}
}
Expand All @@ -69,7 +69,7 @@ where
app: Weak::clone(&self.app),
current_vdom: Weak::clone(&self.current_vdom),
pending_msgs: Weak::clone(&self.pending_msgs),
pending_cmds: Weak::clone(&self.pending_cmds),
pending_dispatches: Weak::clone(&self.pending_dispatches),
}
}
}
Expand All @@ -83,7 +83,7 @@ where
app: Rc::downgrade(&this.app),
current_vdom: Rc::downgrade(&this.current_vdom),
pending_msgs: Rc::downgrade(&this.pending_msgs),
pending_cmds: Rc::downgrade(&this.pending_cmds),
pending_dispatches: Rc::downgrade(&this.pending_dispatches),
}
}
pub fn strong_count(&self) -> usize {
Expand All @@ -103,7 +103,7 @@ where
app: Rc::clone(&self.app),
current_vdom: Rc::clone(&self.current_vdom),
pending_msgs: Rc::clone(&self.pending_msgs),
pending_cmds: Rc::clone(&self.pending_cmds),
pending_dispatches: Rc::clone(&self.pending_dispatches),
}
}
}
Expand Down Expand Up @@ -174,14 +174,14 @@ where

if let Some(cmd) = cmd {
// we put the cmd in the pending_cmd queue
self.pending_cmds.borrow_mut().push_back(cmd);
self.pending_dispatches.borrow_mut().push_back(cmd);
true
} else {
false
}
}

pub fn batch_pending_cmds(&mut self) -> Dispatch<APP> {
Dispatch::batch(self.pending_cmds.borrow_mut().drain(..))
Dispatch::batch(self.pending_dispatches.borrow_mut().drain(..))
}
}

0 comments on commit 8e96fe0

Please sign in to comment.