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.

take.js 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { Subscriber } from '../Subscriber';
  2. import { ArgumentOutOfRangeError } from '../util/ArgumentOutOfRangeError';
  3. import { empty } from '../observable/empty';
  4. export function take(count) {
  5. return (source) => {
  6. if (count === 0) {
  7. return empty();
  8. }
  9. else {
  10. return source.lift(new TakeOperator(count));
  11. }
  12. };
  13. }
  14. class TakeOperator {
  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 TakeSubscriber(subscriber, this.total));
  23. }
  24. }
  25. class TakeSubscriber extends Subscriber {
  26. constructor(destination, total) {
  27. super(destination);
  28. this.total = total;
  29. this.count = 0;
  30. }
  31. _next(value) {
  32. const total = this.total;
  33. const count = ++this.count;
  34. if (count <= total) {
  35. this.destination.next(value);
  36. if (count === total) {
  37. this.destination.complete();
  38. this.unsubscribe();
  39. }
  40. }
  41. }
  42. }
  43. //# sourceMappingURL=take.js.map