Skip to content

Feat/enabling condition with time #1

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 12 additions & 2 deletions src/action/action_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,29 @@ pub type RecursionDepth = u32;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ActionMeta {
id: ActionId,
/// Previously applied action.
prev: ActionId,
/// Recursion depth of a given action.
depth: RecursionDepth,
}

impl ActionMeta {
pub const ZERO: Self = Self {
id: ActionId::ZERO,
prev: ActionId::ZERO,
depth: 0,
};

#[inline(always)]
pub(crate) fn new(id: ActionId, depth: RecursionDepth) -> Self {
Self { id, depth }
pub(crate) fn new(id: ActionId, prev: ActionId, depth: RecursionDepth) -> Self {
Self { id, prev, depth }
}

#[inline(always)]
pub fn zero_custom(time: Timestamp) -> Self {
Self {
id: ActionId::new_unchecked(time.into()),
prev: ActionId::new_unchecked(time.into()),
depth: 0,
}
}
Expand All @@ -45,6 +49,12 @@ impl ActionMeta {
self.depth
}

/// Time of previously applied action.
#[inline(always)]
pub fn prev_time(&self) -> Timestamp {
self.prev.into()
}

#[inline(always)]
pub fn time(&self) -> Timestamp {
self.id.into()
Expand Down
6 changes: 4 additions & 2 deletions src/action/enabling_condition.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#[allow(unused_variables)]
pub trait EnablingCondition<State> {
/// Enabling condition for the Action.
///
/// Checks if the given action is enabled for a given state.
fn is_enabled(&self, #[allow(unused_variables)] state: &State) -> bool {
/// Checks if the given action is enabled for a given state and timestamp.
fn is_enabled(&self, state: &State, time: crate::Timestamp) -> bool {
true
}
}

31 changes: 18 additions & 13 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, SystemTime,
TimeService,
ActionId, ActionMeta, ActionWithMeta, Effects, EnablingCondition, Instant, Reducer, SubStore,
SystemTime, TimeService,
};

/// Wraps around State and allows only immutable borrow,
Expand Down Expand Up @@ -138,10 +138,9 @@ where
where
T: Into<Action> + EnablingCondition<State>,
{
if !action.is_enabled(self.state()) {
if !action.is_enabled(self.state(), self.last_action_id.into()) {
return false;
}

self.dispatch_enabled(action.into());

true
Expand All @@ -153,15 +152,18 @@ where
/// to reducer and then effects.
///
/// If action is not enabled, we return false and do nothing.
pub fn sub_dispatch<SubAction, A>(&mut self, action: A) -> bool
pub fn sub_dispatch<A, S>(&mut self, action: A) -> bool
where
A: Into<SubAction> + EnablingCondition<State>,
SubAction: Into<Action>,
A: Into<<Self as SubStore<State, S>>::SubAction> + EnablingCondition<S>,
<Self as SubStore<State, S>>::SubAction: Into<Action>,
Self: SubStore<State, S>,
{
if !action.is_enabled(self.state()) {
if !action.is_enabled(
<Self as SubStore<State, S>>::state(self),
self.last_action_id.into(),
) {
return false;
}

self.dispatch_enabled(action.into().into());

true
Expand All @@ -173,14 +175,17 @@ where
let time_passed = monotonic_time
.duration_since(self.monotonic_time)
.as_nanos();

self.monotonic_time = monotonic_time;
self.last_action_id = self.last_action_id.next(time_passed as u64);

let action_with_meta =
ActionMeta::new(self.last_action_id, self.recursion_depth).with_action(action);
let prev = self.last_action_id;
let curr = prev.next(time_passed as u64);

self.last_action_id = curr;
self.recursion_depth += 1;

let action_with_meta =
ActionMeta::new(curr, prev, self.recursion_depth).with_action(action);

self.dispatch_reducer(&action_with_meta);
self.dispatch_effects(action_with_meta);

Expand Down
2 changes: 1 addition & 1 deletion src/sub_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ pub trait SubStore<GlobalState, SubState> {
fn state_and_service(&mut self) -> (&SubState, &mut Self::Service);
fn dispatch<A>(&mut self, action: A) -> bool
where
A: Into<Self::SubAction> + crate::EnablingCondition<GlobalState>;
A: Into<Self::SubAction> + crate::EnablingCondition<SubState>;
}