Skip to content

Commit 4b50edb

Browse files
authored
docs(changeset): feat: use Reflect in Proxy (#2)
1 parent ae0b56a commit 4b50edb

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

.changeset/spicy-worms-invent.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"use-echarts-react": patch
3+
---
4+
5+
feat: use Reflect in Proxy

packages/use-echarts-react/src/utils.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,11 @@ describe('utils', () => {
312312
jest.spyOn(console, 'warn').mockImplementation(jest.fn);
313313

314314
mockEChartsInstance = {
315+
id: `${Math.random()}`,
315316
setOption: jest.fn(),
316-
getOption: jest.fn(),
317+
getOption: jest.fn(function () {
318+
return this.id;
319+
}),
317320
resize: jest.fn(),
318321
isDisposed: jest.fn().mockReturnValue(false)
319322
} as unknown as EChartsType;
@@ -326,8 +329,7 @@ describe('utils', () => {
326329

327330
it('should allow accessing allowed properties', () => {
328331
const proxy = createProxyEChartsInstance(mockEChartsInstance);
329-
330-
proxy.getOption();
332+
expect(proxy.getOption()).toBe(mockEChartsInstance.id);
331333
expect(mockEChartsInstance.getOption).toHaveBeenCalled();
332334
});
333335

packages/use-echarts-react/src/utils.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export const createProxyEChartsInstance = (instance?: EChartsType | null, impera
9393
return null as any;
9494
}
9595
return new Proxy(instance, {
96-
get(target, p) {
96+
get(target, p, receiver) {
9797
if (FORBIT_PROP_SET.has(p)) {
9898
console.warn(`Prop '${String(p)}' is now managed by useECharts. This method will be no longer applicable.`);
9999
return undefined;
@@ -102,16 +102,15 @@ export const createProxyEChartsInstance = (instance?: EChartsType | null, impera
102102
console.warn(`Method '${String(p)}' is now managed by useECharts. This method will be no longer applicable.`);
103103
return noop;
104104
}
105-
return target[p as keyof typeof target];
105+
return Reflect.get(target, p, receiver);
106106
},
107-
set(target, p, value) {
107+
set(target, p, value, receiver) {
108108
if (READONLY_PROP_SET.has(p)) {
109109
console.warn(`Prop '${String(p)}' is protected and cannot be modified.`);
110110
// not throw error.
111111
return true;
112112
}
113-
target[p as keyof typeof target] = value;
114-
return true;
113+
return Reflect.set(target, p, value, receiver);
115114
}
116115
});
117116
};

0 commit comments

Comments
 (0)