Skip to content

Commit c28cfca

Browse files
committed
data: withReverse{Contribution,Reference}List: sort by date
1 parent 8c545a1 commit c28cfca

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed

src/data/composite/wiki-data/withReverseContributionList.js

+33-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// is used, a fresh cache will always be created.
1111

1212
import {input, templateCompositeFrom} from '#composite';
13+
import {sortByDate} from '#sort';
1314
import {stitchArrays} from '#sugar';
1415

1516
import {exitWithoutDependency, raiseOutputWithoutDependency}
@@ -129,16 +130,19 @@ export default templateCompositeFrom({
129130

130131
// Actually fill in the cache record. Since we're building up a *reverse*
131132
// 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.
135137
{
136138
dependencies: ['#cacheRecord', '#referencingThings', '#referencedThings'],
137139
compute: (continuation, {
138140
['#cacheRecord']: cacheRecord,
139141
['#referencingThings']: referencingThings,
140142
['#referencedThings']: referencedThings,
141143
}) => {
144+
const allReferencedThings = new Set();
145+
142146
stitchArrays({
143147
referencingThing: referencingThings,
144148
referencedThings: referencedThings,
@@ -148,10 +152,36 @@ export default templateCompositeFrom({
148152
cacheRecord.get(referencedThing).push(referencingThing);
149153
} else {
150154
cacheRecord.set(referencedThing, [referencingThing]);
155+
allReferencedThings.add(referencedThing);
151156
}
152157
}
153158
});
154159

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+
155185
return continuation();
156186
},
157187
},

src/data/composite/wiki-data/withReverseReferenceList.js

+33-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// so any changes should be reflected there (until these are combined).
1313

1414
import {input, templateCompositeFrom} from '#composite';
15+
import {sortByDate} from '#sort';
1516
import {stitchArrays} from '#sugar';
1617

1718
import {exitWithoutDependency, raiseOutputWithoutDependency}
@@ -128,16 +129,19 @@ export default templateCompositeFrom({
128129

129130
// Actually fill in the cache record. Since we're building up a *reverse*
130131
// reference list, track connections in terms of the referenced thing.
131-
// No newly-provided dependencies here since we're mutating the cache
132-
// record, which is properly in store and will probably be reused in the
133-
// future (and certainly in the next step).
132+
// Although we gather all referenced things into a set and provide that
133+
// for sorting purposes in the next step, we *don't* reprovide the cache
134+
// record, because we're mutating that in-place - we'll just reuse its
135+
// existing '#cacheRecord' dependency.
134136
{
135137
dependencies: ['#cacheRecord', '#referencingThings', '#referencedThings'],
136138
compute: (continuation, {
137139
['#cacheRecord']: cacheRecord,
138140
['#referencingThings']: referencingThings,
139141
['#referencedThings']: referencedThings,
140142
}) => {
143+
const allReferencedThings = new Set();
144+
141145
stitchArrays({
142146
referencingThing: referencingThings,
143147
referencedThings: referencedThings,
@@ -147,10 +151,36 @@ export default templateCompositeFrom({
147151
cacheRecord.get(referencedThing).push(referencingThing);
148152
} else {
149153
cacheRecord.set(referencedThing, [referencingThing]);
154+
allReferencedThings.add(referencedThing);
150155
}
151156
}
152157
});
153158

159+
return continuation({
160+
['#allReferencedThings']:
161+
allReferencedThings,
162+
});
163+
},
164+
},
165+
166+
// Sort the entries in the cache records, too, just by date - the rest of
167+
// sorting should be handled outside of withReverseContributionList, either
168+
// preceding (changing the 'data' input) or following (sorting the output).
169+
// Again we're mutating in place, so no need to reprovide '#cacheRecord'
170+
// here.
171+
{
172+
dependencies: ['#cacheRecord', '#allReferencedThings'],
173+
compute: (continuation, {
174+
['#cacheRecord']: cacheRecord,
175+
['#allReferencedThings']: allReferencedThings,
176+
}) => {
177+
for (const referencedThing of allReferencedThings) {
178+
if (cacheRecord.has(referencedThing)) {
179+
const referencingThings = cacheRecord.get(referencedThing);
180+
sortByDate(referencingThings);
181+
}
182+
}
183+
154184
return continuation();
155185
},
156186
},

0 commit comments

Comments
 (0)