takeLast.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  1. import { __values } from "tslib";
  2. import { EMPTY } from '../observable/empty';
  3. import { operate } from '../util/lift';
  4. import { createOperatorSubscriber } from './OperatorSubscriber';
  5. export function takeLast(count) {
  6. return count <= 0
  7. ? function () { return EMPTY; }
  8. : operate(function (source, subscriber) {
  9. var buffer = [];
  10. source.subscribe(createOperatorSubscriber(subscriber, function (value) {
  11. buffer.push(value);
  12. count < buffer.length && buffer.shift();
  13. }, function () {
  14. var e_1, _a;
  15. try {
  16. for (var buffer_1 = __values(buffer), buffer_1_1 = buffer_1.next(); !buffer_1_1.done; buffer_1_1 = buffer_1.next()) {
  17. var value = buffer_1_1.value;
  18. subscriber.next(value);
  19. }
  20. }
  21. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  22. finally {
  23. try {
  24. if (buffer_1_1 && !buffer_1_1.done && (_a = buffer_1.return)) _a.call(buffer_1);
  25. }
  26. finally { if (e_1) throw e_1.error; }
  27. }
  28. subscriber.complete();
  29. }, undefined, function () {
  30. buffer = null;
  31. }));
  32. });
  33. }
  34. //# sourceMappingURL=takeLast.js.map