@mikelgo/ngx-query-stream
TypeScript icon, indicating that this package has built-in type declarations

13.0.0 • Public • Published

ngx-query-stream

This package provides a nice way to feetch data from an endpoint and expose certain states like a isLoading and isRefreshing state.

While refreshing the current values are kept and updated once the refresh is done.

Usage

import { queryStream$ } from 'ngx-query-stream';
@Component({
  template: ` <button (click)="refreshCommand$.next(null)">refresh</button>
    <div *ngIf="query$ | async as req">
      <div>isloading {{req!.isLoading!}} isRefreshing {{req!.isRefreshing}} </div>
      <div>values {{req?.value | json}}</div>
    </div>

  `,
  standalone: true,
  imports: [CommonModule],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class RxjsCacheComponent implements OnInit {
  private http: HttpClient = inject(HttpClient);
  refreshCommand$ = new Subject();

  query$: Observable<Query<any>> =  query$(this.fetch(10), this.refreshCommand$)

  fetch(count: number) {

    return this.http.get(('https://jsonplaceholder.typicode.com/posts')).pipe(
      delay(500),
      map(x => (x as any[]).slice(0, (Math.random() > 0.5 ? 10 : 20))),
    )

  }
}

Note: the example above passes a refreshCommand$, however it is optional but recommended to use.

/@mikelgo/ngx-query-stream/

    Package Sidebar

    Install

    npm i @mikelgo/ngx-query-stream

    Weekly Downloads

    0

    Version

    13.0.0

    License

    none

    Unpacked Size

    16.7 kB

    Total Files

    11

    Last publish

    Collaborators

    • mikelgo