Skip to content

Suport for WeakRef / FinalizationGroup #32393

Closed

Description

@pocesar

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.

Activity

  1. RyanCavanaugh commented on Jul 31, 2019

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

  2. added
    Domain: lib.d.tsThe issue relates to the different libraries shipped with TypeScript
    and removed
    CommittedThe team has roadmapped this issue
    on Jan 24, 2020
  3. added a commit that references this issue on Oct 6, 2020
    bd1d8e5
  4. kitsonk commented on Oct 21, 2020

    @kitsonk
    Contributor

    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?

  5. K4rakara commented on Jan 5, 2021

    @K4rakara

    Can somebody please look into what Kitson Kelly (@kitsonk) mentioned?

    Both WeakMap and WeakSet are defined, but not WeakRef.

  6. AgarwalPragy commented on Jan 5, 2021

    @AgarwalPragy

    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?

  7. kitsonk commented on Jan 5, 2021

    @kitsonk
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptExperience EnhancementNoncontroversial enhancementsHelp WantedYou can do thisSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions