@@ -26,34 +26,39 @@ public FeatureReferences(IFeatureCollection collection)
26
26
public TFeature Fetch < TFeature , TState > (
27
27
ref TFeature cached ,
28
28
TState state ,
29
- Func < TState , TFeature > factory )
29
+ Func < TState , TFeature > factory ) where TFeature : class
30
30
{
31
- var cleared = false ;
32
- if ( Revision != Collection . Revision )
31
+ var revision = Collection . Revision ;
32
+ if ( Revision == revision )
33
33
{
34
- cleared = true ;
35
- Cache = default ( TCache ) ;
36
- Revision = Collection . Revision ;
34
+ // collection unchanged, use cached
35
+ return cached ?? UpdateCached ( ref cached , state , factory ) ;
37
36
}
38
37
39
- var feature = cached ;
40
- if ( feature == null || cleared )
41
- {
42
- feature = Collection . Get < TFeature > ( ) ;
43
- if ( feature == null )
44
- {
45
- feature = factory ( state ) ;
38
+ // collection changed, clear cache
39
+ Cache = default ( TCache ) ;
40
+ // empty cache is current revision
41
+ Revision = revision ;
46
42
47
- Collection . Set ( feature ) ;
43
+ return UpdateCached ( ref cached , state , factory ) ;
44
+ }
48
45
49
- Revision = Collection . Revision ;
50
- }
51
- cached = feature ;
46
+ private TFeature UpdateCached < TFeature , TState > ( ref TFeature cached , TState state , Func < TState , TFeature > factory ) where TFeature : class
47
+ {
48
+ cached = Collection . Get < TFeature > ( ) ;
49
+ if ( cached == null )
50
+ {
51
+ // create if item not in collection
52
+ cached = factory ( state ) ;
53
+ Collection . Set ( cached ) ;
54
+ // Revision changed by .Set, update revision
55
+ Revision = Collection . Revision ;
52
56
}
53
- return feature ;
57
+
58
+ return cached ;
54
59
}
55
60
56
- public TFeature Fetch < TFeature > ( ref TFeature cached , Func < IFeatureCollection , TFeature > factory ) =>
57
- Fetch ( ref cached , Collection , factory ) ;
61
+ public TFeature Fetch < TFeature > ( ref TFeature cached , Func < IFeatureCollection , TFeature > factory )
62
+ where TFeature : class => Fetch ( ref cached , Collection , factory ) ;
58
63
}
59
64
}
0 commit comments