Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dispatch-queue): Add dispatch queue and substate support #2

Draft
wants to merge 2 commits into
base: feat/enabling-condition-with-time
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/action/enabling_condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ pub trait EnablingCondition<State> {
true
}
}

37 changes: 37 additions & 0 deletions src/dispatcher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::{collections::VecDeque, marker::PhantomData};

pub struct Dispatcher<Action, State> {
queue: VecDeque<Action>,
_marker: PhantomData<State>,
}

impl<Action, State> Dispatcher<Action, State>
where
Action: crate::EnablingCondition<State>,
{
pub fn new() -> Self {
Self {
queue: VecDeque::new(),
_marker: Default::default(),
}
}

pub fn push<T>(&mut self, action: T)
where
T: Into<Action>,
{
self.queue.push_back(action.into());
}

pub(crate) fn pop(&mut self) -> Option<Action> {
self.queue.pop_front()
}

pub(crate) fn push_front(&mut self, other: Dispatcher<Action, State>) {
other
.queue
.into_iter()
.rev()
.for_each(|action| self.queue.push_front(action));
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ pub use store::{monotonic_to_time, Store};

mod sub_store;
pub use sub_store::SubStore;

mod dispatcher;
pub use dispatcher::Dispatcher;
5 changes: 3 additions & 2 deletions src/reducer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::ActionWithMeta;
use crate::{ActionWithMeta, Dispatcher};

/// Function signature for a reducer.
pub type Reducer<State, Action> = fn(&mut State, &ActionWithMeta<Action>);
pub type Reducer<State, Action> =
fn(&mut State, &ActionWithMeta<Action>, &mut Dispatcher<Action, State>);
30 changes: 26 additions & 4 deletions src/store.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::sync::OnceLock;

use crate::{
ActionId, ActionMeta, ActionWithMeta, Effects, EnablingCondition, Instant, Reducer, SubStore,
SystemTime, TimeService,
ActionId, ActionMeta, ActionWithMeta, Dispatcher, Effects, EnablingCondition, Instant, Reducer,
SubStore, SystemTime, TimeService,
};

/// Wraps around State and allows only immutable borrow,
Expand Down Expand Up @@ -75,11 +75,15 @@ pub struct Store<State, Service, Action> {
recursion_depth: u32,

last_action_id: ActionId,

/// Queue for actions to be dispatched after the state update
dispatch_queue: Dispatcher<Action, State>,
}

impl<State, Service, Action> Store<State, Service, Action>
where
Service: TimeService,
Action: EnablingCondition<State>,
{
/// Creates a new store.
pub fn new(
Expand Down Expand Up @@ -109,6 +113,8 @@ where

recursion_depth: 0,
last_action_id: ActionId::new_unchecked(initial_time_nanos as u64),

dispatch_queue: Dispatcher::new(),
}
}

Expand Down Expand Up @@ -186,6 +192,7 @@ where
self.last_action_id = curr;
self.recursion_depth += 1;

// TODO: instead return queued actions and pass them to dispatch_effects?
self.dispatch_reducer(&action_with_meta);
self.dispatch_effects(action_with_meta);

Expand All @@ -195,21 +202,34 @@ where
/// Runs the reducer.
#[inline(always)]
fn dispatch_reducer(&mut self, action_with_id: &ActionWithMeta<Action>) {
(self.reducer)(self.state.get_mut(), action_with_id);
// All new queued elements will be stored here
let mut queue = Dispatcher::new();
(self.reducer)(self.state.get_mut(), action_with_id, &mut queue);

// All the enqueued actions gets pushed to the front of the global queue
self.dispatch_queue.push_front(queue);
}

/// Runs the effects.
#[inline(always)]
fn dispatch_effects(&mut self, action_with_id: ActionWithMeta<Action>) {
// First the effects for this specific action must be handled
(self.effects)(self, action_with_id);

// Then dispatch all actions enqueued by the reducer
while let Some(action) = self.dispatch_queue.pop() {
if action.is_enabled(self.state(), self.last_action_id.into()) {
self.dispatch_enabled(action);
}
}
}
}

impl<State, Service, Action> Clone for Store<State, Service, Action>
where
State: Clone,
Service: Clone,
Action: Clone,
Action: Clone + EnablingCondition<State>,
{
fn clone(&self) -> Self {
Self {
Expand All @@ -222,6 +242,8 @@ where

recursion_depth: self.recursion_depth,
last_action_id: self.last_action_id,

dispatch_queue: Dispatcher::new(), // TODO: clone
}
}
}