Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

find.js 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { Subscriber } from '../Subscriber';
  2. export function find(predicate, thisArg) {
  3. if (typeof predicate !== 'function') {
  4. throw new TypeError('predicate is not a function');
  5. }
  6. return (source) => source.lift(new FindValueOperator(predicate, source, false, thisArg));
  7. }
  8. export class FindValueOperator {
  9. constructor(predicate, source, yieldIndex, thisArg) {
  10. this.predicate = predicate;
  11. this.source = source;
  12. this.yieldIndex = yieldIndex;
  13. this.thisArg = thisArg;
  14. }
  15. call(observer, source) {
  16. return source.subscribe(new FindValueSubscriber(observer, this.predicate, this.source, this.yieldIndex, this.thisArg));
  17. }
  18. }
  19. export class FindValueSubscriber extends Subscriber {
  20. constructor(destination, predicate, source, yieldIndex, thisArg) {
  21. super(destination);
  22. this.predicate = predicate;
  23. this.source = source;
  24. this.yieldIndex = yieldIndex;
  25. this.thisArg = thisArg;
  26. this.index = 0;
  27. }
  28. notifyComplete(value) {
  29. const destination = this.destination;
  30. destination.next(value);
  31. destination.complete();
  32. this.unsubscribe();
  33. }
  34. _next(value) {
  35. const { predicate, thisArg } = this;
  36. const index = this.index++;
  37. try {
  38. const result = predicate.call(thisArg || this, value, index, this.source);
  39. if (result) {
  40. this.notifyComplete(this.yieldIndex ? index : value);
  41. }
  42. }
  43. catch (err) {
  44. this.destination.error(err);
  45. }
  46. }
  47. _complete() {
  48. this.notifyComplete(this.yieldIndex ? -1 : undefined);
  49. }
  50. }
  51. //# sourceMappingURL=find.js.map