Skip to content

Commit 0584c09

Browse files
committed
feat: separate global and local store times
We need to be able to control store local time using `TimeService` so that we can run multiple `Store`s per thread/process each with different pace/time. Local time will initialize global time so if we are running code for production with only 1 store, local and global times will match.
1 parent 8c5e611 commit 0584c09

File tree

5 files changed

+21
-20
lines changed

5 files changed

+21
-20
lines changed

src/action/action_meta.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use super::{ActionId, ActionWithMeta};
77
pub type RecursionDepth = u32;
88

99
/// Action with additional metadata like: id.
10-
#[derive(Debug, Clone)]
10+
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone)]
1111
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1212
pub struct ActionMeta {
1313
id: ActionId,
@@ -26,6 +26,14 @@ impl ActionMeta {
2626
Self { id, depth }
2727
}
2828

29+
#[inline(always)]
30+
pub fn zero_custom(time: Timestamp) -> Self {
31+
Self {
32+
id: ActionId::new_unchecked(time.into()),
33+
depth: 0,
34+
}
35+
}
36+
2937
#[inline(always)]
3038
pub fn id(&self) -> ActionId {
3139
self.id

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mod service;
1616
pub use service::{Service, TimeService};
1717

1818
mod store;
19-
pub use store::{Store, monotonic_to_time};
19+
pub use store::{monotonic_to_time, Store};
2020

2121
mod sub_store;
2222
pub use sub_store::SubStore;

src/service.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use crate::{Instant, store::monotonic_time};
1+
use crate::Instant;
22

33
pub trait Service: TimeService {}
44

55
/// Time service.
66
pub trait TimeService {
7-
/// NOTE this should be deprecated as monotonic time now is inherited from Rust library.
87
fn monotonic_time(&mut self) -> Instant {
9-
monotonic_time()
8+
Instant::now()
109
}
1110
}

src/store.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,9 @@ impl<T: Clone> Clone for StateWrapper<T> {
4141
/// Monotonic and system time reference points.
4242
static INITIAL_TIME: OnceLock<(Instant, SystemTime)> = OnceLock::new();
4343

44-
/// Monotonic timer current value
45-
pub(crate) fn monotonic_time() -> Instant {
46-
Instant::now()
47-
}
48-
49-
5044
/// Converts monotonic time to nanoseconds since Unix epoch.
5145
pub fn monotonic_to_time(time: Instant) -> u64 {
52-
let (monotonic, system) = INITIAL_TIME.get_or_init(|| {
53-
(monotonic_time(), SystemTime::now())
54-
});
46+
let (monotonic, system) = INITIAL_TIME.get_or_init(|| (Instant::now(), SystemTime::now()));
5547
let time_passed = time.duration_since(*monotonic);
5648
system
5749
.duration_since(SystemTime::UNIX_EPOCH)
@@ -93,16 +85,18 @@ where
9385
pub fn new(
9486
reducer: Reducer<State, Action>,
9587
effects: Effects<State, Service, Action>,
96-
service: Service,
88+
mut service: Service,
9789
initial_time: SystemTime,
9890
initial_state: State,
9991
) -> Self {
100-
let initial_monotonic_time = monotonic_time();
92+
let initial_monotonic_time = service.monotonic_time();
10193
let initial_time_nanos = initial_time
10294
.duration_since(SystemTime::UNIX_EPOCH)
10395
.map(|x| x.as_nanos())
10496
.unwrap_or(0);
10597

98+
INITIAL_TIME.get_or_init(move || (initial_monotonic_time, initial_time));
99+
106100
Self {
107101
reducer,
108102
effects,
@@ -175,7 +169,7 @@ where
175169

176170
/// Dispatches action without checking the enabling condition.
177171
fn dispatch_enabled(&mut self, action: Action) {
178-
let monotonic_time = monotonic_time();
172+
let monotonic_time = self.service.monotonic_time();
179173
let time_passed = monotonic_time
180174
.duration_since(self.monotonic_time)
181175
.as_nanos();

src/timestamp.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub use std::time::{Instant, SystemTime};
44
#[cfg(target_arch = "wasm32")]
55
pub use wasm_timer::{Instant, SystemTime};
66

7-
use crate::{monotonic_to_time, store::monotonic_time};
7+
use crate::monotonic_to_time;
88

99
/// Time in nanoseconds from [std::time::UNIX_EPOCH].
1010
///
@@ -33,8 +33,8 @@ impl Timestamp {
3333
}
3434

3535
#[inline(always)]
36-
pub fn now() -> Self {
37-
Timestamp::new(monotonic_to_time(monotonic_time()))
36+
pub fn global_now() -> Self {
37+
Timestamp::new(monotonic_to_time(Instant::now()))
3838
}
3939

4040
pub fn checked_sub(self, rhs: Timestamp) -> Option<Duration> {

0 commit comments

Comments
 (0)