npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ng-suspense

v0.0.8

Published

Set of directives for work with async data sources.

Downloads

40

Readme

ng-suspense

Set of directives for work with async data sources.

Gives ability to handle loading, error and resolved states of Observbale or Promise.

Instalation

npm install ng-suspense --save

Usage

Import the ng-suspnse module

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { NgSuspenseModule } from 'ng-suspense';
import { AppComponent } from './app.component';



@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    NgSuspenseModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Use directive inside component template to provide different templates for different states

<h1>Posts</h1>
<div [ngSuspense]="posts$">
  <div *ngSuspenseError="let error" class="error">Unable to load posts</div>
  <div *ngSuspensePlaceholder="500">Loading...</div>
  <ng-container *ngSuspenseSuccess="let posts">
    <div *ngFor="let post of posts">{{post.title}}</div>
  </ng-container>
</div>

NgSuspense

A structural directive which adds or removes templates when provided Observable|Promise changes its state.

Selectors

[ngSuspense]

Properties

| Property | Descritpion | | ------------------------------------------------------ | ----------- | | @Input() ngSuspense: Observable<any>|Promise<any> | |

Description

The [ngSuspense] directive on a container specifies an Observable or Promise to watch on. Whenever state of provided object changes сoresponding template will be rendered.

  • If object changes state to error a view with the ngSuspenseError directive is rendered
  • If object emmits any value a view with the ngSuspenseSuccess directive is rendered
  • If no value emmited a view with the ngSuspensePlaceholder directive is rendered

NgSuspensePlaceholder

Provides a template which should be rendered while Observable or Promise object of enclosing NgSuspense have not produced any value.

Properties

| Property | Description | | ------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- | | @Input() ngSuspensePlaceholder?: number | TemplateRef<any> | If only number provided this number will be used as delay (ms). If TemplateRef provided, this template will be used instead of vew | | @Input() ngSuspensePlaceholderDelay?: number | Delay in ms | | @Input() ngSuspensePlaceholderTemplate?: TemplateRef<any> | Template to be used |

Description

Withing a [ngSuspense] conainer ngSuspensePlaceholder directive provides a template and delay in milliseconds when this template will be showed.

NgSuspenseError

Provides a template which should be rendered when Observable|Promise in enclosing [ngSuspense] change its state to error.

Properties

| Property | Description | | --------------------------------------------- | ------------------- | | @Input() ngSuspenseError?: TemplateRef<any> | Altenative template |

Using non-inlined template

Usually error template inlined with ngSuspenseError directive, but it can be changed using binding (just like then and else in ngIf). Because it is binded the template reference can be changed at runtime.

Storing error object in a value

A common pattern is to show different templates for different error types. NgSuspenseError directive provides { $imlicit: error } as a context for rendered template.

<ng-container *ngSuspenseError="let error">
  <ng-container [ngSwitch]="error.status">
    <div *ngSwitchCase="404">Post not found</div>
    <div *ngSwitchCase="401">Not authenticated</div>
    <div *ngSwitchВefault>Uncnown error</div>
  </ng-container>
</ng-container>

NgSuspenseSuccess

Provides a template for resolved value

Properties

| Property | Description | | ----------------------------------------------- | ------------------- | | @Input() ngSuspenseSuccess?: TemplateRef<any> | Altenative template |

Using non-inline templates

Sometimes you need to provide template in runtime. In this case you can provide it as an inpunt for ngSuspenseSuccess

Store result in a value

ngSuspenseSuccess provides { $implicit: responseData } as a context for the templatte.

<ng-container [ngSuspense]="posts$">
  <div *ngSuspenseSuccess="let posts"><div *ngFor="let post of posts">{{post.title}}</div></div>
</ng-container>