takeLast.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /** PURE_IMPORTS_START tslib,_Subscriber,_util_ArgumentOutOfRangeError,_observable_empty PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { Subscriber } from '../Subscriber';
  4. import { ArgumentOutOfRangeError } from '../util/ArgumentOutOfRangeError';
  5. import { empty } from '../observable/empty';
  6. export function takeLast(count) {
  7. return function takeLastOperatorFunction(source) {
  8. if (count === 0) {
  9. return empty();
  10. }
  11. else {
  12. return source.lift(new TakeLastOperator(count));
  13. }
  14. };
  15. }
  16. var TakeLastOperator = /*@__PURE__*/ (function () {
  17. function TakeLastOperator(total) {
  18. this.total = total;
  19. if (this.total < 0) {
  20. throw new ArgumentOutOfRangeError;
  21. }
  22. }
  23. TakeLastOperator.prototype.call = function (subscriber, source) {
  24. return source.subscribe(new TakeLastSubscriber(subscriber, this.total));
  25. };
  26. return TakeLastOperator;
  27. }());
  28. var TakeLastSubscriber = /*@__PURE__*/ (function (_super) {
  29. tslib_1.__extends(TakeLastSubscriber, _super);
  30. function TakeLastSubscriber(destination, total) {
  31. var _this = _super.call(this, destination) || this;
  32. _this.total = total;
  33. _this.ring = new Array();
  34. _this.count = 0;
  35. return _this;
  36. }
  37. TakeLastSubscriber.prototype._next = function (value) {
  38. var ring = this.ring;
  39. var total = this.total;
  40. var count = this.count++;
  41. if (ring.length < total) {
  42. ring.push(value);
  43. }
  44. else {
  45. var index = count % total;
  46. ring[index] = value;
  47. }
  48. };
  49. TakeLastSubscriber.prototype._complete = function () {
  50. var destination = this.destination;
  51. var count = this.count;
  52. if (count > 0) {
  53. var total = this.count >= this.total ? this.total : this.count;
  54. var ring = this.ring;
  55. for (var i = 0; i < total; i++) {
  56. var idx = (count++) % total;
  57. destination.next(ring[idx]);
  58. }
  59. }
  60. destination.complete();
  61. };
  62. return TakeLastSubscriber;
  63. }(Subscriber));
  64. //# sourceMappingURL=takeLast.js.map