sequenceEqual.js
6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Subscriber_1 = require('../Subscriber');
var tryCatch_1 = require('../util/tryCatch');
var errorObject_1 = require('../util/errorObject');
/**
* Compares all values of two observables in sequence using an optional comparor 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>
*
* <img src="./img/sequenceEqual.png" width="100%">
*
* `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 <caption>figure out if the Konami code matches</caption>
* var code = Rx.Observable.from([
* "ArrowUp",
* "ArrowUp",
* "ArrowDown",
* "ArrowDown",
* "ArrowLeft",
* "ArrowRight",
* "ArrowLeft",
* "ArrowRight",
* "KeyB",
* "KeyA",
* "Enter" // no start key, clearly.
* ]);
*
* var keys = Rx.Observable.fromEvent(document, 'keyup')
* .map(e => e.code);
* var matches = keys.bufferCount(11, 1)
* .mergeMap(
* last11 =>
* Rx.Observable.from(last11)
* .sequenceEqual(code)
* );
* 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} [comparor] 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, comparor) {
return this.lift(new SequenceEqualOperator(compareTo, comparor));
}
exports.sequenceEqual = sequenceEqual;
var SequenceEqualOperator = (function () {
function SequenceEqualOperator(compareTo, comparor) {
this.compareTo = compareTo;
this.comparor = comparor;
}
SequenceEqualOperator.prototype.call = function (subscriber, source) {
return source.subscribe(new SequenceEqualSubscriber(subscriber, this.compareTo, this.comparor));
};
return SequenceEqualOperator;
}());
exports.SequenceEqualOperator = SequenceEqualOperator;
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
var SequenceEqualSubscriber = (function (_super) {
__extends(SequenceEqualSubscriber, _super);
function SequenceEqualSubscriber(destination, compareTo, comparor) {
_super.call(this, destination);
this.compareTo = compareTo;
this.comparor = comparor;
this._a = [];
this._b = [];
this._oneComplete = false;
this.add(compareTo.subscribe(new SequenceEqualCompareToSubscriber(destination, 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;
}
};
SequenceEqualSubscriber.prototype.checkValues = function () {
var _c = this, _a = _c._a, _b = _c._b, comparor = _c.comparor;
while (_a.length > 0 && _b.length > 0) {
var a = _a.shift();
var b = _b.shift();
var areEqual = false;
if (comparor) {
areEqual = tryCatch_1.tryCatch(comparor)(a, b);
if (areEqual === errorObject_1.errorObject) {
this.destination.error(errorObject_1.errorObject.e);
}
}
else {
areEqual = a === b;
}
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();
}
};
return SequenceEqualSubscriber;
}(Subscriber_1.Subscriber));
exports.SequenceEqualSubscriber = SequenceEqualSubscriber;
var SequenceEqualCompareToSubscriber = (function (_super) {
__extends(SequenceEqualCompareToSubscriber, _super);
function SequenceEqualCompareToSubscriber(destination, parent) {
_super.call(this, destination);
this.parent = parent;
}
SequenceEqualCompareToSubscriber.prototype._next = function (value) {
this.parent.nextB(value);
};
SequenceEqualCompareToSubscriber.prototype._error = function (err) {
this.parent.error(err);
};
SequenceEqualCompareToSubscriber.prototype._complete = function () {
this.parent._complete();
};
return SequenceEqualCompareToSubscriber;
}(Subscriber_1.Subscriber));
//# sourceMappingURL=sequenceEqual.js.map