CombineLatest.Generated.tt 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <#@ template debug="false" hostspecific="false" language="C#" #>
  2. <#@ assembly name="System.Core" #>
  3. <#@ import namespace="System.Linq" #>
  4. <#@ import namespace="System.Text" #>
  5. <#@ import namespace="System.Collections.Generic" #>
  6. <#@ output extension=".cs" #>
  7. // Licensed to the .NET Foundation under one or more agreements.
  8. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  9. // See the LICENSE file in the project root for more information.
  10. #if !NO_PERF
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Collections.ObjectModel;
  14. using System.Linq;
  15. using System.Reactive.Disposables;
  16. namespace System.Reactive.Linq.ObservableImpl
  17. {
  18. #region [3,16]-ary
  19. /* The following code is generated by a T4 template. */
  20. #region CombineLatest auto-generated code (<#=DateTime.Now.ToString()#>)
  21. <#
  22. for (var i = 3; i <= 16; i++)
  23. {
  24. var ts = string.Join(", ", Enumerable.Range(1, i).Select(j => "T" + j));
  25. var os = string.Join(", ", Enumerable.Range(1, i).Select(j => "IObservable<T" + j + "> source" + j));
  26. var vs = string.Join(", ", Enumerable.Range(1, i).Select(j => "_observer" + j + ".Value"));
  27. if (i == 5)
  28. {
  29. #>
  30. #if !NO_LARGEARITY
  31. <#
  32. }
  33. #>
  34. class CombineLatest<<#=ts#>, TResult> : Producer<TResult>
  35. {
  36. <#
  37. for (var j = 1; j <= i; j++)
  38. {
  39. #>
  40. private readonly IObservable<T<#=j#>> _source<#=j#>;
  41. <#
  42. }
  43. #>
  44. private readonly Func<<#=ts#>, TResult> _resultSelector;
  45. public CombineLatest(<#=os#>, Func<<#=ts#>, TResult> resultSelector)
  46. {
  47. <#
  48. for (var j = 1; j <= i; j++)
  49. {
  50. #>
  51. _source<#=j#> = source<#=j#>;
  52. <#
  53. }
  54. #>
  55. _resultSelector = resultSelector;
  56. }
  57. protected override IDisposable Run(IObserver<TResult> observer, IDisposable cancel, Action<IDisposable> setSink)
  58. {
  59. var sink = new _(this, observer, cancel);
  60. setSink(sink);
  61. return sink.Run();
  62. }
  63. class _ : CombineLatestSink<TResult>
  64. {
  65. private readonly CombineLatest<<#=ts#>, TResult> _parent;
  66. public _(CombineLatest<<#=ts#>, TResult> parent, IObserver<TResult> observer, IDisposable cancel)
  67. : base(<#=i#>, observer, cancel)
  68. {
  69. _parent = parent;
  70. }
  71. <#
  72. for (var j = 1; j <= i; j++)
  73. {
  74. #>
  75. private CombineLatestObserver<T<#=j#>> _observer<#=j#>;
  76. <#
  77. }
  78. #>
  79. public IDisposable Run()
  80. {
  81. var subscriptions = new SingleAssignmentDisposable[<#=i#>];
  82. for (int i = 0; i < <#=i#>; i++)
  83. subscriptions[i] = new SingleAssignmentDisposable();
  84. <#
  85. for (var j = 1; j <= i; j++)
  86. {
  87. #>
  88. _observer<#=j#> = new CombineLatestObserver<T<#=j#>>(_gate, this, <#=j - 1#>, subscriptions[<#=j - 1#>]);
  89. <#
  90. }
  91. #>
  92. <#
  93. for (var j = 1; j <= i; j++)
  94. {
  95. #>
  96. subscriptions[<#=j - 1#>].Disposable = _parent._source<#=j#>.SubscribeSafe(_observer<#=j#>);
  97. <#
  98. }
  99. #>
  100. return StableCompositeDisposable.Create(subscriptions);
  101. }
  102. protected override TResult GetResult()
  103. {
  104. return _parent._resultSelector(<#=vs#>);
  105. }
  106. }
  107. }
  108. <#
  109. }
  110. #>
  111. #endif
  112. #endregion
  113. #endregion
  114. }
  115. #endif