ToAsync.Generated.tt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. <#@ template debug="false" hostspecific="false" language="C#" #>
  5. <#@ assembly name="System.Core" #>
  6. <#@ import namespace="System.Linq" #>
  7. <#@ import namespace="System.Text" #>
  8. <#@ import namespace="System.Collections.Generic" #>
  9. <#@ output extension=".cs" #>
  10. using System.Reactive.Concurrency;
  11. using System.Reactive.Subjects;
  12. namespace System.Reactive.Linq
  13. {
  14. // REVIEW: Consider if these are worth retaining in the async space.
  15. partial class AsyncObservable
  16. {
  17. <#
  18. for (var i = 0; i <= 16; i++)
  19. {
  20. var args = Enumerable.Range(1, i).Select(j => "T" + j).Concat(new[] { "IAsyncObservable<TResult>" });
  21. var ret = "Func<" + string.Join(", ", args) + ">";
  22. var genArgs = string.Join(", ", Enumerable.Range(1, i).Select(j => "T" + j).Concat(new[] { "TResult" }));
  23. var pars = string.Join(", ", Enumerable.Range(1, i).Select(j => "arg" + j));
  24. var type = "Func<" + string.Join(", ", Enumerable.Range(1, i).Select(j => "T" + j).Concat(new[] { "TResult" })) + ">";
  25. #>
  26. public static <#=ret#> ToAsync<<#=genArgs#>>(<#=type#> function)
  27. {
  28. if (function == null)
  29. throw new ArgumentNullException(nameof(function));
  30. return ToAsync(function, TaskPoolAsyncScheduler.Default);
  31. }
  32. public static <#=ret#> ToAsync<<#=genArgs#>>(<#=type#> function, IAsyncScheduler scheduler)
  33. {
  34. if (function == null)
  35. throw new ArgumentNullException(nameof(function));
  36. if (scheduler == null)
  37. throw new ArgumentNullException(nameof(scheduler));
  38. return (<#=pars#>) =>
  39. {
  40. var subject = new SequentialAsyncAsyncSubject<TResult>();
  41. // NB: We don't do anything with the result of scheduling the action; it can't be cancelled.
  42. scheduler.ScheduleAsync(async ct =>
  43. {
  44. TResult res;
  45. try
  46. {
  47. res = function(<#=pars#>);
  48. }
  49. catch (Exception ex)
  50. {
  51. await subject.OnErrorAsync(ex).RendezVous(scheduler);
  52. return;
  53. }
  54. await subject.OnNextAsync(res).RendezVous(scheduler);
  55. await subject.OnCompletedAsync().RendezVous(scheduler);
  56. });
  57. return subject.AsAsyncObservable();
  58. };
  59. }
  60. <#
  61. }
  62. #>
  63. <#
  64. for (var i = 0; i <= 16; i++)
  65. {
  66. var args = Enumerable.Range(1, i).Select(j => "T" + j).Concat(new[] { "IAsyncObservable<Unit>" });
  67. var ret = "Func<" + string.Join(", ", args) + ">";
  68. var genArgs = string.Join(", ", Enumerable.Range(1, i).Select(j => "T" + j));
  69. var pars = string.Join(", ", Enumerable.Range(1, i).Select(j => "arg" + j));
  70. var type = "Action";
  71. if (genArgs != "")
  72. {
  73. genArgs = "<" + genArgs + ">";
  74. type += genArgs;
  75. }
  76. #>
  77. public static <#=ret#> ToAsync<#=genArgs#>(<#=type#> action)
  78. {
  79. if (action == null)
  80. throw new ArgumentNullException(nameof(action));
  81. return ToAsync(action, TaskPoolAsyncScheduler.Default);
  82. }
  83. public static <#=ret#> ToAsync<#=genArgs#>(<#=type#> action, IAsyncScheduler scheduler)
  84. {
  85. if (action == null)
  86. throw new ArgumentNullException(nameof(action));
  87. if (scheduler == null)
  88. throw new ArgumentNullException(nameof(scheduler));
  89. return (<#=pars#>) =>
  90. {
  91. var subject = new SequentialAsyncAsyncSubject<Unit>();
  92. // NB: We don't do anything with the result of scheduling the action; it can't be cancelled.
  93. scheduler.ScheduleAsync(async ct =>
  94. {
  95. try
  96. {
  97. action(<#=pars#>);
  98. }
  99. catch (Exception ex)
  100. {
  101. await subject.OnErrorAsync(ex).RendezVous(scheduler);
  102. return;
  103. }
  104. await subject.OnNextAsync(Unit.Default).RendezVous(scheduler);
  105. await subject.OnCompletedAsync().RendezVous(scheduler);
  106. });
  107. return subject.AsAsyncObservable();
  108. };
  109. }
  110. <#
  111. }
  112. #>
  113. }
  114. }