« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/thing/cacheable-object.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/thing/cacheable-object.js')
-rw-r--r--src/thing/cacheable-object.js36
1 files changed, 35 insertions, 1 deletions
diff --git a/src/thing/cacheable-object.js b/src/thing/cacheable-object.js
index 3c14101..9af4160 100644
--- a/src/thing/cacheable-object.js
+++ b/src/thing/cacheable-object.js
@@ -83,6 +83,8 @@ function inspect(value) {
 }
 
 export default class CacheableObject {
+    static instance = Symbol('CacheableObject `this` instance');
+
     #propertyUpdateValues = Object.create(null);
     #propertyUpdateCacheInvalidators = Object.create(null);
 
@@ -100,6 +102,19 @@ export default class CacheableObject {
     constructor() {
         this.#defineProperties();
         this.#initializeUpdatingPropertyValues();
+
+        if (CacheableObject.DEBUG_SLOW_TRACK_INVALID_PROPERTIES) {
+            return new Proxy(this, {
+                get: (obj, key) => {
+                    if (!Object.hasOwn(obj, key)) {
+                        if (key !== 'constructor') {
+                            CacheableObject._invalidAccesses.add(`(${obj.constructor.name}).${key}`);
+                        }
+                    }
+                    return obj[key];
+                }
+            });
+        }
     }
 
     #initializeUpdatingPropertyValues() {
@@ -227,7 +242,8 @@ export default class CacheableObject {
 
         const dependencyKeys = expose.dependencies || [];
         const dependencyGetters = dependencyKeys.map(key => () => [key, this.#propertyUpdateValues[key]]);
-        const getAllDependencies = () => Object.fromEntries(dependencyGetters.map(f => f()));
+        const getAllDependencies = () => Object.fromEntries(dependencyGetters.map(f => f())
+            .concat([[this.constructor.instance, this]]));
 
         if (flags.update) {
             return () => transform(this.#propertyUpdateValues[property], getAllDependencies());
@@ -268,4 +284,22 @@ export default class CacheableObject {
             }
         };
     }
+
+    static DEBUG_SLOW_TRACK_INVALID_PROPERTIES = false;
+    static _invalidAccesses = new Set();
+
+    static showInvalidAccesses() {
+        if (!this.DEBUG_SLOW_TRACK_INVALID_PROPERTIES) {
+            return;
+        }
+
+        if (!this._invalidAccesses.size) {
+            return;
+        }
+
+        console.log(`${this._invalidAccesses.size} unique invalid accesses:`);
+        for (const line of this._invalidAccesses) {
+            console.log(` - ${line}`);
+        }
+    }
 }