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-handlers

v12.0.1

Published

NgRx Plugin for Generating Actions and Reducer Based on the Handler Map

Downloads

182

Readme

🕹️ NgRx Handlers

MIT License NPM Build Status Coverage Status Downloads

NgRx Plugin for Generating Actions and Reducer Based on the Handler Map

☝️ Why to use NgRx Handlers?

  • Because it's boring to write action types manually
  • Because you don't need to write too much code for simple functionality
  • Because barrels and import * as Actions are no longer needed
  • Because unlike other similar plugins, NgRx Handlers keep NgRx look and feel

🚀 Getting Started

🔧 Installation

NPM: npm install ngrx-handlers

Yarn: yarn add ngrx-handlers

⚡ Usage

NgRx Actions and Reducer

// books-page.actions.ts
export const enter = createAction('[Books Page] Enter');
export const search = createAction('[Books Page] Search', props<{ searchTerm: string }>());
export const selectBook = createAction(
  '[Books Page] Select Book',
  props<{ selectedBookId: number }>(),
);

// books-api.actions.ts
export const fetchBooksSuccess = createAction(
  '[Books API] Fetch Books Success',
  props<{ books: Book[] }>(),
);
export const fetchBooksError = createAction('[Books API] Fetch Books Error');

// books.reducer.ts
import * as BooksPageActions from './actions/books-page.actions';
import * as BooksApiActions from './actions/books-api.actions';

export const reducer = createReducer(
  initBooksState,
  on(BooksPageActions.enter, state => ({ ...state, searchTerm: '', loading: true })),
  on(BooksPageActions.search, (state, { searchTerm }) => ({ ...state, searchTerm, loading: true })),
  on(BooksPageActions.selectBook, (state, { selectedBookId }) => ({
    ...state,
    selectedBookId,
  })),
  on(BooksApiActions.fetchBooksSuccess, (state, { books }) => ({
    ...state,
    books,
    loading: false,
  })),
  on(BooksApiActions.fetchBooksError, state => ({ ...state, books: [], loading: false })),
);

NgRx Handlers

// books-page.handlers.ts
export const [BooksPageActions, booksPageReducer] = combineHandlers(initBooksState, 'booksPage', {
  enter: state => ({ ...state, searchTerm: '', loading: true }),
  search: (state, { searchTerm }: { searchTerm: string }) => ({
    ...state,
    searchTerm,
    loading: true,
  }),
  selectBook: (state, { selectedBookId }: { selectedBookId: number }) => ({
    ...state,
    selectedBookId,
  }),
});

// books-api.handlers.ts
export const [BooksApiActions, booksApiReducer] = combineHandlers(initBooksState, 'booksApi', {
  fetchBooksSuccess: (state, { books }: { books: Book[] }) => ({
    ...state,
    books,
    loading: false,
  }),
  fetchBooksError: state => ({ ...state, books: [], loading: false }),
});

// books.reducer.ts
export const booksReducer = mergeReducers(booksPageReducer, booksApiReducer);

combineHandlers function returns strongly typed action creators and a reducer with O(1) efficiency.

Another great thing about combineHandlers is that you don't have to manually write action types. For example, when the source is booksPage and the event is search, it will generate the action creator with type [booksPage] search.

Every reducer generated by combineHandlers function can be used independently via StoreModule. However, if you have multiple sources for a single state slice, there is mergeReducers function that will merge provided reducers into one.

In case you need to define actions without changing the state, this plugin provides plain and withPayload functions.

export const [BooksPageActions, booksPageReducer] = combineHandlers(initBooksState, 'booksPage', {
  showCreateBookDialog: plain(),
  createBook: withPayload<{ book: Book }>(),
});

See the sample project here.

✊ Show Your Support

Give a ⭐ if you like NgRx Handlers 🙂

🤝 Contribute

Contributions are always welcome!

💡 Inspiration

This project is inspired by Juliette.

📝 License

This project is MIT licensed.

Copyright © 2020-2021 Marko Stanimirović