Skip to content

Commit

Permalink
Merge ee22b8a into daa419b
Browse files Browse the repository at this point in the history
  • Loading branch information
takeramagan committed Jan 14, 2024
2 parents daa419b + ee22b8a commit a3c382f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
20 changes: 18 additions & 2 deletions packages/solid/store/src/mutable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,35 @@ function wrap<T extends StoreNode>(value: T): T {
Object.defineProperty(value, $PROXY, { value: (p = new Proxy(value, proxyTraps)) });
const keys = Object.keys(value),
desc = Object.getOwnPropertyDescriptors(value);

const proto = Object.getPrototypeOf(value);
const isClass =
value !== null &&
typeof value === "object" &&
!Array.isArray(value) &&
proto !== Object.prototype;
if (isClass) {
const descriptors = Object.getOwnPropertyDescriptors(proto);
keys.push(...Object.keys(descriptors));
Object.assign(desc, descriptors);
}

for (let i = 0, l = keys.length; i < l; i++) {
const prop = keys[i];
if (isClass && prop === "constructor") continue;
if (desc[prop].get) {
const get = desc[prop].get!.bind(p);
Object.defineProperty(value, prop, {
get
get,
configurable: true
});
}
if (desc[prop].set) {
const og = desc[prop].set!,
set = (v: T[keyof T]) => batch(() => og.call(p, v));
Object.defineProperty(value, prop, {
set
set,
configurable: true
});
}
}
Expand Down
55 changes: 55 additions & 0 deletions packages/solid/store/test/mutableWithClass.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @jsxImportSource solid-js
* @vitest-environment jsdom
*/
import { createMutable } from "../src";
import { render } from "../../web";

describe("Class Operator test", () => {
test("read and set class", () => {
let ref: any;
class D {
f = 1;
get e() {
return this.f * 4;
}
}
class A {
a = 1;
get b() {
return this.a * 4;
}
child = new D();
}
let m: any;
function Test() {
m = createMutable(new A());
m.a++;
m.child.f++;

return (
<button
type="button"
onClick={() => {
m.a++;
m.child.f++;
}}
ref={ref}
>
{m.a} - {m.b}
</button>
);
}
const div = document.createElement("div");

render(Test, div);
expect(m.b).toBe(8);
expect(m.child.e).toBe(8);
ref.$$click();
expect(m.b).toBe(12);
expect(m.child.e).toBe(12);
ref.$$click();
expect(m.b).toBe(16);
expect(m.child.e).toBe(16);
});
});

0 comments on commit a3c382f

Please sign in to comment.