-
Notifications
You must be signed in to change notification settings - Fork 16
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
Refactor StatefulAuthProvider for static initialization and improved session management #1861
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { Subject, Subscription } from 'rxjs' | ||
import { debounceTime, distinctUntilChanged } from 'rxjs/operators' | ||
import { authentication, AuthenticationSessionsChangeEvent, Disposable } from 'vscode' | ||
|
||
export default class AuthSessionChangeHandler implements Disposable { | ||
static #instance: AuthSessionChangeHandler | null = null | ||
|
||
#disposables: Disposable[] = [] | ||
#eventSubject: Subject<AuthenticationSessionsChangeEvent> | ||
#subscriptions: Subscription[] = [] | ||
#listeners: ((event: AuthenticationSessionsChangeEvent) => void)[] = [] | ||
|
||
private constructor(private debounceTimeMs: number = 100) { | ||
this.#eventSubject = new Subject<AuthenticationSessionsChangeEvent>() | ||
this.#subscriptions.push( | ||
this.#eventSubject | ||
.pipe(distinctUntilChanged(this.eventComparer), debounceTime(this.debounceTimeMs)) | ||
.subscribe((event) => { | ||
this.notifyListeners(event) | ||
}), | ||
) | ||
|
||
this.#disposables.push( | ||
authentication.onDidChangeSessions((e) => { | ||
this.#eventSubject.next(e) | ||
}), | ||
) | ||
} | ||
|
||
public static get instance(): AuthSessionChangeHandler { | ||
if (!this.#instance) { | ||
this.#instance = new AuthSessionChangeHandler() | ||
} | ||
|
||
return this.#instance | ||
} | ||
|
||
public addListener(listener: (event: AuthenticationSessionsChangeEvent) => void): void { | ||
this.#listeners.push(listener) | ||
} | ||
|
||
public removeListener(listener: (event: AuthenticationSessionsChangeEvent) => void): void { | ||
this.#listeners = this.#listeners.filter((l) => l !== listener) | ||
} | ||
|
||
private notifyListeners(event: AuthenticationSessionsChangeEvent): void { | ||
for (const listener of this.#listeners) { | ||
try { | ||
listener(event) | ||
} catch (err) { | ||
console.error('Error in listener:', err) | ||
} | ||
} | ||
} | ||
|
||
private eventComparer( | ||
previous: AuthenticationSessionsChangeEvent, | ||
current: AuthenticationSessionsChangeEvent, | ||
): boolean { | ||
return ( | ||
previous.provider.id === current.provider.id && | ||
JSON.stringify(previous) === JSON.stringify(current) | ||
) | ||
} | ||
|
||
public async dispose() { | ||
this.#disposables.forEach((d) => d.dispose()) | ||
this.#subscriptions = [] | ||
this.#eventSubject.complete() | ||
this.#listeners = [] | ||
|
||
AuthSessionChangeHandler.#instance = null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pastuxso don't we need to
await
this here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! I realized we really needed to await this, as not doing so could lead to race conditions or unexpected behavior. To address this, I refactored the ensureSession function to properly return the session and updated the flow to await it where necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me know if you have further suggestions!