« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/composite/wiki-data
diff options
context:
space:
mode:
Diffstat (limited to 'src/data/composite/wiki-data')
-rw-r--r--src/data/composite/wiki-data/withReverseContributionList.js36
-rw-r--r--src/data/composite/wiki-data/withReverseReferenceList.js36
2 files changed, 66 insertions, 6 deletions
diff --git a/src/data/composite/wiki-data/withReverseContributionList.js b/src/data/composite/wiki-data/withReverseContributionList.js
index 63e712bb..07b085c4 100644
--- a/src/data/composite/wiki-data/withReverseContributionList.js
+++ b/src/data/composite/wiki-data/withReverseContributionList.js
@@ -10,6 +10,7 @@
 // is used, a fresh cache will always be created.
 
 import {input, templateCompositeFrom} from '#composite';
+import {sortByDate} from '#sort';
 import {stitchArrays} from '#sugar';
 
 import {exitWithoutDependency, raiseOutputWithoutDependency}
@@ -129,9 +130,10 @@ export default templateCompositeFrom({
 
     // Actually fill in the cache record. Since we're building up a *reverse*
     // reference list, track connections in terms of the referenced thing.
-    // No newly-provided dependencies here since we're mutating the cache
-    // record, which is properly in store and will probably be reused in the
-    // future (and certainly in the next step).
+    // Although we gather all referenced things into a set and provide that
+    // for sorting purposes in the next step, we *don't* reprovide the cache
+    // record, because we're mutating that in-place - we'll just reuse its
+    // existing '#cacheRecord' dependency.
     {
       dependencies: ['#cacheRecord', '#referencingThings', '#referencedThings'],
       compute: (continuation, {
@@ -139,6 +141,8 @@ export default templateCompositeFrom({
         ['#referencingThings']: referencingThings,
         ['#referencedThings']: referencedThings,
       }) => {
+        const allReferencedThings = new Set();
+
         stitchArrays({
           referencingThing: referencingThings,
           referencedThings: referencedThings,
@@ -148,10 +152,36 @@ export default templateCompositeFrom({
                 cacheRecord.get(referencedThing).push(referencingThing);
               } else {
                 cacheRecord.set(referencedThing, [referencingThing]);
+                allReferencedThings.add(referencedThing);
               }
             }
           });
 
+        return continuation({
+          ['#allReferencedThings']:
+            allReferencedThings,
+        });
+      },
+    },
+
+    // Sort the entries in the cache records, too, just by date - the rest of
+    // sorting should be handled outside of withReverseContributionList, either
+    // preceding (changing the 'data' input) or following (sorting the output).
+    // Again we're mutating in place, so no need to reprovide '#cacheRecord'
+    // here.
+    {
+      dependencies: ['#cacheRecord', '#allReferencedThings'],
+      compute: (continuation, {
+        ['#cacheRecord']: cacheRecord,
+        ['#allReferencedThings']: allReferencedThings,
+      }) => {
+        for (const referencedThing of allReferencedThings) {
+          if (cacheRecord.has(referencedThing)) {
+            const referencingThings = cacheRecord.get(referencedThing);
+            sortByDate(referencingThings);
+          }
+        }
+
         return continuation();
       },
     },
diff --git a/src/data/composite/wiki-data/withReverseReferenceList.js b/src/data/composite/wiki-data/withReverseReferenceList.js
index 1f8c082f..2da9194d 100644
--- a/src/data/composite/wiki-data/withReverseReferenceList.js
+++ b/src/data/composite/wiki-data/withReverseReferenceList.js
@@ -12,6 +12,7 @@
 // so any changes should be reflected there (until these are combined).
 
 import {input, templateCompositeFrom} from '#composite';
+import {sortByDate} from '#sort';
 import {stitchArrays} from '#sugar';
 
 import {exitWithoutDependency, raiseOutputWithoutDependency}
@@ -128,9 +129,10 @@ export default templateCompositeFrom({
 
     // Actually fill in the cache record. Since we're building up a *reverse*
     // reference list, track connections in terms of the referenced thing.
-    // No newly-provided dependencies here since we're mutating the cache
-    // record, which is properly in store and will probably be reused in the
-    // future (and certainly in the next step).
+    // Although we gather all referenced things into a set and provide that
+    // for sorting purposes in the next step, we *don't* reprovide the cache
+    // record, because we're mutating that in-place - we'll just reuse its
+    // existing '#cacheRecord' dependency.
     {
       dependencies: ['#cacheRecord', '#referencingThings', '#referencedThings'],
       compute: (continuation, {
@@ -138,6 +140,8 @@ export default templateCompositeFrom({
         ['#referencingThings']: referencingThings,
         ['#referencedThings']: referencedThings,
       }) => {
+        const allReferencedThings = new Set();
+
         stitchArrays({
           referencingThing: referencingThings,
           referencedThings: referencedThings,
@@ -147,10 +151,36 @@ export default templateCompositeFrom({
                 cacheRecord.get(referencedThing).push(referencingThing);
               } else {
                 cacheRecord.set(referencedThing, [referencingThing]);
+                allReferencedThings.add(referencedThing);
               }
             }
           });
 
+        return continuation({
+          ['#allReferencedThings']:
+            allReferencedThings,
+        });
+      },
+    },
+
+    // Sort the entries in the cache records, too, just by date - the rest of
+    // sorting should be handled outside of withReverseContributionList, either
+    // preceding (changing the 'data' input) or following (sorting the output).
+    // Again we're mutating in place, so no need to reprovide '#cacheRecord'
+    // here.
+    {
+      dependencies: ['#cacheRecord', '#allReferencedThings'],
+      compute: (continuation, {
+        ['#cacheRecord']: cacheRecord,
+        ['#allReferencedThings']: allReferencedThings,
+      }) => {
+        for (const referencedThing of allReferencedThings) {
+          if (cacheRecord.has(referencedThing)) {
+            const referencingThings = cacheRecord.get(referencedThing);
+            sortByDate(referencingThings);
+          }
+        }
+
         return continuation();
       },
     },