Repository navigation
Suport for WeakRef / FinalizationGroup #32393
Copy link
Copy link
Closed
Labels
Domain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptThe issue relates to the different libraries shipped with TypeScriptExperience EnhancementNoncontroversial enhancementsNoncontroversial enhancementsHelp WantedYou can do thisYou can do thisSuggestionAn idea for TypeScriptAn idea for TypeScript
Milestone
Description
Search Terms
stage 3, tc39, weakref, finalization group
Suggestion
WeakRef and FinalizationGroup are Stage 3 https://github.com/tc39/proposal-weakrefs
Only drawback I could think of is that, implementing a polyfill would make it over engineered and less performant than the real thing (of course), and actually leak memory if not careful.
Use Cases
Key/value cache are a great example (as stated in the proposal itself)
Examples
// Fixed version that doesn't leak memory.
function makeWeakCached(f) {
const cache = new Map();
const cleanup = new FinalizationGroup(iterator => {
for (const key of iterator) {
// See note below on concurrency considerations.
const ref = cache.get(key);
if (ref && !ref.deref()) cache.delete(key);
}
});
return key => {
const ref = cache.get(key);
if (ref) {
const cached = ref.deref();
// See note below on concurrency considerations.
if (cached !== undefined) return cached;
}
const fresh = f(key);
cache.set(key, new WeakRef(fresh));
cleanup.register(fresh, key, key);
return fresh;
};
}
var getImageCached = makeWeakCached(getImage);Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Domain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptThe issue relates to the different libraries shipped with TypeScriptExperience EnhancementNoncontroversial enhancementsNoncontroversial enhancementsHelp WantedYou can do thisYou can do thisSuggestionAn idea for TypeScriptAn idea for TypeScript
Activity
RyanCavanaugh commentedon Jul 31, 2019
Generally we don't provide polyfills, just definitions. Since there's no definition here, the only thing that should happen is a lib update
Add definitions for WeakRef and FinalizationRegistry (#38232)
kitsonk commentedon Oct 21, 2020
This is closed but
/lib/lib.esnext.weakref.d.tsstill doesn't exist... It appears to be insrcbut is still being dropped somewhere in the build. Does an update to the LKG need to occur on master?K4rakara commentedon Jan 5, 2021
Can somebody please look into what Kitson Kelly (@kitsonk) mentioned?
Both
WeakMapandWeakSetare defined, but notWeakRef.AgarwalPragy commentedon Jan 5, 2021
As of Jan 2021, WeakRef is now supported in Chrome, Firefox and Edge.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef
Typescript is still missing WeakRef.
Can we please reopen this issue?
kitsonk commentedon Jan 5, 2021
Pragy Agarwal (@AgarwalPragy) it is in TypeScript 4.1 and later: https://www.typescriptlang.org/play?target=99&ts=4.1.3#code/OoUwhg1gSiBmDcAoIA so the issue doesn't need to be re-opened.