using System;
namespace Minigames.StatueDressup
{
///
/// Interface for managers that load asynchronously and notify when ready.
/// Allows dependent components to safely access the manager via WhenReady callbacks.
///
public interface IReadyNotifier
{
///
/// True when the manager has finished initialization and is ready to use
///
bool IsReady { get; }
///
/// Event invoked when the manager becomes ready
///
event Action OnReady;
}
///
/// Extension methods for IReadyNotifier to provide common callback behavior
///
public static class ReadyNotifierExtensions
{
///
/// Execute callback when ready. If already ready, executes immediately.
/// If not ready yet, subscribes to OnReady event and executes when fired.
///
public static void WhenReady(this IReadyNotifier notifier, Action callback)
{
if (notifier == null)
{
Core.Logging.Warning("[ReadyNotifierExtensions] Notifier is null, cannot execute callback");
return;
}
if (callback == null)
{
return;
}
if (notifier.IsReady)
{
// Already ready - execute immediately
callback.Invoke();
}
else
{
// Not ready yet - subscribe to event and auto-unsubscribe after invocation
Action handler = null;
handler = () =>
{
callback.Invoke();
notifier.OnReady -= handler; // Unsubscribe to prevent memory leaks
};
notifier.OnReady += handler;
}
}
}
}