Last active
March 2, 2023 16:17
-
-
Save nolanlawson/79a3d36e8e6cc25c5048bb17c1795aea to your computer and use it in GitHub Desktop.
Test FinalizationRegistry
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
<!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