Files
flutter_lua_runtime/lib/runtime/events/runtime_event_gate.dart
2026-06-07 22:53:58 +08:00

48 lines
1.4 KiB
Dart

import '../lifecycle/runtime_session.dart';
import '../models/runtime_event.dart';
class RuntimeEventGate {
const RuntimeEventGate({
required this.session,
required bool Function(String id) isScopeAlive,
bool Function(String id, int epoch)? isNodeEpochAlive,
}) : _isScopeAlive = isScopeAlive,
_isNodeEpochAlive = isNodeEpochAlive;
final RuntimeSession session;
final bool Function(String id) _isScopeAlive;
final bool Function(String id, int epoch)? _isNodeEpochAlive;
RuntimeEvent attachSession(RuntimeEvent event) {
return event.withLifecycle(sessionId: event.sessionId ?? session.id);
}
bool accepts(RuntimeEvent event) {
final eventSessionId = event.sessionId;
if (eventSessionId != null && !session.accepts(eventSessionId)) {
return false;
}
final target = event.target;
final targetEpoch = event.targetEpoch;
final epochChecker = _isNodeEpochAlive;
if (target != null && targetEpoch != null && epochChecker != null) {
if (!epochChecker(target, targetEpoch)) {
return false;
}
}
final scope = event.scope;
if (scope != null && !_isScopeAlive(scope)) {
return false;
}
final scopeEpoch = event.scopeEpoch;
if (scope != null && scopeEpoch != null && epochChecker != null) {
if (!epochChecker(scope, scopeEpoch)) {
return false;
}
}
return true;
}
}