UNPKG

@reactivex/rxjs

Version:

Reactive Extensions for modern JavaScript

186 lines 6.96 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); } return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var Subscriber_1 = require("../Subscriber"); /** * Compares all values of two observables in sequence using an optional comparator function * and returns an observable of a single boolean value representing whether or not the two sequences * are equal. * * <span class="informal">Checks to see of all values emitted by both observables are equal, in order.</span> * * ![](sequenceEqual.png) * * `sequenceEqual` subscribes to two observables and buffers incoming values from each observable. Whenever either * observable emits a value, the value is buffered and the buffers are shifted and compared from the bottom * up; If any value pair doesn't match, the returned observable will emit `false` and complete. If one of the * observables completes, the operator will wait for the other observable to complete; If the other * observable emits before completing, the returned observable will emit `false` and complete. If one observable never * completes or emits after the other complets, the returned observable will never complete. * * ## Example * figure out if the Konami code matches * ```ts * import { from, fromEvent } from 'rxjs'; * import { sequenceEqual, bufferCount, mergeMap, map } from 'rxjs/operators'; * * const codes = from([ * 'ArrowUp', * 'ArrowUp', * 'ArrowDown', * 'ArrowDown', * 'ArrowLeft', * 'ArrowRight', * 'ArrowLeft', * 'ArrowRight', * 'KeyB', * 'KeyA', * 'Enter', // no start key, clearly. * ]); * * const keys = fromEvent(document, 'keyup').pipe(map(e => e.code)); * const matches = keys.pipe( * bufferCount(11, 1), * mergeMap( * last11 => from(last11).pipe(sequenceEqual(codes)), * ), * ); * matches.subscribe(matched => console.log('Successful cheat at Contra? ', matched)); * ``` * * @see {@link combineLatest} * @see {@link zip} * @see {@link withLatestFrom} * * @param {Observable} compareTo The observable sequence to compare the source sequence to. * @param {function} [comparator] An optional function to compare each value pair * @return {Observable} An Observable of a single boolean value representing whether or not * the values emitted by both observables were equal in sequence. * @method sequenceEqual * @owner Observable */ function sequenceEqual(compareTo, comparator) { return function (source) { return source.lift(new SequenceEqualOperator(compareTo, comparator)); }; } exports.sequenceEqual = sequenceEqual; var SequenceEqualOperator = /** @class */ (function () { function SequenceEqualOperator(compareTo, comparator) { this.compareTo = compareTo; this.comparator = comparator; } SequenceEqualOperator.prototype.call = function (subscriber, source) { return source.subscribe(new SequenceEqualSubscriber(subscriber, this.compareTo, this.comparator)); }; return SequenceEqualOperator; }()); exports.SequenceEqualOperator = SequenceEqualOperator; /** * We need this JSDoc comment for affecting ESDoc. * @ignore * @extends {Ignored} */ var SequenceEqualSubscriber = /** @class */ (function (_super) { __extends(SequenceEqualSubscriber, _super); function SequenceEqualSubscriber(destination, compareTo, comparator) { var _this = _super.call(this, destination) || this; _this.compareTo = compareTo; _this.comparator = comparator; _this._a = []; _this._b = []; _this._oneComplete = false; _this.destination.add(compareTo.subscribe(new SequenceEqualCompareToSubscriber(destination, _this))); return _this; } SequenceEqualSubscriber.prototype._next = function (value) { if (this._oneComplete && this._b.length === 0) { this.emit(false); } else { this._a.push(value); this.checkValues(); } }; SequenceEqualSubscriber.prototype._complete = function () { if (this._oneComplete) { this.emit(this._a.length === 0 && this._b.length === 0); } else { this._oneComplete = true; } this.unsubscribe(); }; SequenceEqualSubscriber.prototype.checkValues = function () { var _c = this, _a = _c._a, _b = _c._b, comparator = _c.comparator; while (_a.length > 0 && _b.length > 0) { var a = _a.shift(); var b = _b.shift(); var areEqual = false; try { areEqual = comparator ? comparator(a, b) : a === b; } catch (e) { this.destination.error(e); } if (!areEqual) { this.emit(false); } } }; SequenceEqualSubscriber.prototype.emit = function (value) { var destination = this.destination; destination.next(value); destination.complete(); }; SequenceEqualSubscriber.prototype.nextB = function (value) { if (this._oneComplete && this._a.length === 0) { this.emit(false); } else { this._b.push(value); this.checkValues(); } }; SequenceEqualSubscriber.prototype.completeB = function () { if (this._oneComplete) { this.emit(this._a.length === 0 && this._b.length === 0); } else { this._oneComplete = true; } }; return SequenceEqualSubscriber; }(Subscriber_1.Subscriber)); exports.SequenceEqualSubscriber = SequenceEqualSubscriber; var SequenceEqualCompareToSubscriber = /** @class */ (function (_super) { __extends(SequenceEqualCompareToSubscriber, _super); function SequenceEqualCompareToSubscriber(destination, parent) { var _this = _super.call(this, destination) || this; _this.parent = parent; return _this; } SequenceEqualCompareToSubscriber.prototype._next = function (value) { this.parent.nextB(value); }; SequenceEqualCompareToSubscriber.prototype._error = function (err) { this.parent.error(err); this.unsubscribe(); }; SequenceEqualCompareToSubscriber.prototype._complete = function () { this.parent.completeB(); this.unsubscribe(); }; return SequenceEqualCompareToSubscriber; }(Subscriber_1.Subscriber)); //# sourceMappingURL=sequenceEqual.js.map