Skip to content

Event handler

In Reactter, event handler plays a pivotal role in facilitating seamless communication and coordination between various components within the application. Designed to ensure efficient state management and dependency injection, fostering a cohesive ecosystem where different parts of the application can interact harmoniously.

API

Reactter offers the following event handler mechanisms:

How it works

The event handler in Reactter is based on some fundamental concepts:

  • Event: It is an enum that represents a specific action or occurrence associated with a particular instance. It defines the type of interaction or change that can happen.
  • Instance: It is an Object used to identify the entity that triggers the event and to emit the corresponding events. It acts as the emitter that connects the event to the action.
  • Action: It is a Function that executes in response to an emitted event. It contains the logic needed to manage the event and define the desired behavior.

Understanding these concepts is crucial for efficiently managing event-driven interactions in Reactter applications.

Example

To illustrate this, let’s take a countdown example seen from the State Management page:

main.dart
1
import 'dart:async';
2
import 'package:reactter/reactter.dart';
3
4
// Create a reactive state called `count` using the `Signal` class
5
final count = Signal(10);
6
7
void main() async {
8
// Listen to the `didUpdate` event of the `count` state
9
// and print the `value` of `count` each time it changes
10
Rt.on(
11
count,
12
Lifecycle.didUpdate,
13
(_, __) => print('Count: $count')
14
);
15
16
// Create a timer that decrements the `value` of `count`
17
// by 1 each time second until it reaches 0
18
await Timer.periodic(Duration(seconds: 1), countdown);
19
}
20
21
// Decrement the `value` of `count` by 1 each time the timer ticks
22
// and cancel the `timer` when the `value` of `count` reaches 0
23
void countdown(Timer timer) {
24
count.value -= 1;
25
26
if (count.value == 0) {
27
timer.cancel();
28
}
29
}

In this example, we see that the Rt.on method is used to subscribe to the event Lifecycle.didUpdate of the count instance from line 10 to 14. Whenever the count state changes, the listener function is invoked, printing the current value of the count state.

Here, we cannot see the emitting statement( emit ) because it is encapsulated inside the Signal class and is called when the value of the count state changes. To see how an event is emitted, let’s make a small adjustment to add an emitter:

main.dart
1
import 'dart:async';
2
import 'package:reactter/reactter.dart';
3
4
enum CustomEvent { countdownFinished }
5
6
// Create a reactive state called `count` using the `Signal` class
7
final count = Signal(10);
8
9
void main() async {
10
// Listen to the `didUpdate` event of the `count` state
11
// and print the `value` of `count` each time it changes
12
Rt.on(
13
count,
14
Lifecycle.didUpdate,
15
(_, __) => print('Count: $count')
16
);
17
18
// Listen to the `countdownFinished` event
19
// and print a message when the countdown is finished
20
Rt.on(
21
count,
22
CustomEvent.countdownFinished,
23
(_, __) => print('Countdown finished!')
24
);
25
26
// Create a timer that decrements the `value` of `count`
27
// by 1 each time second until it reaches 0
28
await Timer.periodic(Duration(seconds: 1), countdown);
29
}
30
31
// Decrement the `value` of `count` by 1 each time the timer ticks
32
// and cancel the `timer` when the `value` of `count` reaches 0
33
void countdown(Timer timer) {
34
count.value -= 1;
35
36
if (count.value == 0) {
37
timer.cancel();
38
// Emit the `CustomEvent.countdownFinished` event
39
Rt.emit(count, CustomEvent.countdownFinished);
40
}
41
}

We added a new event called CustomEvent.countdownFinished and a new listener that prints a message when the countdown is finished. When the countdown reaches 0 , the count instance emits the CustomEvent.countdownFinished event, and the listener function is invoked, printing the message.

This example demonstrates how the event handler system in Reactter enables seamless communication between different parts of the application, facilitating efficient coordination and interaction.