Skip to content

Commit

Permalink
Rename Worker methods for readability
Browse files Browse the repository at this point in the history
Change-type: minor
  • Loading branch information
pipex committed Feb 14, 2025
1 parent 4eb5a0a commit d41cfe5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ async fn main() {
// for global state
.job("/", update(plus_one))
// The initial state of the worker
.with_state(0)
.initial_state(0)


// Tell the agent to find a plan from the initial state (0)
// to the target state (3) and execute it
agent.seek(3);
agent.seek_target(3);

// Wait for the agent to return a result
let res = agent.wait(0).await;
Expand All @@ -82,7 +82,7 @@ async fn main() {
}
```

When receiving a call to `seek`, the worker looks for a plan to get the system to a state equal to 3 target and then executes it.
When receiving a call to `seek_target`, the worker looks for a plan to get the system to a state equal to 3 target and then executes it.
In this case it will identify that 3 sequential calls to `plus_one` are necessary to reach the target.

## Performing IO
Expand Down Expand Up @@ -168,12 +168,12 @@ async fn main() {
// on any given counter
.job("/counters/{name}", update(plus_one))
// Initialize two counters "a" and "b" to 0
.with_state(State {counters: HashMap::from([("a".to_string(), 0), ("b".to_string(), 0)])})
.initial_state(State {counters: HashMap::from([("a".to_string(), 0), ("b".to_string(), 0)])})


// Tell the agent to find a plan from the initial state
// to the target state and execute it
agent.seek(State {counters: HashMap::from([("a".to_string(), 3), ("b".to_string(), 2)])});
agent.seek_target(State {counters: HashMap::from([("a".to_string(), 3), ("b".to_string(), 2)])});

// Wait for the agent to return a result
let res = agent.wait(0).await;
Expand Down Expand Up @@ -214,7 +214,7 @@ async fn main() {
.job("/counters/{name}", update(plus_one))
.job("/counters/{name}", update(plus_two))
// Initialize two counters "a" and "b" to 0
.with_state(State {counters: HashMap::from([("a".to_string(), 0), ("b".to_string(), 0)])})
.initial_state(State {counters: HashMap::from([("a".to_string(), 0), ("b".to_string(), 0)])})

// Seek some state, etc
}
Expand Down
42 changes: 20 additions & 22 deletions src/worker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl<T> Worker<T, Uninitialized> {
Worker::from_inner(inner)
}

pub fn with_state(self, state: T) -> Worker<T, Ready>
pub fn initial_state(self, state: T) -> Worker<T, Ready>
where
T: Serialize + DeserializeOwned,
{
Expand All @@ -152,7 +152,7 @@ impl<T: Serialize + DeserializeOwned> Worker<T, Ready> {
self.inner.system.state().unwrap()
}

pub fn seek(self, tgt: T, local_set: &LocalSet) -> Worker<T, Running> {
pub fn seek_target(self, tgt: T, local_set: &LocalSet) -> Worker<T, Running> {
let Ready {
planner,
system,
Expand Down Expand Up @@ -321,22 +321,20 @@ mod tests {
use crate::task::*;
use tokio::time::sleep;

fn plus_one(counter: Update<i32>, Target(tgt): Target<i32>) -> Effect<Update<i32>> {
if *counter >= tgt {
// We reached the target, no further changes
// are needed
return Effect::of(counter);
fn plus_one(mut counter: Update<i32>, Target(tgt): Target<i32>) -> Effect<Update<i32>> {
if *counter < tgt {
// Modify the counter if we are below target
*counter += 1;
}

Effect::of(counter)
.map(|mut counter| {
*counter += 1;
counter
})
.with_io(|counter| async {
sleep(Duration::from_millis(10)).await;
Ok(counter)
})
// Return the updated counter. The I/O part of the
// effect will only be called if the job is chosen
// in the workflow which will only happens if there are
// changes
Effect::of(counter).with_io(|counter| async {
sleep(Duration::from_millis(10)).await;
Ok(counter)
})
}

fn init() {
Expand All @@ -349,8 +347,8 @@ mod tests {
let local_set = LocalSet::new();
let worker = Worker::new()
.job("", update(plus_one))
.with_state(0)
.seek(2, &local_set);
.initial_state(0)
.seek_target(2, &local_set);

local_set
.run_until(async move {
Expand All @@ -367,8 +365,8 @@ mod tests {
let local_set = LocalSet::new();
let worker = Worker::new()
.job("", update(plus_one))
.with_state(0)
.seek(2, &local_set);
.initial_state(0)
.seek_target(2, &local_set);

local_set
.run_until(async move {
Expand All @@ -387,8 +385,8 @@ mod tests {
let local_set = LocalSet::new();
let worker = Worker::new()
.job("", update(plus_one))
.with_state(0)
.seek(2, &local_set);
.initial_state(0)
.seek_target(2, &local_set);

local_set
.run_until(async move {
Expand Down

0 comments on commit d41cfe5

Please sign in to comment.