If I have two WeakMaps in javascript:
/**
* @type {WeakMap.<Function, Provider>}
*/
const providerFtn = new WeakMap();
/**
* @type {WeakMap.<Provider, Function>}
*/
const provider = new WeakMap();
and I place a Provider and its function in the WeakMaps...
function registerThingProvider(title, params) {
const myProvider = new Provider(title);
function factory() { new Thing(params); }
providerFtn.set(factory, myProvider);
provider.set(myProvider, factory);
return myProvider;
}
let stronglyReachableProvider = registerThingProvider('fooBar', [ 3.14, null ]);
at this point, I would hope that both the provider and its function are strongly reachable. My assumption is that a strongly reachable key makes both itself and its value unavailable for garbage collection. The references in providerFtn
and provider
should be safe.
But, if I do a bunch of important work with my provider and then clear my one and only strong stronglyReachableProvider
reference ...
await importantWork(stronglyReachableProvider);
stronglyReachableProvider = null;
Assume there are no other strong references to the objects outside of the WeakMaps. Are myProvider
and factory
now eligible for garbage collection, or not? It would be great if the value references in the WeakMaps were considered weakly reachable in this case, but I suspect WeakMap values would necessarily need to be implemented as strong references.