You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix: resolve targetKey collision issue in template generation by using JSON.stringify for tMap; update mental model documentation to reflect the fix and clarify template key generation
### Component with Dynamic Binding Inside Slottable (Under Investigation)
881
+
### Template targetKey Collision Bug (Fixed)
882
882
883
-
**Issue:**A component with dynamic bindings (e.g., `{title}`) used inside another component's slottable crashes at runtime.
883
+
**Issue:**Components with child content bindings on root elements could crash at runtime with `TypeError: Cannot read properties of undefined (reading 'data')`.
884
884
885
-
**Reproduction:**
886
-
```jsx
887
-
constCardTitle= ({ title }) => (
888
-
<h2 class="card-title">{title}</h2>
889
-
);
885
+
**Root Cause:** The `targetKey` hash in `Template.js` used `Array.join(';')` which flattens nested arrays:
**Status:** Fixed (see `packages/thoth/compiler.test.js` "targetKey must distinguish root vs child element bindings" test)
928
+
929
+
---
930
+
931
+
## Template Key Hashing Strategy
932
+
933
+
The compiler generates three hash keys for template deduplication:
934
+
935
+
### targetKey
936
+
Hash of `JSON.stringify(tMap)` — identifies the target function that locates DOM nodes to bind.
937
+
938
+
**tMap structure:**
939
+
- Child/component bindings on root: `[[childIndex]]`
940
+
- Child/component bindings on child: `[[queryIndex, childIndex]]`
941
+
- Property bindings: `[queryIndex]` (or `-1` for root)
942
+
943
+
### bindKey
944
+
Hash of `bMap.join(';') + propertyNames?.join(';')` — identifies the bind function that applies values.
945
+
946
+
**bMap structure:**
947
+
-`BIND.CHILD (1)` or `BIND.COMPONENT (2)`: positive number (binding type)
948
+
-`BIND.PROP (3)`: negative number (index into propertyNames × -1)
949
+
950
+
### Template ID
951
+
Hash of `html + bindKey + targetKey` — unique identifier for the entire template.
952
+
953
+
### Future Considerations
954
+
955
+
The current approach duplicates property names across templates. For large projects with many elements using common properties (e.g., `className`, `onClick`), this could be optimized:
956
+
957
+
1.**Property name interning:** Use indexes into a shared property name table
958
+
2.**Trade-off:** Smaller payload vs. extra indirection at runtime
959
+
960
+
The bindKey already uses indexes for property names within a template, but doesn't share across templates. Whether this optimization is worthwhile depends on actual payload sizes in production builds.
0 commit comments