« get me outta code hell

data: misc. utility additions - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-08-31 15:53:02 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-09-05 21:02:55 -0300
commitc0bbd7e8fa6c76df4fa492e3a9d3b5e9ef42ec5c (patch)
tree21704bbb4bc15fdcb955d5662460cf8da63c62f3 /src
parent001bcb69db4f4050fca222568ae2895f58a2f0df (diff)
data: misc. utility additions
* add earlyExitWithoutUpdateValue
* add raiseWithoutDependency
* add raiseWithoutUpdateValue
* add earlyExitIfAvailabilityCheckFailed (internal)
* refactor earlyExitWithoutDependency

The "raise" utilities make use of the new `raiseAbove` continuation
feature.
Diffstat (limited to 'src')
-rw-r--r--src/data/things/thing.js100
1 files changed, 92 insertions, 8 deletions
diff --git a/src/data/things/thing.js b/src/data/things/thing.js
index 782946ce..501286d7 100644
--- a/src/data/things/thing.js
+++ b/src/data/things/thing.js
@@ -1384,6 +1384,36 @@ export default class Thing extends CacheableObject {
       ]);
     },
 
+    // Early exits if an availability check fails.
+    // This is for internal use only - use `earlyExitWithoutDependency` or
+    // `earlyExitWIthoutUpdateValue` instead.
+    earlyExitIfAvailabilityCheckFailed({
+      availability = '#availability',
+      value = null,
+    }) {
+      return Thing.composite.from(`Thing.composite.earlyExitIfAvailabilityCheckFailed`, [
+        {
+          flags: {expose: true, compose: true},
+          expose: {
+            mapDependencies: {availability},
+            compute: ({availability}, continuation) =>
+              (availability
+                ? continuation.raise()
+                : continuation()),
+          },
+        },
+
+        {
+          flags: {expose: true, compose: true},
+          expose: {
+            options: {value},
+            compute: ({'#options': {value}}, continuation) =>
+              continuation.exit(value),
+          },
+        },
+      ]);
+    },
+
     // Early exits if a dependency isn't available.
     // See withResultOfAvailabilityCheck for {mode} options!
     earlyExitWithoutDependency(dependency, {
@@ -1391,10 +1421,32 @@ export default class Thing extends CacheableObject {
       value = null,
     } = {}) {
       return Thing.composite.from(`Thing.composite.earlyExitWithoutDependency`, [
-        Thing.composite.withResultOfAvailabilityCheck({
-          fromDependency: dependency,
-          mode,
-        }),
+        Thing.composite.withResultOfAvailabilityCheck({fromDependency: dependency, mode}),
+        Thing.composite.earlyExitIfAvailabilityCheckFailed({value}),
+      ]);
+    },
+
+    // Early exits if this property's update value isn't available.
+    // See withResultOfAvailabilityCheck for {mode} options!
+    earlyExitWithoutUpdateValue({
+      mode = 'null',
+      value = null,
+    } = {}) {
+      return Thing.composite.from(`Thing.composite.earlyExitWithoutDependency`, [
+        Thing.composite.withResultOfAvailabilityCheck({fromUpdateValue: true, mode}),
+        Thing.composite.earlyExitIfAvailabilityCheckFailed({value}),
+      ]);
+    },
+
+    // Raises if a dependency isn't available.
+    // See withResultOfAvailabilityCheck for {mode} options!
+    raiseWithoutDependency(dependency, {
+      mode = 'null',
+      map = {},
+      raise = {},
+    } = {}) {
+      return Thing.composite.from(`Thing.composite.raiseWithoutDependency`, [
+        Thing.composite.withResultOfAvailabilityCheck({fromDependency: dependency, mode}),
 
         {
           flags: {expose: true, compose: true},
@@ -1410,11 +1462,43 @@ export default class Thing extends CacheableObject {
         {
           flags: {expose: true, compose: true},
           expose: {
-            dependencies: ['#availability'],
-            options: {value},
+            options: {raise},
+            mapContinuation: map,
+            compute: ({'#options': {raise}}, continuation) =>
+              continuation.raiseAbove(raise),
+          },
+        },
+      ]);
+    },
 
-            compute: ({'#options': {value}}, continuation) =>
-              continuation.exit(value),
+    // Raises if this property's update value isn't available.
+    // See withResultOfAvailabilityCheck for {mode} options!
+    raiseWithoutUpdateValue({
+      mode = 'null',
+      map = {},
+      raise = {},
+    } = {}) {
+      return Thing.composite.from(`Thing.composite.raiseWithoutUpdateValue`, [
+        Thing.composite.withResultOfAvailabilityCheck({fromUpdateValue: true, mode}),
+
+        {
+          flags: {expose: true, compose: true},
+          expose: {
+            mapDependencies: {availability},
+            compute: ({availability}, continuation) =>
+              (availability
+                ? continuation.raise()
+                : continuation()),
+          },
+        },
+
+        {
+          flags: {expose: true, compose: true},
+          expose: {
+            options: {raise},
+            mapContinuation: map,
+            compute: ({'#options': {raise}}, continuation) =>
+              continuation.raiseAbove(raise),
           },
         },
       ]);