takeLast.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use strict";
  2. var __values = (this && this.__values) || function(o) {
  3. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  4. if (m) return m.call(o);
  5. if (o && typeof o.length === "number") return {
  6. next: function () {
  7. if (o && i >= o.length) o = void 0;
  8. return { value: o && o[i++], done: !o };
  9. }
  10. };
  11. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. exports.takeLast = void 0;
  15. var empty_1 = require("../observable/empty");
  16. var lift_1 = require("../util/lift");
  17. var OperatorSubscriber_1 = require("./OperatorSubscriber");
  18. function takeLast(count) {
  19. return count <= 0
  20. ? function () { return empty_1.EMPTY; }
  21. : lift_1.operate(function (source, subscriber) {
  22. var buffer = [];
  23. source.subscribe(OperatorSubscriber_1.createOperatorSubscriber(subscriber, function (value) {
  24. buffer.push(value);
  25. count < buffer.length && buffer.shift();
  26. }, function () {
  27. var e_1, _a;
  28. try {
  29. for (var buffer_1 = __values(buffer), buffer_1_1 = buffer_1.next(); !buffer_1_1.done; buffer_1_1 = buffer_1.next()) {
  30. var value = buffer_1_1.value;
  31. subscriber.next(value);
  32. }
  33. }
  34. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  35. finally {
  36. try {
  37. if (buffer_1_1 && !buffer_1_1.done && (_a = buffer_1.return)) _a.call(buffer_1);
  38. }
  39. finally { if (e_1) throw e_1.error; }
  40. }
  41. subscriber.complete();
  42. }, undefined, function () {
  43. buffer = null;
  44. }));
  45. });
  46. }
  47. exports.takeLast = takeLast;
  48. //# sourceMappingURL=takeLast.js.map