Skip to content
This repository was archived by the owner on Jan 25, 2022. It is now read-only.
This repository was archived by the owner on Jan 25, 2022. It is now read-only.

FAQ: Why not just add a callback to WeakMap/WeakSet? #53

@xaviergonz

Description

@xaviergonz

What if it was done like this instead?

const weakObjs = new WeakSet([], objectAboutToBeCollected => {
  // here do something with the object right before it is going to be collected,e.g.
  objectAboutToBeCollected.dispose();
  // or if objectAboutToBeCollected is attached to a live object then collection is cancelled,
  // this method will be called again when it is ready to be collected once more
});

same for WeakMap

if listening for an object finalization is no longer needed then it can just be removed from the set / the set can be collected

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions