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.

single.js 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. }
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. var Subscriber_1 = require("../Subscriber");
  17. var EmptyError_1 = require("../util/EmptyError");
  18. function single(predicate) {
  19. return function (source) { return source.lift(new SingleOperator(predicate, source)); };
  20. }
  21. exports.single = single;
  22. var SingleOperator = (function () {
  23. function SingleOperator(predicate, source) {
  24. this.predicate = predicate;
  25. this.source = source;
  26. }
  27. SingleOperator.prototype.call = function (subscriber, source) {
  28. return source.subscribe(new SingleSubscriber(subscriber, this.predicate, this.source));
  29. };
  30. return SingleOperator;
  31. }());
  32. var SingleSubscriber = (function (_super) {
  33. __extends(SingleSubscriber, _super);
  34. function SingleSubscriber(destination, predicate, source) {
  35. var _this = _super.call(this, destination) || this;
  36. _this.predicate = predicate;
  37. _this.source = source;
  38. _this.seenValue = false;
  39. _this.index = 0;
  40. return _this;
  41. }
  42. SingleSubscriber.prototype.applySingleValue = function (value) {
  43. if (this.seenValue) {
  44. this.destination.error('Sequence contains more than one element');
  45. }
  46. else {
  47. this.seenValue = true;
  48. this.singleValue = value;
  49. }
  50. };
  51. SingleSubscriber.prototype._next = function (value) {
  52. var index = this.index++;
  53. if (this.predicate) {
  54. this.tryNext(value, index);
  55. }
  56. else {
  57. this.applySingleValue(value);
  58. }
  59. };
  60. SingleSubscriber.prototype.tryNext = function (value, index) {
  61. try {
  62. if (this.predicate(value, index, this.source)) {
  63. this.applySingleValue(value);
  64. }
  65. }
  66. catch (err) {
  67. this.destination.error(err);
  68. }
  69. };
  70. SingleSubscriber.prototype._complete = function () {
  71. var destination = this.destination;
  72. if (this.index > 0) {
  73. destination.next(this.seenValue ? this.singleValue : undefined);
  74. destination.complete();
  75. }
  76. else {
  77. destination.error(new EmptyError_1.EmptyError);
  78. }
  79. };
  80. return SingleSubscriber;
  81. }(Subscriber_1.Subscriber));
  82. //# sourceMappingURL=single.js.map