CombineLatest.Generated.tt 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. using System.Reactive.Disposables;
  11. namespace System.Reactive.Linq.ObservableImpl
  12. {
  13. #region [3,16]-ary
  14. /* The following code is generated by a T4 template. */
  15. #region CombineLatest auto-generated code (<#=DateTime.Now.ToString()#>)
  16. <#
  17. for (var i = 3; i <= 16; i++)
  18. {
  19. var ts = string.Join(", ", Enumerable.Range(1, i).Select(j => "T" + j));
  20. var os = string.Join(", ", Enumerable.Range(1, i).Select(j => "IObservable<T" + j + "> source" + j));
  21. var vs = string.Join(", ", Enumerable.Range(1, i).Select(j => "_observer" + j + ".Value"));
  22. #>
  23. internal sealed class CombineLatest<<#=ts#>, TResult> : Producer<TResult>
  24. {
  25. <#
  26. for (var j = 1; j <= i; j++)
  27. {
  28. #>
  29. private readonly IObservable<T<#=j#>> _source<#=j#>;
  30. <#
  31. }
  32. #>
  33. private readonly Func<<#=ts#>, TResult> _resultSelector;
  34. public CombineLatest(<#=os#>, Func<<#=ts#>, TResult> resultSelector)
  35. {
  36. <#
  37. for (var j = 1; j <= i; j++)
  38. {
  39. #>
  40. _source<#=j#> = source<#=j#>;
  41. <#
  42. }
  43. #>
  44. _resultSelector = resultSelector;
  45. }
  46. protected override IDisposable Run(IObserver<TResult> observer, IDisposable cancel, Action<IDisposable> setSink)
  47. {
  48. var sink = new _(this, observer, cancel);
  49. setSink(sink);
  50. return sink.Run();
  51. }
  52. class _ : CombineLatestSink<TResult>
  53. {
  54. private readonly CombineLatest<<#=ts#>, TResult> _parent;
  55. public _(CombineLatest<<#=ts#>, TResult> parent, IObserver<TResult> observer, IDisposable cancel)
  56. : base(<#=i#>, observer, cancel)
  57. {
  58. _parent = parent;
  59. }
  60. <#
  61. for (var j = 1; j <= i; j++)
  62. {
  63. #>
  64. private CombineLatestObserver<T<#=j#>> _observer<#=j#>;
  65. <#
  66. }
  67. #>
  68. public IDisposable Run()
  69. {
  70. var subscriptions = new SingleAssignmentDisposable[<#=i#>];
  71. for (int i = 0; i < <#=i#>; i++)
  72. subscriptions[i] = new SingleAssignmentDisposable();
  73. <#
  74. for (var j = 1; j <= i; j++)
  75. {
  76. #>
  77. _observer<#=j#> = new CombineLatestObserver<T<#=j#>>(_gate, this, <#=j - 1#>, subscriptions[<#=j - 1#>]);
  78. <#
  79. }
  80. #>
  81. <#
  82. for (var j = 1; j <= i; j++)
  83. {
  84. #>
  85. subscriptions[<#=j - 1#>].Disposable = _parent._source<#=j#>.SubscribeSafe(_observer<#=j#>);
  86. <#
  87. }
  88. #>
  89. return StableCompositeDisposable.Create(subscriptions);
  90. }
  91. protected override TResult GetResult()
  92. {
  93. return _parent._resultSelector(<#=vs#>);
  94. }
  95. }
  96. }
  97. <#
  98. }
  99. #>
  100. #endregion
  101. #endregion
  102. }