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

feat(angular): add support for onpush change detection strategy #4997

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

esauerbo
Copy link
Contributor

@esauerbo esauerbo commented Feb 9, 2024

Description of changes

Changes authenticator component to use OnPush strategy, which means it can be consumed by components that use either OnPush or Default strategy.

e.g.

@Component({
  selector: 'sign-in-with-email',
  template: '<amplify-authenticator></amplify-authenticator>'
})

works the same as

@Component({
  selector: 'sign-in-with-email',
  template: '<amplify-authenticator></amplify-authenticator>'
  changeDetection: ChangeDetectionStrategy.OnPush
})

Because authenticator now uses OnPush, we run change detection manually (changeDetector.detectChanges()) whenever the authService updates. This is achieved by creating an observable from the auth service

this._authStateObservable$ = from(this._authService);

using rxjs, which can then be natively subscribed to emit events when authService updates.

Issue #, if available

fixes #4899

Description of how you validated changes

Checklist

  • Have read the Pull Request Guidelines
  • PR description included
  • Relevant documentation is changed or added (and PR referenced)
  • yarn test passes and tests are updated/added
  • No side effects or sideEffects field updated

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@esauerbo esauerbo requested a review from a team as a code owner February 9, 2024 23:39
Copy link

changeset-bot bot commented Feb 9, 2024

🦋 Changeset detected

Latest commit: 5ff9e7c

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR


it('callback is called when interpreted authservice changes', () => {
Copy link
Contributor Author

@esauerbo esauerbo Feb 9, 2024

Choose a reason for hiding this comment

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

We no longer return the facade from the subscribe function, instead it just invokes a callback so testing that the callback gets called.

@@ -64,6 +69,7 @@ export class SetupTotpComponent implements OnInit {

logger.info('totp code was generated:', totpCode);
this.qrCodeSource = await QRCode.toDataURL(totpCode);
this.qrCodeSource$.next(this.qrCodeSource);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

BehaviorSubject is a type of observable that takes an initial value and emits current value to observers. Using it with the async pipe like"qrCodeSource$ | async" will automatically subscribe to new values and mark component for check when qrCodeSource changes, and will automatically unsubscribe when component is destroyed. It's a cleaner way of subscribing to an observable and then running change detection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FR: Add support for Angular Change detection and "OnPush" mode
1 participant