Skip to content

Commit

Permalink
🐛 Allows access to class methods on data component (#53)
Browse files Browse the repository at this point in the history
* 🐛 Allows accessing class methods of data contexts

* ⚡ Handles prototypemeth ods

* 🚧 Reflects changes accepted into Alpine

* 🏷️ Improves object type
  • Loading branch information
ekwoka authored Mar 3, 2024
1 parent 8300d51 commit a2c8d24
Show file tree
Hide file tree
Showing 12 changed files with 877 additions and 579 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@
"@esbuild/sunos-x64@<0.19.5": "0.19.5",
"@esbuild/win32-arm64@<0.19.5": "0.19.5",
"@esbuild/win32-ia32@<0.19.5": "0.19.5",
"@esbuild/win32-x64@<0.19.5": "0.19.5"
"@esbuild/win32-x64@<0.19.5": "0.19.5",
"vite@=4.5.0": ">=4.5.1",
"vite@>=4.0.0 <=4.5.1": ">=4.5.2"
}
}
}
22 changes: 7 additions & 15 deletions packages/alpinets/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,14 @@ function collapseProxies(this: Record<string, unknown>) {
return collapsed;
}

export const mergeProxies = <
T extends Record<string | number | symbol, unknown>,
>(
objects: T[],
) => {
export const mergeProxies = <T extends object>(objects: T[]) => {
const thisProxy = new Proxy({ objects }, proxyMerger);

return thisProxy as unknown as T;
};

type wrappedProxy = {
objects: Record<string | number | symbol, unknown>[];
objects: object[];
};

const proxyMerger: ProxyHandler<wrappedProxy> = {
Expand All @@ -74,25 +70,21 @@ const proxyMerger: ProxyHandler<wrappedProxy> = {
},
has(proxies, name) {
if (name == Symbol.unscopables) return false;
return proxies.objects.some((obj) =>
Object.prototype.hasOwnProperty.call(obj, name),
);

return proxies.objects.some((obj) => Reflect.has(obj, name));
},
get(proxies, name, thisProxy) {
if (name == 'toJSON') return collapseProxies;
return Reflect.get(
proxies.objects.find((obj) =>
Object.prototype.hasOwnProperty.call(obj, name),
) ?? {},
proxies.objects.find((obj) => Reflect.has(obj, name)) ?? {},
name as string,
thisProxy,
);
},
set(proxies, name, value, thisProxy) {
const target =
proxies.objects.find((obj) =>
Object.prototype.hasOwnProperty.call(obj, name),
) || proxies.objects[proxies.objects.length - 1];
proxies.objects.find((obj) => Reflect.has(obj, name)) ||
proxies.objects.at(-1);
const descriptor = Object.getOwnPropertyDescriptor(target, name);
if (descriptor?.set && descriptor?.get)
return Reflect.set(target, name, value, thisProxy);
Expand Down
14 changes: 14 additions & 0 deletions packages/alpinets/tests/scope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,18 @@ describe('mergeProxies', () => {
expect(objects[0].foo).toBe('baz');
expect(objects[1].foo).toBe('buzz');
});
it('properly works with classes', () => {
class Count {
constructor(public count = 0) {}
increment() {
this.count++;
}
}
const proxy = mergeProxies([new Count()]);
expect(JSON.stringify(proxy)).toBe('{"count":0}');
expect(proxy.count).toBe(0);
expect(proxy.increment).toBeInstanceOf(Function);
proxy.increment();
expect(proxy.count).toBe(1);
});
});
2 changes: 1 addition & 1 deletion packages/anchor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@
"@floating-ui/dom": "^1.5.3"
},
"peerDependencies": {
"alpinets": "*"
"alpinets": "workspace:^"
}
}
2 changes: 1 addition & 1 deletion packages/collapse/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"test": "vitest"
},
"peerDependencies": {
"alpinets": "*"
"alpinets": "workspace:^"
},
"devDependencies": {
"alpinets": "workspace:*"
Expand Down
2 changes: 1 addition & 1 deletion packages/focus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
"tabbable": "^5.3.3"
},
"peerDependencies": {
"alpinets": "*"
"alpinets": "workspace:^"
}
}
2 changes: 1 addition & 1 deletion packages/intersect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
"test": "vitest"
},
"peerDependencies": {
"alpinets": "*"
"alpinets": "workspace:^"
}
}
2 changes: 1 addition & 1 deletion packages/mask/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
"test": "vitest"
},
"peerDependencies": {
"alpinets": "*"
"alpinets": "workspace:^"
}
}
2 changes: 1 addition & 1 deletion packages/morph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"test": "vitest"
},
"peerDependencies": {
"alpinets": "*"
"alpinets": "workspace:^"
},
"devDependencies": {
"alpinets": "workspace:*"
Expand Down
2 changes: 1 addition & 1 deletion packages/persist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
"test": "vitest"
},
"peerDependencies": {
"alpinets": "*"
"alpinets": "workspace:^"
}
}
Loading

0 comments on commit a2c8d24

Please sign in to comment.