last.js 4.4KB

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