subscribe method
Subscribe to a system pulse/action
Returns a subscription object that implies a cancel() method or similar.
Implementation
@override
dynamic subscribe(
String key,
void Function(dynamic) callback, {
String? sourceId,
}) {
String? effectiveSourceId = sourceId;
// Fallback: If sourceId is missing, try to infer it from the key (e.g. 'counter.increment')
// This allows internal module state controllers (like CounterState) to subscribe
// to their own signals even if the identity isn't explicitly passed down yet.
if (effectiveSourceId == null && key.contains('.')) {
effectiveSourceId = key.split('.').first;
}
// Record data interaction for auditing
if (effectiveSourceId != null) {
// Try to find the owner module from the key (e.g., 'counter.count' -> 'counter')
final dotIndex = key.indexOf('.');
final targetModuleId = dotIndex != -1
? key.substring(0, dotIndex)
: 'unknown';
// Only record if it's an interaction between different modules
if (effectiveSourceId != targetModuleId) {
recordInteraction(effectiveSourceId, targetModuleId, 'data', key);
}
}
// Return the subscription via EventBus
return EventBus().onSignal(
key,
callback,
subscriberModuleId: effectiveSourceId,
);
}