feat: add opt-in MonoBehaviour lifecycle weaving
Publish UPM package / publish (push) Successful in 2s
Publish UPM package / publish (push) Successful in 2s
This commit is contained in:
@@ -14,6 +14,41 @@ namespace ShrinkEventBus.PlayMode.Tests
|
||||
public int Value { get; }
|
||||
}
|
||||
|
||||
internal readonly struct AwakeToDestroyEvent : IShrinkEvent
|
||||
{
|
||||
public AwakeToDestroyEvent(int value) => Value = value;
|
||||
public int Value { get; }
|
||||
}
|
||||
|
||||
[ShrinkEventSubscriber(
|
||||
DefaultBus = "game",
|
||||
Lifetime = ShrinkSubscriberLifetime.AwakeToDestroy)]
|
||||
internal sealed class AwakeToDestroyTarget : MonoBehaviour
|
||||
{
|
||||
public static int Sum { get; set; }
|
||||
public static int AwakeCalls { get; set; }
|
||||
public static int DestroyCalls { get; set; }
|
||||
|
||||
private void Awake() => AwakeCalls++;
|
||||
private void OnDestroy() => DestroyCalls++;
|
||||
|
||||
[ShrinkSubscribe]
|
||||
private void OnEvent(AwakeToDestroyEvent value) => Sum += value.Value;
|
||||
}
|
||||
|
||||
[ShrinkEventSubscriber(Lifetime = ShrinkSubscriberLifetime.AwakeToDestroy)]
|
||||
internal abstract class AbstractAwakeToDestroyTarget : MonoBehaviour
|
||||
{
|
||||
public static int Sum { get; set; }
|
||||
|
||||
[ShrinkSubscribe]
|
||||
private void OnEvent(AwakeToDestroyEvent value) => Sum += value.Value;
|
||||
}
|
||||
|
||||
internal sealed class ConcreteAwakeToDestroyTarget : AbstractAwakeToDestroyTarget
|
||||
{
|
||||
}
|
||||
|
||||
[ShrinkEventSubscriber(DefaultBus = "game")]
|
||||
internal sealed class MonoLifecycleTarget : MonoBehaviour
|
||||
{
|
||||
@@ -25,6 +60,53 @@ namespace ShrinkEventBus.PlayMode.Tests
|
||||
|
||||
public sealed class ShrinkMonoEventScopePlayModeTests
|
||||
{
|
||||
[UnityTest]
|
||||
public IEnumerator AwakeToDestroyLifetimeIsInjectedWithoutManualBinding()
|
||||
{
|
||||
AwakeToDestroyTarget.Sum = 0;
|
||||
AwakeToDestroyTarget.AwakeCalls = 0;
|
||||
AwakeToDestroyTarget.DestroyCalls = 0;
|
||||
var gameObject = new GameObject("ShrinkEventBus-AwakeToDestroy");
|
||||
var target = gameObject.AddComponent<AwakeToDestroyTarget>();
|
||||
|
||||
yield return null;
|
||||
Assert.IsInstanceOf<IShrinkGeneratedSubscriber>(target);
|
||||
Assert.AreEqual(1, AwakeToDestroyTarget.AwakeCalls);
|
||||
yield return EventBus.PostAsync(new AwakeToDestroyEvent(3)).ToCoroutine();
|
||||
Assert.AreEqual(3, AwakeToDestroyTarget.Sum);
|
||||
|
||||
gameObject.SetActive(false);
|
||||
yield return null;
|
||||
yield return EventBus.PostAsync(new AwakeToDestroyEvent(5)).ToCoroutine();
|
||||
Assert.AreEqual(8, AwakeToDestroyTarget.Sum,
|
||||
"AwakeToDestroy subscriptions must survive OnDisable.");
|
||||
|
||||
Object.Destroy(gameObject);
|
||||
yield return null;
|
||||
Assert.AreEqual(1, AwakeToDestroyTarget.DestroyCalls);
|
||||
yield return EventBus.PostAsync(new AwakeToDestroyEvent(7)).ToCoroutine();
|
||||
Assert.AreEqual(8, AwakeToDestroyTarget.Sum,
|
||||
"The generated OnDestroy release must remove the subscription.");
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator AwakeToDestroyLifetimeSupportsAbstractSubscriberBasesWithoutLifecycleMethods()
|
||||
{
|
||||
AbstractAwakeToDestroyTarget.Sum = 0;
|
||||
var gameObject = new GameObject("ShrinkEventBus-AbstractAwakeToDestroy");
|
||||
var target = gameObject.AddComponent<ConcreteAwakeToDestroyTarget>();
|
||||
|
||||
yield return null;
|
||||
Assert.IsInstanceOf<IShrinkGeneratedSubscriber>(target);
|
||||
yield return EventBus.PostAsync(new AwakeToDestroyEvent(11)).ToCoroutine();
|
||||
Assert.AreEqual(11, AbstractAwakeToDestroyTarget.Sum);
|
||||
|
||||
Object.Destroy(gameObject);
|
||||
yield return null;
|
||||
yield return EventBus.PostAsync(new AwakeToDestroyEvent(13)).ToCoroutine();
|
||||
Assert.AreEqual(11, AbstractAwakeToDestroyTarget.Sum);
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator OnEnableAttachesAndOnDisableReleases()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user