Closed natevw closed 10 years ago
Hmmm, current spec says:
If an object that is being used as the key of a WeakMap key/value pair is only reachable by following a chain of references that start within that WeakMap, then that key/value pair is inaccessible and is automatically removed from the WeakMap. WeakMap implementations must detect and remove such key/value pairs and any associated resources.
This doesn't sound nearly as promising as a cursory reading of the strawman. c.f. http://www.nczonline.net/blog/2012/11/06/ecmascript-6-collections-part-3-weakmaps/ for potential clarification.
Also note a primitive value can't be used for weakmap key, so further cleverness might be needed there if that's not already implying that this idea won't work.
I've actually got a sinking feeling that what I'm trying to do (detect the garbage collection of an object) is precisely what WeakMap is designed to prevent.
With WeakMaps alone, garbage collection remains as unobservable as before — http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps
It's a bit premature (Chrome still has ESnext stuff behind a flag) but I'm realizing that the coincident of WebRTC and WeakMap support should be pretty likely!
So here's how I think this might work:
serialize — stores object/function in a old-fashioned id:target object mapping. deserialize — creates wrapper object/function and tracks it in
weakmap[id] = wrapper
. when this is GC'ed the remote should unmap the actual target.Periodically, the serializer must send
Object.keys(exposedObjects)
to the deserializer. The deserializer then sends back a list of whichweakmap.has(id)
return false, which the serializer should remove from its strong mapping.[This assumes that values in a weakmap can GC and cause the keypair to go away, which I need to check with the actual draft spec.]