« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/util/aggregate.js19
1 files changed, 18 insertions, 1 deletions
diff --git a/src/util/aggregate.js b/src/util/aggregate.js
index 2e4d9de7..5429e640 100644
--- a/src/util/aggregate.js
+++ b/src/util/aggregate.js
@@ -93,10 +93,27 @@ export function openAggregate({
 
   aggregate.receive = (results) => {
     if (!Array.isArray(results)) {
-      throw new Error(`Expected an array`);
+      if (typeof results === 'object' && results.aggregate) {
+        const {aggregate, result} = results;
+
+        try {
+          aggregate.close();
+        } catch (error) {
+          errors.push(error);
+        }
+
+        return result;
+      }
+
+      throw new Error(`Expected an array or {aggregate, result} object`);
     }
 
     return results.map(({aggregate, result}) => {
+      if (!aggregate) {
+        console.log('nope:', results);
+        throw new Error(`Expected an array of {aggregate, result} objects`);
+      }
+
       try {
         aggregate.close();
       } catch (error) {