2
0

ObservableDebugExtensions.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. namespace UniRx.Diagnostics
  3. {
  4. public static class ObservableDebugExtensions
  5. {
  6. /// <summary>
  7. /// Debug helper of observbale stream. Works for only DEBUG symbol.
  8. /// </summary>
  9. public static IObservable<T> Debug<T>(this IObservable<T> source, string label = null)
  10. {
  11. #if DEBUG
  12. var l = (label == null) ? "" : "[" + label + "]";
  13. return source.Materialize()
  14. .Do(x => UnityEngine.Debug.Log(l + x.ToString()))
  15. .Dematerialize()
  16. .DoOnCancel(() => UnityEngine.Debug.Log(l + "OnCancel"))
  17. .DoOnSubscribe(() => UnityEngine.Debug.Log(l + "OnSubscribe"));
  18. #else
  19. return source;
  20. #endif
  21. }
  22. /// <summary>
  23. /// Debug helper of observbale stream. Works for only DEBUG symbol.
  24. /// </summary>
  25. public static IObservable<T> Debug<T>(this IObservable<T> source, UniRx.Diagnostics.Logger logger)
  26. {
  27. #if DEBUG
  28. return source.Materialize()
  29. .Do(x => logger.Debug(x.ToString()))
  30. .Dematerialize()
  31. .DoOnCancel(() => logger.Debug("OnCancel"))
  32. .DoOnSubscribe(() => logger.Debug("OnSubscribe"));
  33. #else
  34. return source;
  35. #endif
  36. }
  37. }
  38. }