Skip to content

Commit

Permalink
fix(angular,vue): Fix unsubscription timing issues in Vue/Angular (#1592
Browse files Browse the repository at this point in the history
)

* Fix unscription timing issues in Vue/Angular

* Create healthy-zoos-ring.md
  • Loading branch information
wlee221 authored Mar 25, 2022
1 parent 6d3981c commit fc0acac
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changeset/healthy-zoos-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@aws-amplify/ui-vue": patch
"@aws-amplify/ui-angular": patch
---

Fix unscription timing issues in Vue/Angular
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Component,
ContentChildren,
Input,
OnDestroy,
OnInit,
QueryList,
TemplateRef,
Expand All @@ -23,7 +24,9 @@ import { AuthenticatorService } from '../../../../services/authenticator.service
providers: [CustomComponentsService], // make sure custom components are scoped to this authenticator only
encapsulation: ViewEncapsulation.None,
})
export class AuthenticatorComponent implements OnInit, AfterContentInit {
export class AuthenticatorComponent
implements OnInit, AfterContentInit, OnDestroy
{
@Input() formFields: AuthenticatorMachineOptions['formFields'];
@Input() initialState: AuthenticatorMachineOptions['initialState'];
@Input() loginMechanisms: AuthenticatorMachineOptions['loginMechanisms'];
Expand All @@ -41,6 +44,7 @@ export class AuthenticatorComponent implements OnInit, AfterContentInit {
public signUpTitle = translate('Create Account');

private hasInitialized = false;
private unsubscribeMachine: () => void;

constructor(
private authenticator: AuthenticatorService,
Expand All @@ -57,8 +61,11 @@ export class AuthenticatorComponent implements OnInit, AfterContentInit {
formFields,
} = this;

// send INIT event once machine is at 'setup' state
const { unsubscribe } = this.authenticator.subscribe(() => {
/**
* Subscribes to state machine changes and sends INIT event
* once machine reaches 'setup' state.
*/
this.unsubscribeMachine = this.authenticator.subscribe(() => {
const { route } = this.authenticator;
if (!this.hasInitialized && route === 'setup') {
this.authenticator.send({
Expand All @@ -74,9 +81,8 @@ export class AuthenticatorComponent implements OnInit, AfterContentInit {
});

this.hasInitialized = true;
unsubscribe();
}
});
}).unsubscribe;

/**
* handling translations after content init, because authenticator and its
Expand All @@ -95,6 +101,10 @@ export class AuthenticatorComponent implements OnInit, AfterContentInit {
);
}

ngOnDestroy(): void {
if (this.unsubscribeMachine) this.unsubscribeMachine();
}

/**
* Class Functions
*/
Expand Down
13 changes: 9 additions & 4 deletions packages/vue/src/components/authenticator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,19 @@ const emit = defineEmits([
const machine = createAuthenticatorMachine();
const service = useInterpret(machine);
let unsubscribeHub: ReturnType<typeof listenToAuthHub>;
let unsubscribeHub: () => void;
let unsubscribeMachine: () => void;
const { state, send } = useActor(service);
useAuth(service);
const hasInitialized = ref(false);
const { unsubscribe } = service.subscribe((newState) => {
/**
* Subscribes to state machine changes and sends INIT event
* once machine reaches 'setup' state.
*/
unsubscribeMachine = service.subscribe((newState) => {
if (newState.matches('setup') && !hasInitialized.value) {
send({
type: 'INIT',
Expand All @@ -96,16 +101,16 @@ const { unsubscribe } = service.subscribe((newState) => {
},
});
hasInitialized.value = true;
unsubscribe();
}
});
}).unsubscribe;
onMounted(() => {
unsubscribeHub = listenToAuthHub(send);
});
onUnmounted(() => {
if (unsubscribeHub) unsubscribeHub();
if (unsubscribeMachine) unsubscribeMachine();
});
const actorState = computed(() => getActorState(state.value));
Expand Down

0 comments on commit fc0acac

Please sign in to comment.