Skip to content

Commit b05303e

Browse files
Fix CRS cached variants immutability
1 parent 4fd8f05 commit b05303e

1 file changed

Lines changed: 26 additions & 10 deletions

File tree

‎lib/src/geo/crs.dart‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,27 +85,43 @@ abstract class Crs {
8585
}
8686
}
8787

88+
final class _ScaleZoomCache {
89+
double lastScaleZoom = double.nan;
90+
double lastScaleValue = double.nan;
91+
double lastZoomScale = double.nan;
92+
double lastZoomValue = double.nan;
93+
}
94+
8895
mixin _ScaleCacheMixin on Crs {
89-
double _lastScaleZoom = double.nan;
90-
double _lastScaleValue = double.nan;
91-
double _lastZoomScale = double.nan;
92-
double _lastZoomValue = double.nan;
96+
static final Expando<_ScaleZoomCache> _caches = Expando<_ScaleZoomCache>(
97+
'_scaleZoomCache',
98+
);
99+
100+
_ScaleZoomCache get _cache {
101+
final existing = _caches[this];
102+
if (existing != null) return existing;
103+
final created = _ScaleZoomCache();
104+
_caches[this] = created;
105+
return created;
106+
}
93107

94108
@override
95109
double scale(double zoom) {
96-
if (zoom == _lastScaleZoom) return _lastScaleValue;
110+
final cache = _cache;
111+
if (zoom == cache.lastScaleZoom) return cache.lastScaleValue;
97112
final value = super.scale(zoom);
98-
_lastScaleZoom = zoom;
99-
_lastScaleValue = value;
113+
cache.lastScaleZoom = zoom;
114+
cache.lastScaleValue = value;
100115
return value;
101116
}
102117

103118
@override
104119
double zoom(double scale) {
105-
if (scale == _lastZoomScale) return _lastZoomValue;
120+
final cache = _cache;
121+
if (scale == cache.lastZoomScale) return cache.lastZoomValue;
106122
final value = super.zoom(scale);
107-
_lastZoomScale = scale;
108-
_lastZoomValue = value;
123+
cache.lastZoomScale = scale;
124+
cache.lastZoomValue = value;
109125
return value;
110126
}
111127
}

0 commit comments

Comments
 (0)