sequenceEqual.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { operate } from '../util/lift';
  2. import { createOperatorSubscriber } from './OperatorSubscriber';
  3. import { innerFrom } from '../observable/innerFrom';
  4. export function sequenceEqual(compareTo, comparator = (a, b) => a === b) {
  5. return operate((source, subscriber) => {
  6. const aState = createState();
  7. const bState = createState();
  8. const emit = (isEqual) => {
  9. subscriber.next(isEqual);
  10. subscriber.complete();
  11. };
  12. const createSubscriber = (selfState, otherState) => {
  13. const sequenceEqualSubscriber = createOperatorSubscriber(subscriber, (a) => {
  14. const { buffer, complete } = otherState;
  15. if (buffer.length === 0) {
  16. complete ? emit(false) : selfState.buffer.push(a);
  17. }
  18. else {
  19. !comparator(a, buffer.shift()) && emit(false);
  20. }
  21. }, () => {
  22. selfState.complete = true;
  23. const { complete, buffer } = otherState;
  24. complete && emit(buffer.length === 0);
  25. sequenceEqualSubscriber === null || sequenceEqualSubscriber === void 0 ? void 0 : sequenceEqualSubscriber.unsubscribe();
  26. });
  27. return sequenceEqualSubscriber;
  28. };
  29. source.subscribe(createSubscriber(aState, bState));
  30. innerFrom(compareTo).subscribe(createSubscriber(bState, aState));
  31. });
  32. }
  33. function createState() {
  34. return {
  35. buffer: [],
  36. complete: false,
  37. };
  38. }
  39. //# sourceMappingURL=sequenceEqual.js.map