Skip to content

Commit 9bba4a6

Browse files
whxaxeswanghx
andauthored
fix: proxy function lost this (#48)
Co-authored-by: wanghx <whx89768@antgroup.com>
1 parent 4679e4d commit 9bba4a6

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

src/lazy_helper.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ function createHandler<T extends Object>(delayedObject: () => T): ProxyHandler<T
77
const handler: ProxyHandler<T> = {};
88
const install = (name: keyof ProxyHandler<T>) => {
99
handler[name] = (...args: any[]) => {
10-
args[0] = delayedObject();
10+
const instance = args[0] = delayedObject();
1111
const method = Reflect[name];
12-
return (method as any)(...args);
12+
const result = (method as any)(...args);
13+
return typeof result === 'function'
14+
? result.bind(instance)
15+
: result;
1316
};
1417
};
1518
reflectMethods.forEach(install);

test/container.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import ClassB from './fixtures/value/b';
1212
import LazyCClass from './fixtures/lazy/lazy_c';
1313
import LazyBClass from './fixtures/lazy/lazy_b';
1414
import LazyAClass from './fixtures/lazy/lazy_a';
15+
import LazyDClass from './fixtures/lazy/lazy_d';
1516

1617
const ctx = {};
1718
const container = new Container('default');
@@ -282,10 +283,13 @@ describe('container#lazy', () => {
282283
const instance = container.get(LazyAClass);
283284
expect(instance).toBeInstanceOf(LazyAClass);
284285
container.set({ type: LazyBClass });
286+
container.set({ type: LazyDClass });
285287
expect(instance.lazyB).toBeDefined();
286288
expect(instance.lazyB).toBeInstanceOf(LazyBClass);
287289
expect(instance.lazyB === instance.lazyB).toBeTruthy();
288290
expect(instance.lazyB.name).toBe('lazyBClass');
291+
expect(instance.lazyB.testLazyD()).toBe('a,b');
292+
289293
container.set({ type: LazyCClass });
290294
const instanceb = container.get(LazyBClass);
291295
expect(instanceb.lazyC).toBeDefined();

test/fixtures/lazy/lazy_b.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
import LazyC from './lazy_c';
2+
import LazyD from './lazy_d';
23
import { Inject, Injectable } from '../../../src';
34

45
@Injectable()
56
export default class LazyBClass {
67
@Inject({ lazy: true })
78
lazyC!: LazyC;
9+
10+
@Inject({ lazy: true })
11+
lazyD!: LazyD;
12+
813
public name = 'lazyBClass';
9-
}
14+
15+
testLazyD() {
16+
this.lazyD.set('a', 'b');
17+
return this.lazyD.doSomething();
18+
}
19+
}

test/fixtures/lazy/lazy_d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Injectable } from '../../../src';
2+
3+
@Injectable()
4+
export default class LazyDClass extends Map {
5+
doSomething() {
6+
return Array.from(this.entries()).join(',');
7+
}
8+
}

0 commit comments

Comments
 (0)