Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spicy-worms-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"use-echarts-react": patch
---

feat: use Reflect in Proxy
8 changes: 5 additions & 3 deletions packages/use-echarts-react/src/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,11 @@ describe('utils', () => {
jest.spyOn(console, 'warn').mockImplementation(jest.fn);

mockEChartsInstance = {
id: `${Math.random()}`,
setOption: jest.fn(),
getOption: jest.fn(),
getOption: jest.fn(function () {
return this.id;
}),
resize: jest.fn(),
isDisposed: jest.fn().mockReturnValue(false)
} as unknown as EChartsType;
Expand All @@ -326,8 +329,7 @@ describe('utils', () => {

it('should allow accessing allowed properties', () => {
const proxy = createProxyEChartsInstance(mockEChartsInstance);

proxy.getOption();
expect(proxy.getOption()).toBe(mockEChartsInstance.id);
expect(mockEChartsInstance.getOption).toHaveBeenCalled();
});

Expand Down
9 changes: 4 additions & 5 deletions packages/use-echarts-react/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const createProxyEChartsInstance = (instance?: EChartsType | null, impera
return null as any;
}
return new Proxy(instance, {
get(target, p) {
get(target, p, receiver) {
if (FORBIT_PROP_SET.has(p)) {
console.warn(`Prop '${String(p)}' is now managed by useECharts. This method will be no longer applicable.`);
return undefined;
Expand All @@ -102,16 +102,15 @@ export const createProxyEChartsInstance = (instance?: EChartsType | null, impera
console.warn(`Method '${String(p)}' is now managed by useECharts. This method will be no longer applicable.`);
return noop;
}
return target[p as keyof typeof target];
return Reflect.get(target, p, receiver);
},
set(target, p, value) {
set(target, p, value, receiver) {
if (READONLY_PROP_SET.has(p)) {
console.warn(`Prop '${String(p)}' is protected and cannot be modified.`);
// not throw error.
return true;
}
target[p as keyof typeof target] = value;
return true;
return Reflect.set(target, p, value, receiver);
}
});
};
Expand Down
Loading