Skip to content

MuharremTozan/Agentic-Duel / .agent/skills/unity-event-bus

unity-event-bus

Verified

Advanced code-driven event bus with reflection-based bootstrapping. Provides zero-setup global messaging.

assets/code/EventBusCore.cs.txt

View on GitHub
using System;
using System.Collections.Generic;

namespace Standard.Events.Bus {
    /// <summary>
    /// Base interface for all events in the bus system.
    /// Use structs implementing this for performance.
    /// </summary>
    public interface IEvent { }

    /// <summary>
    /// Interface for event bindings that link a callback to an event type.
    /// </summary>
    public interface IEventBinding<T> where T : IEvent {
        Action<T> OnEvent { get; set; }
        Action OnEventNoArgs { get; set; }
    }

    /// <summary>
    /// Concrete binding for an event type T.
    /// </summary>
    public class EventBinding<T> : IEventBinding<T> where T : IEvent {
        public Action<T> OnEvent { get; set; } = _ => { };
        public Action OnEventNoArgs { get; set; } = () => { };

        public EventBinding(Action<T> onEvent) => OnEvent = onEvent;
        public EventBinding(Action onEventNoArgs) => OnEventNoArgs = onEventNoArgs;
    }

    /// <summary>
    /// The global hub for a specific event type.
    /// </summary>
    public static class EventBus<T> where T : IEvent {
        private static readonly HashSet<IEventBinding<T>> _bindings = new();

        public static void Register(IEventBinding<T> binding) => _bindings.Add(binding);
        public static void Deregister(IEventBinding<T> binding) => _bindings.Remove(binding);

        public static void Raise(T @event) {
            foreach (var binding in _bindings) {
                binding.OnEvent.Invoke(@event);
                binding.OnEventNoArgs.Invoke();
            }
        }

        public static void Clear() => _bindings.Clear();
    }
}
receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementationStars: 322kUpdated Claude CodeCodexVerifiedInstall
requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirementsStars: 322kUpdated Claude CodeCodexVerifiedInstall
using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallbackStars: 322kUpdated Claude CodeCodexVerifiedInstall
verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions alwaysStars: 322kUpdated Claude CodeCodexVerifiedInstall