find.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /** PURE_IMPORTS_START tslib,_Subscriber PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { Subscriber } from '../Subscriber';
  4. export function find(predicate, thisArg) {
  5. if (typeof predicate !== 'function') {
  6. throw new TypeError('predicate is not a function');
  7. }
  8. return function (source) { return source.lift(new FindValueOperator(predicate, source, false, thisArg)); };
  9. }
  10. var FindValueOperator = /*@__PURE__*/ (function () {
  11. function FindValueOperator(predicate, source, yieldIndex, thisArg) {
  12. this.predicate = predicate;
  13. this.source = source;
  14. this.yieldIndex = yieldIndex;
  15. this.thisArg = thisArg;
  16. }
  17. FindValueOperator.prototype.call = function (observer, source) {
  18. return source.subscribe(new FindValueSubscriber(observer, this.predicate, this.source, this.yieldIndex, this.thisArg));
  19. };
  20. return FindValueOperator;
  21. }());
  22. export { FindValueOperator };
  23. var FindValueSubscriber = /*@__PURE__*/ (function (_super) {
  24. tslib_1.__extends(FindValueSubscriber, _super);
  25. function FindValueSubscriber(destination, predicate, source, yieldIndex, thisArg) {
  26. var _this = _super.call(this, destination) || this;
  27. _this.predicate = predicate;
  28. _this.source = source;
  29. _this.yieldIndex = yieldIndex;
  30. _this.thisArg = thisArg;
  31. _this.index = 0;
  32. return _this;
  33. }
  34. FindValueSubscriber.prototype.notifyComplete = function (value) {
  35. var destination = this.destination;
  36. destination.next(value);
  37. destination.complete();
  38. this.unsubscribe();
  39. };
  40. FindValueSubscriber.prototype._next = function (value) {
  41. var _a = this, predicate = _a.predicate, thisArg = _a.thisArg;
  42. var index = this.index++;
  43. try {
  44. var result = predicate.call(thisArg || this, value, index, this.source);
  45. if (result) {
  46. this.notifyComplete(this.yieldIndex ? index : value);
  47. }
  48. }
  49. catch (err) {
  50. this.destination.error(err);
  51. }
  52. };
  53. FindValueSubscriber.prototype._complete = function () {
  54. this.notifyComplete(this.yieldIndex ? -1 : undefined);
  55. };
  56. return FindValueSubscriber;
  57. }(Subscriber));
  58. export { FindValueSubscriber };
  59. //# sourceMappingURL=find.js.map