| 69 | * @returns {LazyHashedEtag} etag |
| 70 | */ |
| 71 | const getter = (obj, hashFunction = DEFAULTS.HASH_FUNCTION) => { |
| 72 | /** @type {undefined | InnerCache} */ |
| 73 | let innerMap; |
| 74 | if (typeof hashFunction === "string") { |
| 75 | innerMap = mapStrings.get(hashFunction); |
| 76 | if (innerMap === undefined) { |
| 77 | const newHash = new LazyHashedEtag(obj, hashFunction); |
| 78 | /** @type {InnerCache} */ |
| 79 | innerMap = new WeakMap(); |
| 80 | innerMap.set(obj, newHash); |
| 81 | mapStrings.set(hashFunction, innerMap); |
| 82 | return newHash; |
| 83 | } |
| 84 | } else { |
| 85 | innerMap = mapObjects.get(hashFunction); |
| 86 | if (innerMap === undefined) { |
| 87 | const newHash = new LazyHashedEtag(obj, hashFunction); |
| 88 | /** @type {InnerCache} */ |
| 89 | innerMap = new WeakMap(); |
| 90 | innerMap.set(obj, newHash); |
| 91 | mapObjects.set(hashFunction, innerMap); |
| 92 | return newHash; |
| 93 | } |
| 94 | } |
| 95 | const hash = innerMap.get(obj); |
| 96 | if (hash !== undefined) return hash; |
| 97 | const newHash = new LazyHashedEtag(obj, hashFunction); |
| 98 | innerMap.set(obj, newHash); |
| 99 | return newHash; |
| 100 | }; |
| 101 | |
| 102 | module.exports = getter; |