mirror of
https://github.com/shaka-project/shaka-player.git
synced 2026-06-14 15:56:38 +03:00
fd0dc8a5cc
Closes #1518 Change-Id: I865f7a0311516d04ae84532dab873e1aaa31eb24
83 lines
2.0 KiB
JavaScript
83 lines
2.0 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2016 Google Inc.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
goog.provide('shaka.util.Functional');
|
|
|
|
|
|
/**
|
|
* @summary A set of functional utility functions.
|
|
*/
|
|
shaka.util.Functional = class {
|
|
/**
|
|
* Creates a promise chain that calls the given callback for each element in
|
|
* the array in a catch of a promise.
|
|
*
|
|
* e.g.:
|
|
* Promise.reject().catch(callback(array[0])).catch(callback(array[1]));
|
|
*
|
|
* @param {!Array.<ELEM>} array
|
|
* @param {function(ELEM):!Promise.<RESULT>} callback
|
|
* @return {!Promise.<RESULT>}
|
|
* @template ELEM,RESULT
|
|
*/
|
|
static createFallbackPromiseChain(array, callback) {
|
|
return array.reduce((promise, elem) => {
|
|
return promise.catch(() => callback(elem));
|
|
}, Promise.reject());
|
|
}
|
|
|
|
|
|
/**
|
|
* Returns the first array concatenated to the second; used to collapse an
|
|
* array of arrays into a single array.
|
|
*
|
|
* @param {!Array.<T>} all
|
|
* @param {!Array.<T>} part
|
|
* @return {!Array.<T>}
|
|
* @template T
|
|
*/
|
|
static collapseArrays(all, part) {
|
|
return all.concat(part);
|
|
}
|
|
|
|
/**
|
|
* A no-op function that ignores its arguments. This is used to suppress
|
|
* unused variable errors.
|
|
* @param {...*} args
|
|
*/
|
|
static ignored(...args) {}
|
|
|
|
|
|
/**
|
|
* A no-op function. Useful in promise chains.
|
|
*/
|
|
static noop() {}
|
|
|
|
|
|
/**
|
|
* Returns if the given value is not null; useful for filtering out null
|
|
* values.
|
|
*
|
|
* @param {T} value
|
|
* @return {boolean}
|
|
* @template T
|
|
*/
|
|
static isNotNull(value) {
|
|
return value != null;
|
|
}
|
|
};
|