| 1 |
import { OperatorFunction, ObservableInput } from '../types'; |
| 2 |
import { operate } from '../util/lift'; |
| 3 |
import { createOperatorSubscriber } from './OperatorSubscriber'; |
| 4 |
import { innerFrom } from '../observable/innerFrom'; |
| 5 |
|
| 6 |
/** |
| 7 |
* Compares all values of two observables in sequence using an optional comparator function |
| 8 |
* and returns an observable of a single boolean value representing whether or not the two sequences |
| 9 |
* are equal. |
| 10 |
* |
| 11 |
* <span class="informal">Checks to see of all values emitted by both observables are equal, in order.</span> |
| 12 |
* |
| 13 |
*  |
| 14 |
* |
| 15 |
* `sequenceEqual` subscribes to source observable and `compareTo` `ObservableInput` (that internally |
| 16 |
* gets converted to an observable) and buffers incoming values from each observable. Whenever either |
| 17 |
* observable emits a value, the value is buffered and the buffers are shifted and compared from the bottom |
| 18 |
* up; If any value pair doesn't match, the returned observable will emit `false` and complete. If one of the |
| 19 |
* observables completes, the operator will wait for the other observable to complete; If the other |
| 20 |
* observable emits before completing, the returned observable will emit `false` and complete. If one observable never |
| 21 |
* completes or emits after the other completes, the returned observable will never complete. |
| 22 |
* |
| 23 |
* ## Example |
| 24 |
* |
| 25 |
* Figure out if the Konami code matches |
| 26 |
* |
| 27 |
* ```ts |
| 28 |
* import { from, fromEvent, map, bufferCount, mergeMap, sequenceEqual } from 'rxjs'; |
| 29 |
* |
| 30 |
* const codes = from([ |
| 31 |
* 'ArrowUp', |
| 32 |
* 'ArrowUp', |
| 33 |
* 'ArrowDown', |
| 34 |
* 'ArrowDown', |
| 35 |
* 'ArrowLeft', |
| 36 |
* 'ArrowRight', |
| 37 |
* 'ArrowLeft', |
| 38 |
* 'ArrowRight', |
| 39 |
* 'KeyB', |
| 40 |
* 'KeyA', |
| 41 |
* 'Enter', // no start key, clearly. |
| 42 |
* ]); |
| 43 |
* |
| 44 |
* const keys = fromEvent<KeyboardEvent>(document, 'keyup').pipe(map(e => e.code)); |
| 45 |
* const matches = keys.pipe( |
| 46 |
* bufferCount(11, 1), |
| 47 |
* mergeMap(last11 => from(last11).pipe(sequenceEqual(codes))) |
| 48 |
* ); |
| 49 |
* matches.subscribe(matched => console.log('Successful cheat at Contra? ', matched)); |
| 50 |
* ``` |
| 51 |
* |
| 52 |
* @see {@link combineLatest} |
| 53 |
* @see {@link zip} |
| 54 |
* @see {@link withLatestFrom} |
| 55 |
* |
| 56 |
* @param compareTo The `ObservableInput` sequence to compare the source sequence to. |
| 57 |
* @param comparator An optional function to compare each value pair. |
| 58 |
* |
| 59 |
* @return A function that returns an Observable that emits a single boolean |
| 60 |
* value representing whether or not the values emitted by the source |
| 61 |
* Observable and provided `ObservableInput` were equal in sequence. |
| 62 |
*/ |
| 63 |
export function sequenceEqual<T>( |
| 64 |
compareTo: ObservableInput<T>, |
| 65 |
comparator: (a: T, b: T) => boolean = (a, b) => a === b |
| 66 |
): OperatorFunction<T, boolean> { |
| 67 |
return operate((source, subscriber) => { |
| 68 |
// The state for the source observable |
| 69 |
const aState = createState<T>(); |
| 70 |
// The state for the compareTo observable; |
| 71 |
const bState = createState<T>(); |
| 72 |
|
| 73 |
/** A utility to emit and complete */ |
| 74 |
const emit = (isEqual: boolean) => { |
| 75 |
subscriber.next(isEqual); |
| 76 |
subscriber.complete(); |
| 77 |
}; |
| 78 |
|
| 79 |
/** |
| 80 |
* Creates a subscriber that subscribes to one of the sources, and compares its collected |
| 81 |
* state -- `selfState` -- to the other source's collected state -- `otherState`. This |
| 82 |
* is used for both streams. |
| 83 |
*/ |
| 84 |
const createSubscriber = (selfState: SequenceState<T>, otherState: SequenceState<T>) => { |
| 85 |
const sequenceEqualSubscriber = createOperatorSubscriber( |
| 86 |
subscriber, |
| 87 |
(a: T) => { |
| 88 |
const { buffer, complete } = otherState; |
| 89 |
if (buffer.length === 0) { |
| 90 |
// If there's no values in the other buffer |
| 91 |
// and the other stream is complete, we know |
| 92 |
// this isn't a match, because we got one more value. |
| 93 |
// Otherwise, we push onto our buffer, so when the other |
| 94 |
// stream emits, it can pull this value off our buffer and check it |
| 95 |
// at the appropriate time. |
| 96 |
complete ? emit(false) : selfState.buffer.push(a); |
| 97 |
} else { |
| 98 |
// If the other stream *does* have values in its buffer, |
| 99 |
// pull the oldest one off so we can compare it to what we |
| 100 |
// just got. If it wasn't a match, emit `false` and complete. |
| 101 |
!comparator(a, buffer.shift()!) && emit(false); |
| 102 |
} |
| 103 |
}, |
| 104 |
() => { |
| 105 |
// Or observable completed |
| 106 |
selfState.complete = true; |
| 107 |
const { complete, buffer } = otherState; |
| 108 |
// If the other observable is also complete, and there's |
| 109 |
// still stuff left in their buffer, it doesn't match, if their |
| 110 |
// buffer is empty, then it does match. This is because we can't |
| 111 |
// possibly get more values here anymore. |
| 112 |
complete && emit(buffer.length === 0); |
| 113 |
// Be sure to clean up our stream as soon as possible if we can. |
| 114 |
sequenceEqualSubscriber?.unsubscribe(); |
| 115 |
} |
| 116 |
); |
| 117 |
|
| 118 |
return sequenceEqualSubscriber; |
| 119 |
}; |
| 120 |
|
| 121 |
// Subscribe to each source. |
| 122 |
source.subscribe(createSubscriber(aState, bState)); |
| 123 |
innerFrom(compareTo).subscribe(createSubscriber(bState, aState)); |
| 124 |
}); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* A simple structure for the data used to test each sequence |
| 129 |
*/ |
| 130 |
interface SequenceState<T> { |
| 131 |
/** A temporary store for arrived values before they are checked */ |
| 132 |
buffer: T[]; |
| 133 |
/** Whether or not the sequence source has completed. */ |
| 134 |
complete: boolean; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Creates a simple structure that is used to represent |
| 139 |
* data used to test each sequence. |
| 140 |
*/ |
| 141 |
function createState<T>(): SequenceState<T> { |
| 142 |
return { |
| 143 |
buffer: [], |
| 144 |
complete: false, |
| 145 |
}; |
| 146 |
} |