Skip to content

Commit 8a60c0b

Browse files
authored
Ensure proper this context in lifecycle callback defined at the time of instantiation (#383)
1 parent d85234b commit 8a60c0b

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

packages/frint/src/App.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,4 +511,35 @@ describe('frint › App', function () {
511511
const app = new Root();
512512
expect(app.get('foo')).to.equal('original foo [updated]');
513513
});
514+
515+
it('can access and update providers from lifecycle callback defined while instantiating', function () {
516+
const Root = createApp({
517+
name: 'RootApp',
518+
providers: [
519+
{
520+
name: 'foo',
521+
useValue: 'original foo',
522+
},
523+
],
524+
initialize() {
525+
const foo = this.get('foo');
526+
this.container.register({
527+
name: 'foo',
528+
useValue: `${foo} [updatedFromCreateApp]`,
529+
});
530+
}
531+
});
532+
533+
const app = new Root({
534+
initialize() {
535+
const foo = this.get('foo');
536+
this.container.register({
537+
name: 'foo',
538+
useValue: `${foo} [updatedFromInstantiation]`,
539+
});
540+
}
541+
});
542+
543+
expect(app.get('foo')).to.equal('original foo [updatedFromCreateApp] [updatedFromInstantiation]');
544+
});
514545
});

packages/frint/src/createApp.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ function mergeOptions(createAppOptions, constructorOptions) {
1616
typeof constructorOptions[cbName] === 'function'
1717
) {
1818
mergedOptions[cbName] = function lifecycleCb() {
19-
createAppOptions[cbName]();
20-
constructorOptions[cbName]();
19+
createAppOptions[cbName].call(this);
20+
constructorOptions[cbName].call(this);
2121
};
2222
}
2323
});

0 commit comments

Comments
 (0)