Skip to content

Instantly share code, notes, and snippets.

@slikts
Last active July 11, 2018 10:47
Show Gist options
  • Save slikts/cf0719c6b5d5d2809bcedd201105a604 to your computer and use it in GitHub Desktop.
Save slikts/cf0719c6b5d5d2809bcedd201105a604 to your computer and use it in GitHub Desktop.
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