Skip to content

Instantly share code, notes, and snippets.

@nolanlawson
Last active March 2, 2023 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nolanlawson/79a3d36e8e6cc25c5048bb17c1795aea to your computer and use it in GitHub Desktop.
Save nolanlawson/79a3d36e8e6cc25c5048bb17c1795aea to your computer and use it in GitHub Desktop.
Test FinalizationRegistry
<!doctype html>
<html>
<button id="derefKey">Deref key</button>
<button id="derefValue">Deref value</button>
<script type=module>
const reg = new FinalizationRegistry(held => {
console.log('gced', held)
})
const map = new WeakMap()
window.reg = reg
window.map = map
class MyKey {
}
class MyValue {
}
window.key = new MyKey()
window.value = new MyValue()
let array = [new WeakRef(value)]
map.set(key, array)
reg.register(value, array)
reg.register(key, "the key")
derefKey.addEventListener('click', () => {
window.key = undefined
})
derefValue.addEventListener('click', () => {
window.value = undefined
})
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment