Skip to content

Commit

Permalink
fix(angular): stop auth actor when destroying AuthenticatorService (#…
Browse files Browse the repository at this point in the history
…6333)

* fix(angular): stop interpreter when destroying AuthenticatorService

* chore: add changeset

* test(angular): in AuthenticatorService, ensure underlying auth actor is closed on destruction
  • Loading branch information
QuentinFchx authored Feb 7, 2025
1 parent b682921 commit 10b7c9e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/chilled-moles-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@aws-amplify/ui-angular": patch
---

fix(angular): stop auth actor when destroying `AuthenticatorService`
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import * as UIModule from '@aws-amplify/ui';
import * as XState from 'xstate';

// mock state machine service
// based on https://github.com/statelyai/xstate/blob/main/packages/core/src/interpreter.ts
// based on https://github.com/statelyai/xstate/blob/v4/packages/core/src/interpreter.ts
class MockAuthService {
initialized = false;
private listeners: (() => void)[] = [];

subscribe(callback: () => void): { unsubscribe: () => void } {
Expand All @@ -15,6 +16,12 @@ class MockAuthService {
}

start(): this {
this.initialized = true;
return this;
}

stop(): this {
this.initialized = false;
return this;
}

Expand Down Expand Up @@ -58,4 +65,20 @@ describe('AuthenticatorService', () => {

subscription.unsubscribe();
});

it('should stop actor on destruction', () => {
expect(
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
((authService as any)._authService as ReturnType<typeof XState.interpret>)
.initialized
).toBeTruthy();

authService.ngOnDestroy();

expect(
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
((authService as any)._authService as ReturnType<typeof XState.interpret>)
.initialized
).toBeFalsy();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ export class AuthenticatorService implements OnDestroy {
ngOnDestroy(): void {
if (this._machineSubscription) this._machineSubscription.unsubscribe();
if (this._unsubscribeHub) this._unsubscribeHub();
this._authService.stop();
}

/** @deprecated For internal use only */
Expand Down

0 comments on commit 10b7c9e

Please sign in to comment.