Library for composing asynchronous and event-based operations in JavaScript using Observable sequences and fluent query operators
—
Operators for filtering observable sequences including filter, take, skip, distinct, and debounce operations.
Filters elements based on a predicate function.
/**
* Filters elements based on a predicate function
* @param {function} predicate - Function to test each element
* @param {*} [thisArg] - Object to use as this when executing predicate
* @returns {Observable} Observable sequence containing filtered elements
*/
observable.filter = function(predicate, thisArg);
observable.where = function(predicate, thisArg); // Alias for filterUsage Example:
var source = Rx.Observable.range(1, 10);
var evens = source.filter(function(x) { return x % 2 === 0; });
// Emits: 2, 4, 6, 8, 10Takes the first specified number of elements.
/**
* Takes the first count elements from an observable sequence
* @param {number} count - Number of elements to take
* @param {Scheduler} [scheduler] - Scheduler to use for the subscription
* @returns {Observable} Observable sequence containing the first count elements
*/
observable.take = function(count, scheduler);Takes elements while a predicate is true.
/**
* Takes elements from start of sequence while predicate is true
* @param {function} predicate - Function to test each element
* @param {*} [thisArg] - Object to use as this when executing predicate
* @returns {Observable} Observable sequence containing elements while predicate is true
*/
observable.takeWhile = function(predicate, thisArg);Takes the last specified number of elements.
/**
* Takes the last count elements from an observable sequence
* @param {number} count - Number of elements to take from the end
* @returns {Observable} Observable sequence containing the last count elements
*/
observable.takeLast = function(count);Takes the last elements and returns them as an array.
/**
* Takes the last count elements and returns them as a single array
* @param {number} count - Number of elements to take from the end
* @returns {Observable} Observable sequence containing array of last count elements
*/
observable.takeLastBuffer = function(count);Takes elements until another observable emits.
/**
* Takes elements until the other observable sequence emits a value
* @param {Observable} other - Observable sequence that terminates propagation
* @returns {Observable} Observable sequence taking elements until other emits
*/
observable.takeUntil = function(other);Usage Example:
var source = Rx.Observable.interval(100);
var stopSignal = Rx.Observable.timer(500);
var result = source.takeUntil(stopSignal);
// Emits: 0, 1, 2, 3, 4 (stops after 500ms)Skips the first specified number of elements.
/**
* Skips the first count elements from an observable sequence
* @param {number} count - Number of elements to skip
* @returns {Observable} Observable sequence skipping the first count elements
*/
observable.skip = function(count);Skips elements while a predicate is true.
/**
* Skips elements from start of sequence while predicate is true
* @param {function} predicate - Function to test each element
* @param {*} [thisArg] - Object to use as this when executing predicate
* @returns {Observable} Observable sequence skipping elements while predicate is true
*/
observable.skipWhile = function(predicate, thisArg);Skips the last specified number of elements.
/**
* Skips the last count elements from an observable sequence
* @param {number} count - Number of elements to skip from the end
* @returns {Observable} Observable sequence skipping the last count elements
*/
observable.skipLast = function(count);Skips elements until another observable emits.
/**
* Skips elements until the other observable sequence emits a value
* @param {Observable} other - Observable sequence that starts propagation
* @returns {Observable} Observable sequence skipping elements until other emits
*/
observable.skipUntil = function(other);Filters out duplicate values from the sequence.
/**
* Returns distinct elements from an observable sequence
* @param {function} [keySelector] - Function to compute comparison key for each element
* @param {function} [comparer] - Function to compare keys for equality
* @returns {Observable} Observable sequence containing distinct elements
*/
observable.distinct = function(keySelector, comparer);Usage Example:
var source = Rx.Observable.fromArray([1, 2, 2, 3, 1, 4]);
var distinct = source.distinct();
// Emits: 1, 2, 3, 4Filters out consecutive duplicate values.
/**
* Returns distinct contiguous elements from an observable sequence
* @param {function} [keySelector] - Function to compute comparison key for each element
* @param {function} [comparer] - Function to compare keys for equality
* @returns {Observable} Observable sequence with consecutive duplicates removed
*/
observable.distinctUntilChanged = function(keySelector, comparer);Usage Example:
var source = Rx.Observable.fromArray([1, 1, 2, 2, 2, 3, 3, 1]);
var distinct = source.distinctUntilChanged();
// Emits: 1, 2, 3, 1Ignores all elements and only propagates completion or error.
/**
* Ignores all elements in an observable sequence leaving only termination messages
* @returns {Observable} Observable sequence with all elements ignored
*/
observable.ignoreElements = function();Provides a default value if the sequence is empty.
/**
* Returns elements from sequence, or default value if sequence is empty
* @param {*} [defaultValue] - Default value to return if sequence is empty
* @returns {Observable} Observable sequence with default value if empty
*/
observable.defaultIfEmpty = function(defaultValue);Usage Example:
var empty = Rx.Observable.empty();
var withDefault = empty.defaultIfEmpty('No data');
// Emits: 'No data'Returns the element at a specified index.
/**
* Returns the element at the specified index in the sequence
* @param {number} index - Zero-based index of element to retrieve
* @returns {Observable} Observable sequence containing element at specified index
*/
observable.elementAt = function(index);Returns the element at a specified index or a default value.
/**
* Returns element at specified index or default value if index is out of range
* @param {number} index - Zero-based index of element to retrieve
* @param {*} [defaultValue] - Default value if index is out of range
* @returns {Observable} Observable sequence containing element or default value
*/
observable.elementAtOrDefault = function(index, defaultValue);Returns the only element of a sequence or throws an error.
/**
* Returns the only element of a sequence, or throws error if not exactly one element
* @param {function} [predicate] - Function to test each element
* @param {*} [thisArg] - Object to use as this when executing predicate
* @returns {Observable} Observable sequence containing the single element
*/
observable.single = function(predicate, thisArg);Returns the only element of a sequence or a default value.
/**
* Returns the only element of a sequence or default value
* @param {function} [predicate] - Function to test each element
* @param {*} [defaultValue] - Default value if no element or more than one element
* @param {*} [thisArg] - Object to use as this when executing predicate
* @returns {Observable} Observable sequence containing the single element or default
*/
observable.singleOrDefault = function(predicate, defaultValue, thisArg);Searches for an element that matches the conditions and returns the first occurrence.
/**
* Searches for an element that matches the conditions and returns the first occurrence
* @param {function} predicate - Function to test each element
* @param {*} [thisArg] - Object to use as this when executing predicate
* @returns {Observable} Observable sequence containing the first matching element or undefined
*/
observable.find = function(predicate, thisArg);Searches for an element that matches the conditions and returns the index.
/**
* Searches for an element that matches the conditions and returns the zero-based index
* @param {function} predicate - Function to test each element
* @param {*} [thisArg] - Object to use as this when executing predicate
* @returns {Observable} Observable sequence containing the index or -1 if not found
*/
observable.findIndex = function(predicate, thisArg);Returns the index of the first occurrence of a value.
/**
* Returns the index of the first occurrence of a value in the sequence
* @param {*} searchElement - Element to locate
* @param {number} [fromIndex] - Index to start the search at
* @returns {Observable} Observable sequence containing the index or -1 if not found
*/
observable.indexOf = function(searchElement, fromIndex);Returns the index of the last occurrence of a value.
/**
* Returns the index of the last occurrence of a value in the sequence
* @param {*} searchElement - Element to locate
* @param {number} [fromIndex] - Index to start the search at
* @returns {Observable} Observable sequence containing the index or -1 if not found
*/
observable.lastIndexOf = function(searchElement, fromIndex);Determines whether the sequence includes a certain element.
/**
* Determines whether the sequence includes a certain element
* @param {*} searchElement - Element to search for
* @param {number} [fromIndex] - Index to start the search at
* @returns {Observable} Observable sequence containing boolean result
*/
observable.includes = function(searchElement, fromIndex);Returns elements from start index to end index.
/**
* Returns elements from start index to end index
* @param {number} start - Zero-based index at which to begin extraction
* @param {number} [end] - Zero-based index at which to end extraction
* @returns {Observable} Observable sequence containing sliced elements
*/
observable.slice = function(start, end);Returns a specified number of contiguous elements from the end.
/**
* Returns a specified number of contiguous elements from the end of sequence
* @param {number} count - Number of elements to take from end
* @returns {Observable} Observable sequence containing the last elements
*/
observable.takeLast = function(count);Bypasses a specified number of elements at the end.
/**
* Bypasses a specified number of elements at the end of sequence
* @param {number} count - Number of elements to skip from end
* @returns {Observable} Observable sequence bypassing the last elements
*/
observable.skipLast = function(count);Returns the last elements as a single array emission.
/**
* Returns the last elements as a single array emission
* @param {number} count - Number of elements to take from end
* @returns {Observable} Observable sequence containing array of last elements
*/
observable.takeLastBuffer = function(count);Returns elements within a specified time window from the end.
/**
* Returns elements within a specified time window from the end
* @param {number} duration - Maximum time interval between values
* @param {Scheduler} [scheduler] - Scheduler to run timers on
* @returns {Observable} Observable sequence containing time-windowed elements
*/
observable.takeLastWithTime = function(duration, scheduler);Skips elements within a specified time window from the end.
/**
* Skips elements within a specified time window from the end
* @param {number} duration - Maximum time interval between values
* @param {Scheduler} [scheduler] - Scheduler to run timers on
* @returns {Observable} Observable sequence skipping time-windowed elements
*/
observable.skipLastWithTime = function(duration, scheduler);Takes elements for the specified duration.
/**
* Takes elements for the specified duration from the start of sequence
* @param {number} duration - Duration to take elements for
* @param {Scheduler} [scheduler] - Scheduler to run timers on
* @returns {Observable} Observable sequence containing elements within time window
*/
observable.takeWithTime = function(duration, scheduler);Skips elements for the specified duration.
/**
* Skips elements for the specified duration from the start of sequence
* @param {number} duration - Duration to skip elements for
* @param {Scheduler} [scheduler] - Scheduler to run timers on
* @returns {Observable} Observable sequence skipping elements within time window
*/
observable.skipWithTime = function(duration, scheduler);