« get me outta code hell

aggregate: receive single {aggregate, result} object - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-05-07 10:00:38 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-05-07 10:00:38 -0300
commitc447d44bf78efcb32a0a7427aff0dc1d44d3a6fc (patch)
treee1fc01ddaf873e7a9647785efce606ea0834f2b0
parent1d5513d02d23034aff3bd043a1e2e6755abcb93d (diff)
aggregate: receive single {aggregate, result} object
-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) {