Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

takeLast.js 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { Subscriber } from '../Subscriber';
  2. import { ArgumentOutOfRangeError } from '../util/ArgumentOutOfRangeError';
  3. import { empty } from '../observable/empty';
  4. export function takeLast(count) {
  5. return function takeLastOperatorFunction(source) {
  6. if (count === 0) {
  7. return empty();
  8. }
  9. else {
  10. return source.lift(new TakeLastOperator(count));
  11. }
  12. };
  13. }
  14. class TakeLastOperator {
  15. constructor(total) {
  16. this.total = total;
  17. if (this.total < 0) {
  18. throw new ArgumentOutOfRangeError;
  19. }
  20. }
  21. call(subscriber, source) {
  22. return source.subscribe(new TakeLastSubscriber(subscriber, this.total));
  23. }
  24. }
  25. class TakeLastSubscriber extends Subscriber {
  26. constructor(destination, total) {
  27. super(destination);
  28. this.total = total;
  29. this.ring = new Array();
  30. this.count = 0;
  31. }
  32. _next(value) {
  33. const ring = this.ring;
  34. const total = this.total;
  35. const count = this.count++;
  36. if (ring.length < total) {
  37. ring.push(value);
  38. }
  39. else {
  40. const index = count % total;
  41. ring[index] = value;
  42. }
  43. }
  44. _complete() {
  45. const destination = this.destination;
  46. let count = this.count;
  47. if (count > 0) {
  48. const total = this.count >= this.total ? this.total : this.count;
  49. const ring = this.ring;
  50. for (let i = 0; i < total; i++) {
  51. const idx = (count++) % total;
  52. destination.next(ring[idx]);
  53. }
  54. }
  55. destination.complete();
  56. }
  57. }
  58. //# sourceMappingURL=takeLast.js.map