Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suport for WeakRef / FinalizationGroup #32393

Closed
3 of 5 tasks
pocesar opened this issue Jul 14, 2019 · 5 comments · Fixed by #38232
Closed
3 of 5 tasks

Suport for WeakRef / FinalizationGroup #32393

pocesar opened this issue Jul 14, 2019 · 5 comments · Fixed by #38232
Labels
Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Experience Enhancement Noncontroversial enhancements Help Wanted You can do this Suggestion An idea for TypeScript
Milestone

Comments

@pocesar
Copy link

pocesar commented Jul 14, 2019

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.
@RyanCavanaugh
Copy link
Member

Generally we don't provide polyfills, just definitions. Since there's no definition here, the only thing that should happen is a lib update

@RyanCavanaugh RyanCavanaugh added Committed The team has roadmapped this issue Suggestion An idea for TypeScript labels Jul 31, 2019
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.7.0 milestone Jul 31, 2019
@RyanCavanaugh RyanCavanaugh added Experience Enhancement Noncontroversial enhancements Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this and removed Committed The team has roadmapped this issue labels Jan 24, 2020
sandersn added a commit that referenced this issue Oct 6, 2020
* Add definitions for WeakRef and FinalizationRegistry

Fixes #32393

* Mark callback parameter in FinalizationRegistry#cleanupSome() as optional

* Make FinalizationRegistry.prototype.cleanupSome optional

* Remove FinalizationRegistry.prototype.cleanupSome()

Co-authored-by: Nathan Shively-Sanders <[email protected]>
@kitsonk
Copy link
Contributor

kitsonk commented Oct 21, 2020

This is closed but /lib/lib.esnext.weakref.d.ts still doesn't exist... It appears to be in src but is still being dropped somewhere in the build. Does an update to the LKG need to occur on master?

@K4rakara
Copy link

K4rakara commented Jan 5, 2021

Can somebody please look into what @kitsonk mentioned?

Both WeakMap and WeakSet are defined, but not WeakRef.

@AgarwalPragy
Copy link

AgarwalPragy commented 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
Copy link
Contributor

kitsonk commented Jan 5, 2021

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Experience Enhancement Noncontroversial enhancements Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants