64 lines
1.9 KiB
C#
64 lines
1.9 KiB
C#
using System;
|
|
|
|
namespace Minigames.StatueDressup
|
|
{
|
|
/// <summary>
|
|
/// Interface for managers that load asynchronously and notify when ready.
|
|
/// Allows dependent components to safely access the manager via WhenReady callbacks.
|
|
/// </summary>
|
|
public interface IReadyNotifier
|
|
{
|
|
/// <summary>
|
|
/// True when the manager has finished initialization and is ready to use
|
|
/// </summary>
|
|
bool IsReady { get; }
|
|
|
|
/// <summary>
|
|
/// Event invoked when the manager becomes ready
|
|
/// </summary>
|
|
event Action OnReady;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extension methods for IReadyNotifier to provide common callback behavior
|
|
/// </summary>
|
|
public static class ReadyNotifierExtensions
|
|
{
|
|
/// <summary>
|
|
/// Execute callback when ready. If already ready, executes immediately.
|
|
/// If not ready yet, subscribes to OnReady event and executes when fired.
|
|
/// </summary>
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|