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

Introduces persist_event_if #142

Merged
merged 1 commit into from
Feb 1, 2024
Merged
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
54 changes: 54 additions & 0 deletions akka-persistence-rs/src/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,28 @@ where
{
}

/// A side effect to run a function asynchronously and then, if ok,
/// persist an event. The associated
/// behavior is available so that communication channels, for
/// example, can be accessed by the side-effect. Additionally, the
/// latest state given any previous effect having persisted an event,
/// or else the state at the outset of the effects being applied,
/// is also available.
pub fn persist_event_if<B, F, R>(f: F) -> ThenPersistEvent<B, F, R>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to combine persist_event_if with a reply, so that you can reply with different things depending on the outcome of the async _if ? (I think so, but want to be sure)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that’s possible. I’ve got an example of this which I’ll share when I’m back at my desk tomorrow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's my example:

let entity_id = context.entity_id.clone();
persist_event_if(move |b: &Self, _, _| {
    let registered = entity_id.parse().ok().map(|mac_address| {
        Event::Registered(Registered {
            client_ip: into_eui(&mac_address),
            client_pub_key,
            server_host: b.server_host.clone(),
            server_pub_key: b.server_pub_key.clone(),
        })
    });

    future::ready(Ok(registered))
})
.then_reply(move |s| {
    s.and_then(|s| {
        match (
            s.client_ip,
            &s.client_pub_key,
            &s.server_host,
            &s.server_pub_key,
        ) {
            (
                Some(client_ip),
                Some(client_pub_key),
                Some(server_host),
                Some(server_pub_key),
            ) => Some((
                reply_to,
                Registered {
                    client_ip,
                    client_pub_key: client_pub_key.clone(),
                    server_host: server_host.clone(),
                    server_pub_key: server_pub_key.clone(),
                },
            )),
            _ => None,
        }
    })
})
.boxed()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

where
B: EventSourcedBehavior + Send + Sync + 'static,
B::State: Send + Sync,
F: FnOnce(&B, Option<&B::State>, Result) -> R + Send,
R: Future<Output = StdResult<Option<B::Event>, Error>> + Send,
<B as EventSourcedBehavior>::Event: Send,
{
ThenPersistEvent {
deletion_event: false,
f: Some(f),
phantom: PhantomData,
}
}

/// The return type of [EffectExt::then_reply].
pub struct ThenReply<B, F, R, T> {
f: Option<F>,
Expand Down Expand Up @@ -784,4 +806,36 @@ mod tests {

assert_eq!(reply_to_receiver.await, Ok(reply_value));
}

#[test(tokio::test)]
async fn test_persist_event_if() {
let entity_id = EntityId::from("entity-id");
let expected = EventEnvelope {
deletion_event: false,
entity_id: entity_id.clone(),
seq_nr: 1,
event: TestEvent,
timestamp: Utc::now(),
};
let mut handler = TestHandler {
expected: expected.clone(),
};
let mut entity_ops = TestEntityOps {
expected_get_entity_id: entity_id.clone(),
get_result: TestState,
expected_update: expected,
};

assert!(persist_event_if(|_b, _, _| async { Ok(Some(TestEvent)) })
.process(
&TestBehavior,
&mut handler,
&mut entity_ops,
&entity_id,
&mut 0,
Ok(()),
)
.await
.is_ok());
}
}
Loading