# 5. Handle Callbacks as Events

Date: 2023-02-13

## Status

Accepted

## Context

An event-driven system has many asynchronous operations, like callbacks in response to events or interrupts. How are these types of operations to be handled?

## Decision

Callbacks will be treated as a type of event that stores a std::function object. This will allow callbacks to be dispatched by interrupt handlers (and other contexts) by submitting an event.

The other option, which was rejected, as to create a separate "dispatcher" for handling asynchronous function operations.
- In a multi-threaded system, this would be easy: create a dispatch queue to handle callbacks. But this is a single-threaded system, and it will require a separate queue that is processed before events are processed. Keeping a single queue would be beneficial for serializing callbacks and events in a consistent order.
- A dispatch concept would increase the complexity of the system. The complexity increase, given the current development team skill level, does not seem justified by the value of a new dispatcher concept.

## Consequences

- Callbacks (which are handled as events) are serialized along with actual events, rather than executing in a different context.
- Callbacks do not need to be handled by the event processing elements of the system (primarily, the CPF state engine). This needs to be kept in mine when processing events: ideally, all callbacks will be processed in a separate action, and only other event types will be passed to the CPF state processing engine.
