AsyncPlan.Generated.tt 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.Collections.Generic;
  11. using System.Threading.Tasks;
  12. namespace System.Reactive.Joins
  13. {
  14. <#
  15. for (var i = 1; i <= 16; i++)
  16. {
  17. var genArgs = string.Join(", ", Enumerable.Range(1, i).Select(j => "TSource" + j));
  18. var args = string.Join(", ", Enumerable.Range(1, i).Select(j => "IObservable<TSource" + j + "> source" + j));
  19. var pars = string.Join(", ", Enumerable.Range(1, i).Select(j => "arg" + j));
  20. #>
  21. public class AsyncPlan<<#=genArgs#>, TResult> : AsyncPlan<TResult>
  22. {
  23. public AsyncPattern<<#=genArgs#>> Expression { get; }
  24. public Func<<#=genArgs#>, TResult> Selector { get; }
  25. internal AsyncPlan(AsyncPattern<<#=genArgs#>> expression, Func<<#=genArgs#>, TResult> selector)
  26. {
  27. Expression = expression;
  28. Selector = selector;
  29. }
  30. internal override ActiveAsyncPlan Activate(Dictionary<object, IAsyncJoinObserver> externalSubscriptions, IAsyncObserver<TResult> observer, Func<ActiveAsyncPlan, Task> deactivate)
  31. {
  32. var onError = new Func<Exception, Task>(observer.OnErrorAsync);
  33. <#
  34. for (var j = 1; j <= i; j++)
  35. {
  36. #>
  37. var joinObserver<#=j#> = AsyncPlan<TResult>.CreateObserver<TSource<#=j#>>(externalSubscriptions, this.Expression.Source<#=j#>, onError);
  38. <#
  39. }
  40. #>
  41. var activePlan = default(ActiveAsyncPlan<<#=genArgs#>>);
  42. activePlan = new ActiveAsyncPlan<<#=genArgs#>>(
  43. <#
  44. for (var j = 1; j <= i; j++)
  45. {
  46. #>
  47. joinObserver<#=j#>,
  48. <#
  49. }
  50. #>
  51. async (<#=pars#>) =>
  52. {
  53. var res = default(TResult);
  54. try
  55. {
  56. res = Selector(<#=pars#>);
  57. }
  58. catch (Exception ex)
  59. {
  60. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  61. return;
  62. }
  63. await observer.OnNextAsync(res).ConfigureAwait(false);
  64. },
  65. async () =>
  66. {
  67. <#
  68. for (var j = 1; j <= i; j++)
  69. {
  70. #>
  71. await joinObserver<#=j#>.RemoveActivePlan(activePlan).ConfigureAwait(false);
  72. <#
  73. }
  74. #>
  75. await deactivate(activePlan).ConfigureAwait(false);
  76. }
  77. );
  78. <#
  79. for (var j = 1; j <= i; j++)
  80. {
  81. #>
  82. joinObserver<#=j#>.AddActivePlan(activePlan);
  83. <#
  84. }
  85. #>
  86. return activePlan;
  87. }
  88. }
  89. <#
  90. }
  91. #>
  92. }