Skip to content

Commit

Permalink
Merge pull request #17060 from webpack/thelarkinn/rename-map-helper
Browse files Browse the repository at this point in the history
refactor(MapHelpers): rename `provide` to `getOrInsert` in MapHelpers and document it better
  • Loading branch information
TheLarkInn committed Apr 25, 2023
2 parents 1a0a5b8 + b9f96bf commit e56df72
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/CodeGenerationResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

"use strict";

const { provide } = require("./util/MapHelpers");
const { getOrInsert } = require("./util/MapHelpers");
const { first } = require("./util/SetHelpers");
const createHash = require("./util/createHash");
const { runtimeToString, RuntimeSpecMap } = require("./util/runtime");
Expand Down Expand Up @@ -147,7 +147,7 @@ Caller might not support runtime-dependent code generation (opt-out via optimiza
* @returns {void}
*/
add(module, runtime, result) {
const map = provide(this.map, module, () => new RuntimeSpecMap());
const map = getOrInsert(this.map, module, () => new RuntimeSpecMap());
map.set(runtime, result);
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Compilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const StatsPrinter = require("./stats/StatsPrinter");
const { equals: arrayEquals } = require("./util/ArrayHelpers");
const AsyncQueue = require("./util/AsyncQueue");
const LazySet = require("./util/LazySet");
const { provide } = require("./util/MapHelpers");
const { getOrInsert } = require("./util/MapHelpers");
const WeakTupleMap = require("./util/WeakTupleMap");
const { cachedCleverMerge } = require("./util/cleverMerge");
const {
Expand Down Expand Up @@ -2618,7 +2618,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
const logByLoadersSummary = (category, getDuration, getParallelism) => {
const map = new Map();
for (const [module, profile] of modulesWithProfiles) {
const list = provide(
const list = getOrInsert(
map,
module.type + "!" + module.identifier().replace(/(!|^)[^!]*$/, ""),
() => []
Expand Down
24 changes: 19 additions & 5 deletions lib/util/MapHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,30 @@
"use strict";

/**
* getOrInsert is a helper function for maps that allows you to get a value
* from a map if it exists, or insert a new value if it doesn't. If it value doesn't
* exist, it will be computed by the provided function.
*
* @template K
* @template V
* @param {Map<K, V>} map a map
* @param {K} key the key
* @param {function(): V} computer compute value
* @returns {V} value
* @param {Map<K, V>} map The map object to check
* @param {K} key The key to check
* @param {function(): V} computer function which will compute the value if it doesn't exist
* @returns {V} The value from the map, or the computed value
*
* @example
* ```js
* const map = new Map();
* const value = getOrInsert(map, "key", () => "value");
* console.log(value); // "value"
* ```
*/
exports.provide = (map, key, computer) => {
exports.getOrInsert = (map, key, computer) => {
// Grab key from map
const value = map.get(key);
// If the value already exists, return it
if (value !== undefined) return value;
// Otherwise compute the value, set it in the map, and return it
const newValue = computer();
map.set(key, newValue);
return newValue;
Expand Down

0 comments on commit e56df72

Please sign in to comment.