« get me outta code hell

data: process Thing subclasses in order - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/things
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2025-02-22 17:12:49 -0400
committer(quasar) nebula <qznebula@protonmail.com>2025-02-22 21:12:24 -0400
commit6820bb9b53d022394fc350c4f6307aa141954290 (patch)
treef73ca51d6e8363c7b972cbd41918e0824ed7cb5f /src/data/things
parenta90bca338d37db3880ef712980a3b5bc91cfd88a (diff)
data: process Thing subclasses in order
Diffstat (limited to 'src/data/things')
-rw-r--r--src/data/things/index.js15
1 files changed, 14 insertions, 1 deletions
diff --git a/src/data/things/index.js b/src/data/things/index.js
index d0eb5aa7..17471f31 100644
--- a/src/data/things/index.js
+++ b/src/data/things/index.js
@@ -6,6 +6,7 @@ import CacheableObject from '#cacheable-object';
 import {logError} from '#cli';
 import {compositeFrom} from '#composite';
 import * as serialize from '#serialize';
+import {withEntries} from '#sugar';
 import Thing from '#thing';
 
 import * as albumClasses from './album.js';
@@ -81,13 +82,25 @@ function errorDuplicateClassNames() {
 }
 
 function flattenClassLists() {
+  let allClassesUnsorted = Object.create(null);
+
   for (const classes of Object.values(allClassLists)) {
     for (const [name, constructor] of Object.entries(classes)) {
       if (typeof constructor !== 'function') continue;
       if (!(constructor.prototype instanceof Thing)) continue;
-      allClasses[name] = constructor;
+      allClassesUnsorted[name] = constructor;
     }
   }
+
+  // Sort subclasses after their superclasses.
+  Object.assign(allClasses,
+    withEntries(allClassesUnsorted, entries =>
+      entries.sort(({[1]: A}, {[1]: B}) =>
+        (A.prototype instanceof B
+          ? +1
+       : B.prototype instanceof A
+          ? -1
+          :  0))));
 }
 
 function descriptorAggregateHelper({