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>
60 lines
1.6 KiB
JavaScript
60 lines
1.6 KiB
JavaScript
/*! @license
|
|
* Shaka Player
|
|
* Copyright 2025 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* @fileoverview Externs for Map getOrInsert/getOrInsertComputed methods
|
|
*
|
|
* @externs
|
|
*/
|
|
|
|
/**
|
|
* 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>}
|
|
* @template K, V
|
|
*/
|
|
// eslint-disable-next-line no-extend-native
|
|
Map.prototype.getOrInsert = function(key, defaultValue) {};
|
|
|
|
/**
|
|
* 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>}
|
|
* @template K, V
|
|
*/
|
|
// eslint-disable-next-line no-extend-native
|
|
Map.prototype.getOrInsertComputed = function(key, callbackFunction) {};
|
|
|
|
/**
|
|
* 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 {WeakMap<K, V>}
|
|
* @template K, V
|
|
*/
|
|
// eslint-disable-next-line no-extend-native
|
|
WeakMap.prototype.getOrInsert = function(key, defaultValue) {};
|
|
|
|
/**
|
|
* 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 {WeakMap<K, V>}
|
|
* @template K, V
|
|
*/
|
|
// eslint-disable-next-line no-extend-native
|
|
WeakMap.prototype.getOrInsertComputed = function(key, callbackFunction) {};
|