question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to handle state in a event trigger program ?

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
orthoxeroxcommented, Jan 1, 2020

@qwas368

  1. enums are not guaranteed to be restricted to the variants you’ve defined, (RobotEventEnum)42 is a valid enum value
  2. DUs can have properties, e.g., MoveUp(int distance)
0reactions
qwas368commented, Jan 6, 2020

Thanks for your detail explanation! I need more practice, now. 😃

The reason I suggest this, is because a lot of people use this library think that monads begin and end with Option or the other in-built monads.

You are right. The reason is we don’t need to reinvent the wheel. We can use Option such as IEnumerable.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found