Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

session.setup must be guarded to avoid double calling #2951

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 8 additions & 6 deletions packages/ember-simple-auth/src/services/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,13 @@ export default class SessionService<Data = DefaultDataShape> extends Service {
@public
*/
setup() {
this._setupIsCalled = true;
this._setupHandlers();

return this.session.restore().catch(() => {
// If it raises an error then it means that restore didn't find any restorable state.
});
if(!this._setupIsCalled) {
this._setupIsCalled = true;
this._setupHandlers();

return this.session.restore().catch(() => {
// If it raises an error then it means that restore didn't find any restorable state.
});
}
Comment on lines +337 to +344
Copy link
Collaborator

@BobrImperator BobrImperator Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR 👍
It seems like one alternative could be to use an instance-initializer instead (thinking out loud).

Could you add a test asserting that _setupHandlers can be called only once?

We might want to make sure that the setupHandlers runs only once while restore continues to be called.

Suggested change
if(!this._setupIsCalled) {
this._setupIsCalled = true;
this._setupHandlers();
return this.session.restore().catch(() => {
// If it raises an error then it means that restore didn't find any restorable state.
});
}
if (!this._setupIsCalled) {
this._setupIsCalled = true;
this._setupHandlers();
}
return this.session.restore().catch(() => {
// If it raises an error then it means that restore didn't find any restorable state.
});

}
}