Last active
July 11, 2018 10:47
-
-
Save slikts/cf0719c6b5d5d2809bcedd201105a604 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PairWeakMap<A extends object, B extends object, C> { | |
map: WeakMap<A, WeakMap<B, C>> | |
constructor() { | |
this.map = new WeakMap() | |
} | |
set(a: A, b: B, c: C): this { | |
const { map } = this | |
let submap = map.get(a) | |
if (!submap) { | |
submap = new WeakMap() | |
map.set(a, submap) | |
} | |
submap.set(b, c) | |
return this | |
} | |
get(a: A, b: B): C | undefined { | |
const submap = this.map.get(a) | |
if (!submap) { | |
return | |
} | |
return submap.get(b) | |
} | |
} | |
const a = {} | |
const b = {} | |
console.log(new PairWeakMap().set(a, b, 123).get(a, b)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment