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

ngrx-lazy

v15.0.1

Published

Dead simple data lazy loading for @ngrx state management

Downloads

19

Readme

ngrx-lazy

Dead simple data lazy loading for @ngrx state management.

Inspired by the early work of @Synthx

Get started

npm install ngrx-lazy --save

How to use?

To fully understand how to use this library, we'll go through an example of a project that list brands on an e-commerce website.

Lazy state

A lazy state is made of several properties:

type Lazy<T> = {
  fetched: boolean;
  pending: boolean;
  error?: any;
  data: T;
};

You can then use the createLazy function to initialize your feature state.

import { createLazy, Lazy } from "ngrx-lazy";
import { Brand } from "./models";

export interface BrandsState {
  all: Lazy<Brand[]>;
  popular: Lazy<Brand[]>;
}

export const initialBrandsState: BrandsState = {
  all: createLazy<Brand[]>([]),
  popular: createLazy<Brand[]>([]),
};

In this example, we create two lazy loadable array of brands inside our brands feature state.

Lazy reducers

Once you have created your state, you can handle the reducers based on a simple data fetching logic.

import { createReducer, on } from "@ngrx/store";
import * as BrandsActions from "./brands.actions";
import { initialBrandsState } from "./brands.state";

export const brandsReducer = createReducer(
  initialBrandsState,

  on(BrandsActions.loadAll, (state) => ({
    ...state,
    all: { ...state.all, pending: true },
  })),
  on(BrandsActions.loadAllSuccess, (state, { brands }) => ({
    ...state,
    all: { data: brands, fetched: true, pending: false },
  })),
  on(BrandsActions.loadAllError, (state, { error }) => ({
    ...state,
    all: { ...state.all, pending: false, error },
  })),

  on(BrandsActions.loadPopular, (state) => ({
    ...state,
    popular: { ...state.popular, pending: true },
  })),
  on(BrandsActions.loadPopularSuccess, (state, { popularBrands }) => ({
    ...state,
    popular: { data: popularBrands, fetched: true, pending: false },
  })),
  on(BrandsActions.loadPopularError, (state, { error }) => ({
    ...state,
    popular: { ...state.popular, pending: false, error },
  }))
);

Lazy selectors

You can create basic selectors using the existing createSelector from @ngrx.

import { createSelector } from "@ngrx/store";
import { getEntityState } from "../selectors/entity.selectors";

const selectBrandsState = createSelector(
  getEntityState,
  (state) => state.brands
);

const lazyAll = createSelector(selectBrandsState, (state) => state.all);

const selectBrands = createSelector(lazyAll, (lazy) => lazy.data);
const selectBrandBySlug = (slug: string) =>
  createSelector(selectBrands, (brands) => brands.find((b) => b.slug === slug));

export const AllBrandsSelectors = {
  selectLazy: lazyAll,
  selectBrands,
  selectBrandBySlug,
};

const lazyPopular = createSelector(selectBrandsState, (state) => state.popular);

const selectPopularBrands = createSelector(lazyPopular, (lazy) => lazy.data);

export const PopularBrandsSelectors = {
  selectLazy: lazyPopular,
  selectPopularBrands,
};

In order to make this library shine, you absolutely need a Facade on top of the selector. Here is an example what it can looks like:

import { Injectable } from "@angular/core";
import { Store } from "@ngrx/store";
import { selectLazy } from "ngrx-lazy";
import { RootState } from "../models";
import * as BrandsActions from "./brands.actions";
import { AllBrandsSelectors, PopularBrandsSelectors } from "./brands.selectors";

@Injectable()
export class BrandsSelectors {
  constructor(private readonly store: Store) {}

  lazyAll$ = selectLazy({
    store: this.store,
    selector: AllBrandsSelectors.lazyAll,
    loadAction: BrandsActions.loadAll(),
  });

  brands$ = this.lazyAll$.select(AllBrandsSelectors.getBrands);
  brandBySlug$ = (slug: string) =>
    this.lazyAll$.select(AllBrandsSelectors.getBrandBySlug(slug));
  brandById$ = (id: number) =>
    this.lazyAll$.select(AllBrandsSelectors.getBrandById(id));

  lazyPopular$ = selectLazy({
    store: this.store,
    selector: PopularBrandsSelectors.lazyPopular,
    loadAction: BrandsActions.loadPopular(),
  });

  popularBrands$ = this.lazyPopular$.select(
    PopularBrandsSelectors.getPopularBrands
  );
}

Now, when you call any property on this selector service, it will automatically trigger the data loading related to state of the lazy state.

Of course, you'd need to implement Effects to execute data fetching!

RxJS operators

firstNotPending

The operator firstNotPending takes a lazy selector and dispatch the first value of the selector once data fetching is done.

const allBrands$: Observable<Brand[]> = this.brandsSelectors.lazyAll$.pipe(firstNotPending()):

skipUntilLazyLoaded

The operator skipUntilLazyLoaded takes a selector and dispatch its values only once a parent selector is fully lazy loaded (once data fetching is done).

const brand$: Observable<Brand> = this.brandsSelectors.brandById$(id).pipe(
    skipUntilLazyLoaded(this.brandsSelectors.lazyAll$),
):