webpack/lib/util/SortableSet.js

105 lines
2.2 KiB
JavaScript
Raw Normal View History

"use strict";
2018-01-11 03:32:39 +08:00
class SortableSet extends Set {
constructor(initialIterable, defaultSort) {
super(initialIterable);
this._sortFn = defaultSort;
this._lastActiveSortFn = null;
2018-01-11 03:32:39 +08:00
this._cache = undefined;
this._cacheOrderIndependent = undefined;
}
/**
* @param {any} value - value to add to set
2018-03-29 12:47:37 +08:00
* @returns {this} - returns itself
*/
add(value) {
this._lastActiveSortFn = null;
2018-01-11 03:32:39 +08:00
this._invalidateCache();
this._invalidateOrderedCache();
super.add(value);
return this;
}
2017-09-22 20:07:28 +08:00
delete(value) {
2018-01-11 03:32:39 +08:00
this._invalidateCache();
this._invalidateOrderedCache();
2017-09-22 20:07:28 +08:00
return super.delete(value);
}
clear() {
2018-01-11 03:32:39 +08:00
this._invalidateCache();
this._invalidateOrderedCache();
2017-09-22 20:07:28 +08:00
return super.clear();
}
2018-04-06 13:44:58 +08:00
sortWith(/** @type {(a: any, b: any) => number} */ sortFn) {
2018-02-25 09:00:20 +08:00
if (this.size === 0 || sortFn === this._lastActiveSortFn) {
// already sorted - nothing to do
return;
}
const sortedArray = Array.from(this).sort(sortFn);
super.clear();
2018-02-25 09:00:20 +08:00
for (let i = 0; i < sortedArray.length; i += 1) {
2017-09-22 23:23:49 +08:00
super.add(sortedArray[i]);
}
this._lastActiveSortFn = sortFn;
2018-01-11 03:32:39 +08:00
this._invalidateCache();
}
/**
* @returns {void}
*/
sort() {
this.sortWith(this._sortFn);
}
2017-09-22 20:07:28 +08:00
/**
* @param {Function} fn - function to calculate value
* @returns {any} - returns result of fn(this), cached until set changes
2017-09-22 20:07:28 +08:00
*/
2017-11-06 19:15:23 +08:00
getFromCache(fn) {
2018-02-25 09:00:20 +08:00
if (this._cache === undefined) {
this._cache = new Map();
} else {
const data = this._cache.get(fn);
2018-02-25 09:00:20 +08:00
if (data !== undefined) {
return data;
}
2017-09-22 20:07:28 +08:00
}
const newData = fn(this);
this._cache.set(fn, newData);
return newData;
}
2018-01-11 03:32:39 +08:00
/**
* @param {Function} fn - function to calculate value
* @returns {any} - returns result of fn(this), cached until set changes
*/
2017-11-06 19:15:23 +08:00
getFromUnorderedCache(fn) {
2018-02-25 09:00:20 +08:00
if (this._cacheOrderIndependent === undefined) {
this._cacheOrderIndependent = new Map();
} else {
const data = this._cacheOrderIndependent.get(fn);
2018-02-25 09:00:20 +08:00
if (data !== undefined) {
return data;
}
}
const newData = fn(this);
this._cacheOrderIndependent.set(fn, newData);
return newData;
2017-09-22 20:07:28 +08:00
}
2018-01-11 03:32:39 +08:00
_invalidateCache() {
2018-02-25 09:00:20 +08:00
if (this._cache !== undefined) this._cache.clear();
2018-01-11 03:32:39 +08:00
}
_invalidateOrderedCache() {
2018-02-25 09:00:20 +08:00
if (this._cacheOrderIndependent !== undefined)
2018-01-11 03:32:39 +08:00
this._cacheOrderIndependent.clear();
}
}
module.exports = SortableSet;