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.

scan.js 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { Subscriber } from '../Subscriber';
  2. export function scan(accumulator, seed) {
  3. let hasSeed = false;
  4. if (arguments.length >= 2) {
  5. hasSeed = true;
  6. }
  7. return function scanOperatorFunction(source) {
  8. return source.lift(new ScanOperator(accumulator, seed, hasSeed));
  9. };
  10. }
  11. class ScanOperator {
  12. constructor(accumulator, seed, hasSeed = false) {
  13. this.accumulator = accumulator;
  14. this.seed = seed;
  15. this.hasSeed = hasSeed;
  16. }
  17. call(subscriber, source) {
  18. return source.subscribe(new ScanSubscriber(subscriber, this.accumulator, this.seed, this.hasSeed));
  19. }
  20. }
  21. class ScanSubscriber extends Subscriber {
  22. constructor(destination, accumulator, _seed, hasSeed) {
  23. super(destination);
  24. this.accumulator = accumulator;
  25. this._seed = _seed;
  26. this.hasSeed = hasSeed;
  27. this.index = 0;
  28. }
  29. get seed() {
  30. return this._seed;
  31. }
  32. set seed(value) {
  33. this.hasSeed = true;
  34. this._seed = value;
  35. }
  36. _next(value) {
  37. if (!this.hasSeed) {
  38. this.seed = value;
  39. this.destination.next(value);
  40. }
  41. else {
  42. return this._tryNext(value);
  43. }
  44. }
  45. _tryNext(value) {
  46. const index = this.index++;
  47. let result;
  48. try {
  49. result = this.accumulator(this.seed, value, index);
  50. }
  51. catch (err) {
  52. this.destination.error(err);
  53. }
  54. this.seed = result;
  55. this.destination.next(result);
  56. }
  57. }
  58. //# sourceMappingURL=scan.js.map