CombineLatest.Generated.tt 3.1 KB

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