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(component): add suspense template input to LetDirective #3377

Merged

Conversation

markostanimirovic
Copy link
Member

@markostanimirovic markostanimirovic commented Apr 13, 2022

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Other... Please describe:

What is the current behavior?

Closes #3340

What is the new behavior?

Added ability to pass the suspense template to LetDirective:

<p *ngrxLet="obs$ as o; suspenseTpl: loading">
  {{ o }}
</p>
<ng-template #loading>Loading...</ng-template>

A suspense template will be displayed when passed observable is in a suspense state. If the suspense template is not passed, the behavior will remain the same:

  • The main template will be rendered only when the first observable emits next/error/complete event.
  • If a new observable is passed, the main template will be displayed in a suspense state as well, but the value will be undefined. However, there is new view context property $suspense that can be used in this case:
@Component({
  template: `
    <button (click)="onSwitch()">Switch Observable</button>
    <ng-container *ngrxLet="obs$ as o; $suspense as s">
      <p *ngIf="!s">{{ o }}</p>
      <p *ngIf="s">Loading...</p>
    </ng-container>
  `,
})
export class DemoComponent {
  obs$ = of(true);

  onSwitch(): void {
    this.obs$ = of(false).pipe(delay(1000));
  }
}

Does this PR introduce a breaking change?

[ ] Yes
[x] No

@netlify
Copy link

netlify bot commented Apr 13, 2022

Deploy Preview for ngrx-io ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit fd57c30
🔍 Latest deploy log https://app.netlify.com/sites/ngrx-io/deploys/625758fb7239b800082d4a09
😎 Deploy Preview https://deploy-preview-3377--ngrx-io.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

Comment on lines +173 to +177
static ngTemplateContextGuard<U>(
dir: LetDirective<U>,
ctx: unknown | null | undefined
): ctx is LetViewContext<U> {
return true;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

moved because of the @typescript-eslint/member-ordering rule

Comment on lines -33 to -54
* The `*ngrxLet` directive serves a convenient way of binding observables to a view context (a dom element scope).
* It also helps with several internal processing under the hood.
*
* The current way of binding an observable to the view looks like that:
* ```html
* <ng-container *ngIf="observableNumber$ | async as n">
* <app-number [number]="n">
* </app-number>
* <app-number-special [number]="n">
* </app-number-special>
* </ng-container>
* ```
*
* The problem is `*ngIf` is also interfering with rendering and in case of a `0` the component would be hidden
*
* Included Features:
* - binding is always present. (`*ngIf="truthy$ | async"`)
* - it takes away the multiple usages of the `async` or `ngrxPush` pipe
* - a unified/structured way of handling null and undefined
* - triggers change-detection differently if `zone.js` is present or not (`ChangeDetectorRef.detectChanges` or `ChangeDetectorRef.markForCheck`)
* - triggers change-detection differently if ViewEngine or Ivy is present (`ChangeDetectorRef.detectChanges` or `ɵdetectChanges`)
* - distinct same values in a row (distinctUntilChanged operator)
Copy link
Member Author

Choose a reason for hiding this comment

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

Removed duplicated content that is also in the let.md file. I have left a short description and examples here.

@markostanimirovic markostanimirovic force-pushed the feat/component/suspense-template branch from fd57c30 to 4a47bf5 Compare May 13, 2022 11:55
@brandonroberts brandonroberts merged commit 345ee53 into ngrx:master May 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@ngrx/component: Add suspense template as input of LetDirective
3 participants