Scope::LOCAL¶
Default emit scope — distributes tasks via the thread pool and stores data in the global cache.
Syntax¶
// Implicit (shorthand)
emit(std::make_unique<T>(args...));
// Explicit
emit<Scope::LOCAL>(std::make_unique<T>(args...));
Behavior¶
When data is emitted with Scope::LOCAL:
- All reactions bound to
Trigger<T>generate tasks queued for thread pool execution. - The data is stored in the global
DataStore<T>, making it accessible to subsequentWith<T>retrievals. - Tasks are scheduled normally — execution order depends on priority and thread availability.
sequenceDiagram
participant E as Emitter
participant S as DataStore<T>
participant TP as Thread Pool
participant R1 as Reaction A
participant R2 as Reaction B
E->>TP: emit(data) — queue tasks
E->>S: Store latest T
TP->>R1: Execute task
TP->>R2: Execute task
Example¶
#include <nuclear>
struct SensorReading {
double value;
};
class Sensor : public NUClear::Reactor {
public:
explicit Sensor(std::unique_ptr<NUClear::Environment> environment) : Reactor(std::move(environment)) {
on<Every<1, std::chrono::seconds>>().then([this] {
emit(std::make_unique<SensorReading>(SensorReading{42.0}));
});
on<Trigger<SensorReading>>().then([this](const SensorReading& reading) {
log<INFO>("Received:", reading.value);
});
}
};
Notes¶
- This is the default scope.
emit(ptr)andemit<Scope::LOCAL>(ptr)are equivalent. - Data persists in the global store until overwritten by a subsequent emit of the same type.
- Tasks are not guaranteed to execute before the emitter continues — they are queued asynchronously.