10
10
// is used, a fresh cache will always be created.
11
11
12
12
import { input , templateCompositeFrom } from '#composite' ;
13
+ import { sortByDate } from '#sort' ;
13
14
import { stitchArrays } from '#sugar' ;
14
15
15
16
import { exitWithoutDependency , raiseOutputWithoutDependency }
@@ -129,16 +130,19 @@ export default templateCompositeFrom({
129
130
130
131
// Actually fill in the cache record. Since we're building up a *reverse*
131
132
// reference list, track connections in terms of the referenced thing.
132
- // No newly-provided dependencies here since we're mutating the cache
133
- // record, which is properly in store and will probably be reused in the
134
- // future (and certainly in the next step).
133
+ // Although we gather all referenced things into a set and provide that
134
+ // for sorting purposes in the next step, we *don't* reprovide the cache
135
+ // record, because we're mutating that in-place - we'll just reuse its
136
+ // existing '#cacheRecord' dependency.
135
137
{
136
138
dependencies : [ '#cacheRecord' , '#referencingThings' , '#referencedThings' ] ,
137
139
compute : ( continuation , {
138
140
[ '#cacheRecord' ] : cacheRecord ,
139
141
[ '#referencingThings' ] : referencingThings ,
140
142
[ '#referencedThings' ] : referencedThings ,
141
143
} ) => {
144
+ const allReferencedThings = new Set ( ) ;
145
+
142
146
stitchArrays ( {
143
147
referencingThing : referencingThings ,
144
148
referencedThings : referencedThings ,
@@ -148,10 +152,36 @@ export default templateCompositeFrom({
148
152
cacheRecord . get ( referencedThing ) . push ( referencingThing ) ;
149
153
} else {
150
154
cacheRecord . set ( referencedThing , [ referencingThing ] ) ;
155
+ allReferencedThings . add ( referencedThing ) ;
151
156
}
152
157
}
153
158
} ) ;
154
159
160
+ return continuation ( {
161
+ [ '#allReferencedThings' ] :
162
+ allReferencedThings ,
163
+ } ) ;
164
+ } ,
165
+ } ,
166
+
167
+ // Sort the entries in the cache records, too, just by date - the rest of
168
+ // sorting should be handled outside of withReverseContributionList, either
169
+ // preceding (changing the 'data' input) or following (sorting the output).
170
+ // Again we're mutating in place, so no need to reprovide '#cacheRecord'
171
+ // here.
172
+ {
173
+ dependencies : [ '#cacheRecord' , '#allReferencedThings' ] ,
174
+ compute : ( continuation , {
175
+ [ '#cacheRecord' ] : cacheRecord ,
176
+ [ '#allReferencedThings' ] : allReferencedThings ,
177
+ } ) => {
178
+ for ( const referencedThing of allReferencedThings ) {
179
+ if ( cacheRecord . has ( referencedThing ) ) {
180
+ const referencingThings = cacheRecord . get ( referencedThing ) ;
181
+ sortByDate ( referencingThings ) ;
182
+ }
183
+ }
184
+
155
185
return continuation ( ) ;
156
186
} ,
157
187
} ,
0 commit comments