Skip to content

Commit

Permalink
AT
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Jan 7, 2024
1 parent b914f50 commit 9572a41
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
19 changes: 16 additions & 3 deletions src/R3.Unity/Assets/R3.Unity/Runtime/MonoBehaviourExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Threading;
using System;
using System.Threading;
using UnityEngine;

namespace R3
{
internal static class MonoBehaviourExtensions
public static class MonoBehaviourExtensions
{
public static CancellationToken GetDestroyCancellationToken(this MonoBehaviour value)
internal static CancellationToken GetDestroyCancellationToken(this MonoBehaviour value)
{
// UNITY_2022_2_OR_NEWER has MonoBehavior.destroyCancellationToken
#if UNITY_2022_2_OR_NEWER
Expand All @@ -14,5 +15,17 @@ public static CancellationToken GetDestroyCancellationToken(this MonoBehaviour v
return CancellationToken.None;;
#endif
}

#if UNITY_2022_2_OR_NEWER

public static CancellationTokenRegistration AddTo(this IDisposable disposable, MonoBehaviour value)
{
return value.destroyCancellationToken.Register(state =>
{
((IDisposable)state!).Dispose();
}, disposable, false);
}

#endif
}
}
26 changes: 9 additions & 17 deletions src/R3.Unity/Assets/Scenes/NewBehaviourScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,30 @@

public class NewBehaviourScript : MonoBehaviour
{
IDisposable d;

void Start()
{
var a = Observable.TimerFrame(5, 100)
Observable.TimerFrame(5, 100)
.TakeUntil(this.destroyCancellationToken)
.Subscribe(x =>
{
Debug.Log(Time.time);
});

})
.AddTo(this);


var b = Observable.EveryUpdate()
Observable.EveryUpdate()
.Where(x => true)
.Subscribe(x =>
{
Debug.Log(Time.frameCount);
});
})
.AddTo(this);

var c = Observable.EveryValueChanged(this, x => x.transform, destroyCancellationToken)
Observable.EveryValueChanged(this, x => x.transform, destroyCancellationToken)
.Select(x => x)
.Subscribe(x =>
{
Debug.Log(x);
});

d = Disposable.Combine(a, b, c);
}

void OnDestroy()
{
d.Dispose();
})
.AddTo(this);
}
}
6 changes: 3 additions & 3 deletions src/R3/Disposable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void AddTo(this IDisposable disposable, ICollection<IDisposable> d
disposables.Add(disposable);
}

public static CancellationTokenRegistration RegisterTo(this IDisposable disposable, CancellationToken cancellationToken)
public static CancellationTokenRegistration AddTo(this IDisposable disposable, CancellationToken cancellationToken)
{
if (!cancellationToken.CanBeCanceled) throw new ArgumentException("Require CancellationToken CanBeCanceled");

Expand Down Expand Up @@ -468,9 +468,9 @@ public IDisposable Build()
return result;
}

public CancellationTokenRegistration RegisterTo(CancellationToken cancellationToken)
public CancellationTokenRegistration AddTo(CancellationToken cancellationToken)
{
return Build().RegisterTo(cancellationToken);
return Build().AddTo(cancellationToken);
}

public void Dispose()
Expand Down

0 comments on commit 9572a41

Please sign in to comment.