GithubHelp home page GithubHelp logo

lmfinney / ngrx-enums Goto Github PK

View Code? Open in Web Editor NEW
12.0 2.0 2.0 519 KB

A small library that provides the basis for using ts-enums for @ngrx actions and reducers.

License: MIT License

JavaScript 7.98% TypeScript 92.02%
ngrx ngrx-store enums

ngrx-enums's Introduction

ngrx-enums

A small library that provides the base classes for for implementing @ngrx actions and reducers with ts-enums

Motivation

@ngrx/store is a very powerful utility for managing the state of Angular apps, but some developers have criticized the example app for containing too much boilerplate (particularly in the action classes) and for having large switch statements in the reducers. ngrx-example-app-enums is a fork of the example app that uses ts-enums to encapsulate the actions and reducers, thereby reducing boilerplate and hiding the switch statement from view.

This library builds on ts-enums to provide just the base files that ngrx-example-app-enums uses so that they can be used separately in your apps.

The basics

Install:

npm install ngrx-enums

Example

These examples are included in a test.

Actions Defined via Enums

import {ActionEnum, ActionEnumValue} from 'ngrx-enums';

class LayoutAction<T> extends ActionEnumValue<T> {
  constructor(name: string) {
    super(name);
  }
}

class LayoutActionEnumType extends ActionEnum<LayoutAction<any>> {
  OPEN_SIDENAV = new LayoutAction<void>('[Layout] Open Sidenav');
  OPEN_SIDENAV_ALSO = new LayoutAction<void>('[Layout] Open Sidenav Also');
  SET_SIDENAV = new LayoutAction<boolean>('[Layout] Set Sidenav');

  constructor() {
    super();
    this.initEnum('layoutActions');
  }
}

export const LayoutActionEnum: LayoutActionEnumType = new LayoutActionEnumType();

Reducer Defined via matches() Methods

import {simplePropertyReducer, TypedAction} from 'ngrx-enums';

interface State {
  showSidenav: boolean;
}

const initialState: State = {
  showSidenav: false
};

function layoutReducer(state = initialState, action: TypedAction<any>): State {
  if (LayoutActionEnum.matches(action,
      LayoutActionEnum.OPEN_SIDENAV, LayoutActionEnum.OPEN_SIDENAV_ALSO)) {
    // a simple reducer that always sets the same value
    // this shows how to respond to multiple actions
    // (fall-through in switch)
    return {...state, showSidenav: true};
  } else if (LayoutActionEnum.SET_SIDENAV.matches(action)) {
    // a reducer that accepts a value and copies it as a property into state
    return simplePropertyReducer<State, boolean>('showSidenav')(state, action);
  } else {
    return state;
  }
}

Reducer Defined via Enums

import {
  ActionEnumValue,
  ReducerEnum,
  ReducerEnumValue,
  ReducerFunction,
  simplePropertyReducer
} from 'ngrx-enums';

interface State {
  showSidenav: boolean;
}

const initialState: State = {
  showSidenav: false
};

class LayoutReducer<T> extends ReducerEnumValue<State, T> {
  constructor(action: ActionEnumValue<T> | ActionEnumValue<T>[],
              reduce: ReducerFunction<State, T>) {
    super(action, reduce);
  }
}

class LayoutReducerEnumType extends ReducerEnum<LayoutReducer<any>, State> {
  // a simple reducer that always sets the same value
  // this shows how to respond to multiple actions
  // (fall-through in switch)
  OPEN_SIDENAV = new LayoutReducer<void>(
    [LayoutActionEnum.OPEN_SIDENAV, LayoutActionEnum.OPEN_SIDENAV_ALSO],
      (state: State) => ({showSidenav: true}));
  // a reducer that accepts a value and copies it as a property into state
  SET_SIDENAV = new LayoutReducer<boolean>(
    LayoutActionEnum.SET_SIDENAV,
    simplePropertyReducer<State, boolean>('showSidenav')
  );

  constructor() {
    super(initialState);
    this.initEnum('layoutReducers');
  }
}

export const LayoutReducerEnum: LayoutReducerEnumType = new LayoutReducerEnumType();

More information

  • Some users get an error that looks something like this when compiling:

    ERROR in Error encountered resolving symbol values statically. Calling function 'ɵmakeDecorator', function calls are not supported. Consider replacing the function or lambda with a reference to an exported function, resolving symbol Injectable...

    If you get this error, you might be able to fix the problem by adding a path of "@angular/*": ["../node_modules/@angular/*"] to your tsconfig.app.json file (more information).

ngrx-enums's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ngrx-enums's Issues

Use switch statement with type checking for ActionEnums

Is it still possible to use the switch statement instead of the complicated ReducerEnum classes? How would I check the payload type of the actions?

The switch statement has the possibility to fall through several actions, which the ReducerEnum cannot do.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.