Skip to content

Add configurable WeakCache implementation based on FinalizationRegistry - #599

Closed
phryneas wants to merge 4 commits into
benjamn:mainfrom
phryneas:pr/weakCache
Closed

phryneas wants to merge 4 commits into
benjamn:mainfrom
phryneas:pr/weakCache

Conversation

@phryneas

@phryneas phryneas commented Oct 6, 2023 •

Copy link
Copy Markdown
Contributor

This would be an alternative approach to #132 - and adds a second implementation of Cache that only allows object keys and is based on WeakMap, with automatic cleanup.

I tried integrating it with the original Cache at first, but it

  • made the implementation hard to reason about
  • changed the public api of Cache (there cannot be a guarantee that dispose is called with a key!)
  • felt a lot less intentional - it would end up as a cache that accepted both strong and weak keys, which could invite bugs in downstream code. I prefer this option of a "deliberately weak-only" alternative implementation.

The new WeakCache would be incredibly useful in Apollo Client in multiple places, and could also be plugged in quite a lot of our wrap usages.

This implementation polyfills WeakMap with Map and FinalizationRegistry and WeakRef with dummy functions, so it will work in e.g. React Native, but it won't collect weak values.

I haven't finished tests on cleanup behaviour yet, as I wanted to wait for feedback if this is interesting at all first.

For review purposes, it can make sense to look at commit 1e26a2c independently - the first commit in this PR only copies over the existing Cache, so it adds a lot of clutter.

Comment thread src/weakCache.ts Resolved
@phryneas

Copy link
Copy Markdown
Contributor Author

Superseded by #615 and benjamn/wryware#568

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant