benjamn / optimism

Composable reactive caching with efficient invalidation.
MIT License
114 stars 19 forks source link

Add configurable `WeakCache` implementation based on `FinalizationRegistry` #599

Closed phryneas closed 1 year ago

phryneas commented 1 year ago

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

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 1e26a2c0695d9ba9af488042728f5d69a724f4af independently - the first commit in this PR only copies over the existing Cache, so it adds a lot of clutter.

phryneas commented 1 year ago

Superseded by #615 and https://github.com/benjamn/wryware/pull/568