RunnableTResult.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. namespace Terminal.Gui.ViewBase;
  2. /// <summary>
  3. /// Base implementation of <see cref="IRunnable{TResult}"/> for views that can be run as blocking sessions.
  4. /// </summary>
  5. /// <typeparam name="TResult">The type of result data returned when the session completes.</typeparam>
  6. /// <remarks>
  7. /// <para>
  8. /// Views can derive from this class or implement <see cref="IRunnable{TResult}"/> directly.
  9. /// </para>
  10. /// <para>
  11. /// This class provides default implementations of the <see cref="IRunnable{TResult}"/> interface
  12. /// following the Terminal.Gui Cancellable Work Pattern (CWP).
  13. /// </para>
  14. /// <para>
  15. /// For views that don't need to return a result, use <see cref="Runnable"/> instead.
  16. /// </para>
  17. /// <para>
  18. /// This class inherits from <see cref="Runnable"/> to avoid code duplication and ensure consistent behavior.
  19. /// </para>
  20. /// </remarks>
  21. public class Runnable<TResult> : Runnable, IRunnable<TResult>
  22. {
  23. /// <summary>
  24. /// Constructs a new instance of the <see cref="Runnable{TResult}"/> class.
  25. /// </summary>
  26. public Runnable ()
  27. {
  28. // Base constructor handles common initialization
  29. }
  30. /// <inheritdoc/>
  31. public new TResult? Result
  32. {
  33. get => base.Result is TResult typedValue ? typedValue : default;
  34. set => base.Result = value;
  35. }
  36. /// <summary>
  37. /// Override to clear typed result when starting.
  38. /// Called by base <see cref="Runnable.RaiseIsRunningChanging"/> before events are raised.
  39. /// </summary>
  40. protected override bool OnIsRunningChanging (bool oldIsRunning, bool newIsRunning)
  41. {
  42. // Clear previous typed result when starting
  43. if (newIsRunning)
  44. {
  45. Result = default;
  46. }
  47. // Call base implementation to allow further customization
  48. return base.OnIsRunningChanging (oldIsRunning, newIsRunning);
  49. }
  50. }