last.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. "use strict";
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  4. function __() { this.constructor = d; }
  5. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  6. };
  7. var Subscriber_1 = require('../Subscriber');
  8. var EmptyError_1 = require('../util/EmptyError');
  9. /* tslint:enable:max-line-length */
  10. /**
  11. * Returns an Observable that emits only the last item emitted by the source Observable.
  12. * It optionally takes a predicate function as a parameter, in which case, rather than emitting
  13. * the last item from the source Observable, the resulting Observable will emit the last item
  14. * from the source Observable that satisfies the predicate.
  15. *
  16. * <img src="./img/last.png" width="100%">
  17. *
  18. * @throws {EmptyError} Delivers an EmptyError to the Observer's `error`
  19. * callback if the Observable completes before any `next` notification was sent.
  20. * @param {function} predicate - The condition any source emitted item has to satisfy.
  21. * @return {Observable} An Observable that emits only the last item satisfying the given condition
  22. * from the source, or an NoSuchElementException if no such items are emitted.
  23. * @throws - Throws if no items that match the predicate are emitted by the source Observable.
  24. * @method last
  25. * @owner Observable
  26. */
  27. function last(predicate, resultSelector, defaultValue) {
  28. return function (source) { return source.lift(new LastOperator(predicate, resultSelector, defaultValue, source)); };
  29. }
  30. exports.last = last;
  31. var LastOperator = (function () {
  32. function LastOperator(predicate, resultSelector, defaultValue, source) {
  33. this.predicate = predicate;
  34. this.resultSelector = resultSelector;
  35. this.defaultValue = defaultValue;
  36. this.source = source;
  37. }
  38. LastOperator.prototype.call = function (observer, source) {
  39. return source.subscribe(new LastSubscriber(observer, this.predicate, this.resultSelector, this.defaultValue, this.source));
  40. };
  41. return LastOperator;
  42. }());
  43. /**
  44. * We need this JSDoc comment for affecting ESDoc.
  45. * @ignore
  46. * @extends {Ignored}
  47. */
  48. var LastSubscriber = (function (_super) {
  49. __extends(LastSubscriber, _super);
  50. function LastSubscriber(destination, predicate, resultSelector, defaultValue, source) {
  51. _super.call(this, destination);
  52. this.predicate = predicate;
  53. this.resultSelector = resultSelector;
  54. this.defaultValue = defaultValue;
  55. this.source = source;
  56. this.hasValue = false;
  57. this.index = 0;
  58. if (typeof defaultValue !== 'undefined') {
  59. this.lastValue = defaultValue;
  60. this.hasValue = true;
  61. }
  62. }
  63. LastSubscriber.prototype._next = function (value) {
  64. var index = this.index++;
  65. if (this.predicate) {
  66. this._tryPredicate(value, index);
  67. }
  68. else {
  69. if (this.resultSelector) {
  70. this._tryResultSelector(value, index);
  71. return;
  72. }
  73. this.lastValue = value;
  74. this.hasValue = true;
  75. }
  76. };
  77. LastSubscriber.prototype._tryPredicate = function (value, index) {
  78. var result;
  79. try {
  80. result = this.predicate(value, index, this.source);
  81. }
  82. catch (err) {
  83. this.destination.error(err);
  84. return;
  85. }
  86. if (result) {
  87. if (this.resultSelector) {
  88. this._tryResultSelector(value, index);
  89. return;
  90. }
  91. this.lastValue = value;
  92. this.hasValue = true;
  93. }
  94. };
  95. LastSubscriber.prototype._tryResultSelector = function (value, index) {
  96. var result;
  97. try {
  98. result = this.resultSelector(value, index);
  99. }
  100. catch (err) {
  101. this.destination.error(err);
  102. return;
  103. }
  104. this.lastValue = result;
  105. this.hasValue = true;
  106. };
  107. LastSubscriber.prototype._complete = function () {
  108. var destination = this.destination;
  109. if (this.hasValue) {
  110. destination.next(this.lastValue);
  111. destination.complete();
  112. }
  113. else {
  114. destination.error(new EmptyError_1.EmptyError);
  115. }
  116. };
  117. return LastSubscriber;
  118. }(Subscriber_1.Subscriber));
  119. //# sourceMappingURL=last.js.map