How to handle state in a event trigger program ?
See original GitHub issueI know that state monad can use to handle state by returning an updated state.
But most case in my job, we use event trigger, message bus and mediator pattern a lot.
Like below example, Handler
is a entry point and some events trigger them.
How to use state monad and avoid side-effects? Or I shouldn’t struggle with this issue because it’s not worth it ?
namespace Application
{
static class Robot
{
public enum RobotStateEnum
{
Up,
Down,
Unknown
}
public static RobotStateEnum state = RobotStateEnum.Unknown;
}
public class Robot_Up : INotification { }
public class Robot_Down : INotification { }
public class Robot_Up_Handler : INotificationHandler<Robot_Up>
{
public Task Handle(Robot_Up notification, CancellationToken cancellationToken)
{
Robot.state = Robot.RobotStateEnum.Up; // change state
// Do something
return Task.CompletedTask;
}
}
public class Robot_Up_Handler2 : INotificationHandler<Robot_Up>
{
public Task Handle(Robot_Up notification, CancellationToken cancellationToken)
{
// Do something
return Task.CompletedTask;
}
}
public class Robot_Down_Handler : INotificationHandler<Robot_Down>
{
public Task Handle(Robot_Down notification, CancellationToken cancellationToken)
{
Robot.state = Robot.RobotStateEnum.Down; // change state
// Do something
return Task.CompletedTask;
}
}
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Trigger events on state changes
Trigger events on state changes · Register a new event on the Work Order Task [wm_task] table called task.closed . · Navigate to...
Read more >React JS - trigger event on certain state value
I want to trigger the parent's props.handleNextChild method if the child's state's activeTask property equals 3. How would I do that? javascript ...
Read more >Event-driven state management in React using Storeon
This file is responsible for handling state and subsequent state management operations in our app. We must create a module to store our...
Read more >Sustaining form state through event handling - Elixir Forum
Using LiveView, I have a form where I want some dynamic behavior based on the user's interaction with the view. Let's say I...
Read more >React interactivity: Events and state - Learn web development
Objective: To learn about handling events and state in React, and use those to start making the case study app interactive.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@qwas368
(RobotEventEnum)42
is a valid enum valueMoveUp(int distance)
Thanks for your detail explanation! I need more practice, now. 😃
You are right. The reason is we don’t need to reinvent the wheel. We can use
Option
such asIEnumerable
.