Skip to content

Commit cbff564

Browse files
ddomonkosryansolid
andauthored
feat: createMutable support for class inheritance (#2405)
* feat: createMutable support for class inheritance * Create tasty-garlics-agree.md --------- Co-authored-by: Ryan Carniato <[email protected]>
1 parent 2cd810f commit cbff564

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

.changeset/tasty-garlics-agree.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"solid-js": patch
3+
---
4+
5+
feat: createMutable support for class inheritance

packages/solid/store/src/mutable.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,13 @@ function wrap<T extends StoreNode>(value: T): T {
104104
!Array.isArray(value) &&
105105
proto !== Object.prototype;
106106
if (isClass) {
107-
const descriptors = Object.getOwnPropertyDescriptors(proto);
108-
keys.push(...Object.keys(descriptors));
109-
Object.assign(desc, descriptors);
107+
let curProto = proto;
108+
while (curProto != null) {
109+
const descriptors = Object.getOwnPropertyDescriptors(curProto);
110+
keys.push(...Object.keys(descriptors));
111+
Object.assign(desc, descriptors);
112+
curProto = Object.getPrototypeOf(curProto);
113+
}
110114
}
111115

112116
for (let i = 0, l = keys.length; i < l; i++) {

packages/solid/store/test/mutableWithClass.spec.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,27 @@ describe("Class Operator test", () => {
5353
expect(count).toBe(3);
5454
expect(childCount).toBe(1);
5555
});
56+
57+
test("inherited properties", () => {
58+
class A {
59+
val = 0;
60+
get getVal() {
61+
return this.val;
62+
}
63+
}
64+
class B extends A {}
65+
66+
const instance = createMutable(new B());
67+
let lastVal: number | undefined;
68+
69+
createRoot(() => {
70+
createEffect(() => {
71+
lastVal = instance.getVal;
72+
});
73+
});
74+
75+
expect(lastVal).toBe(0);
76+
instance.val = 1;
77+
expect(lastVal).toBe(1);
78+
});
5679
});

0 commit comments

Comments
 (0)