mirror of
https://github.com/shaka-project/shaka-player.git
synced 2026-06-14 15:56:38 +03:00
05b09728c7
Added polyfills for `Map.getOrInsert()` and `Map.getOrInsertComputed()` from the TC39 upsert proposal and refactor the codebase to use them. These methods replace the common "check if key exists, then set default" pattern with a single atomic operation. This improves code readability and eliminates redundant map lookups throughout the player. --------- Co-authored-by: Álvaro Velad Galván <ladvan91@hotmail.com>
96 lines
2.9 KiB
JavaScript
96 lines
2.9 KiB
JavaScript
/*! @license
|
|
* Shaka Player
|
|
* Copyright 2016 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
goog.provide('shaka.polyfill.Map');
|
|
|
|
goog.require('shaka.log');
|
|
goog.require('shaka.polyfill');
|
|
|
|
/**
|
|
* @summary A polyfill to provide Map.prototype.getOrInsert,
|
|
* Map.prototype.getOrInsertComputed, WeakMap.prototype.getOrInsert,
|
|
* and WeakMap.prototype.getOrInsertComputed methods.
|
|
* @see https://github.com/tc39/proposal-upsert
|
|
* @export
|
|
*/
|
|
shaka.polyfill.Map = class {
|
|
/**
|
|
* Install the polyfill if needed.
|
|
* @export
|
|
*/
|
|
static install() {
|
|
shaka.log.debug('Map.install');
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
if (!('getOrInsert' in Map.prototype)) {
|
|
shaka.log.debug('Map: Installing getOrInsert polyfill.');
|
|
// eslint-disable-next-line no-extend-native, no-restricted-syntax
|
|
Map.prototype.getOrInsert = shaka.polyfill.Map.mapGetOrInsert_;
|
|
}
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
if (!('getOrInsertComputed' in Map.prototype)) {
|
|
shaka.log.debug('Map: Installing getOrInsertComputed polyfill.');
|
|
// eslint-disable-next-line no-extend-native, no-restricted-syntax
|
|
Map.prototype.getOrInsertComputed =
|
|
shaka.polyfill.Map.mapGetOrInsertComputed_;
|
|
}
|
|
|
|
shaka.log.debug('WeakMap.install');
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
if (!('getOrInsert' in WeakMap.prototype)) {
|
|
shaka.log.debug('WeakMap: Installing getOrInsert polyfill.');
|
|
// eslint-disable-next-line no-extend-native, no-restricted-syntax
|
|
WeakMap.prototype.getOrInsert = shaka.polyfill.Map.mapGetOrInsert_;
|
|
}
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
if (!('getOrInsertComputed' in WeakMap.prototype)) {
|
|
shaka.log.debug('WeakMap: Installing getOrInsertComputed polyfill.');
|
|
// eslint-disable-next-line no-extend-native, no-restricted-syntax
|
|
WeakMap.prototype.getOrInsertComputed =
|
|
shaka.polyfill.Map.mapGetOrInsertComputed_;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns the value for the given key if present; otherwise inserts
|
|
* the default value, and returns that.
|
|
* @param {K} key
|
|
* @param {V} defaultValue
|
|
* @return {V}
|
|
* @this {Map<K, V>|WeakMap<K, V>}
|
|
* @template K, V
|
|
* @private
|
|
*/
|
|
static mapGetOrInsert_(key, defaultValue) {
|
|
if (!this.has(key)) {
|
|
this.set(key, defaultValue);
|
|
}
|
|
return this.get(key);
|
|
}
|
|
|
|
/**
|
|
* Returns the value for the given key if present; otherwise calls
|
|
* the callback with the key, inserts the returned value, and returns that.
|
|
* @param {K} key
|
|
* @param {function(K): V} callbackFunction
|
|
* @return {V}
|
|
* @this {Map<K, V>|WeakMap<K, V>}
|
|
* @template K, V
|
|
* @private
|
|
*/
|
|
static mapGetOrInsertComputed_(key, callbackFunction) {
|
|
if (!this.has(key)) {
|
|
this.set(key, callbackFunction(key));
|
|
}
|
|
return this.get(key);
|
|
}
|
|
};
|
|
|
|
shaka.polyfill.register(shaka.polyfill.Map.install);
|