greg 4d1d2f8c52
Some checks failed
continuous-integration/drone Build is failing
maj
2023-06-11 20:17:11 +02:00

41 lines
1.1 KiB
JavaScript

// this provides a weak-valued map to ensure we only have a single instance of an object per id, but can still be GC-ed
let WeakValueMap
try {
let NativeWeakValueMap = require('weakvaluemap')
let allInstances = []
WeakValueMap = function() {
let map = new NativeWeakValueMap()
allInstances.push(map)
return map
}
WeakValueMap.getStatus = function() {
let mapStats = []
for (let map of allInstances) {
let size = 0
let count = 0
for (let key of map.keys()) {
let value = map.get(key)
size += value && value.approximateSize || 100
count++
}
if (count > 0) {
mapStats.push({
name: map.name,
size,
count
})
}
}
return mapStats
}
} catch (error) {
console.warn('No weak value map available, this can be used for development, but weak value maps should be enabled for production use', error.toString())
WeakValueMap = Map
WeakValueMap.getStatus = function() {
return 'WeakValueMap failed to load'
}
WeakValueMap.prototype._keysAsArray = function() {
return Array.from(this.keys())
}
}
exports.WeakValueMap = WeakValueMap