ngrx-enums
A small library that provides the base classes for for implementing @ngrx actions and reducers with ts-enums.
Last updated 2 years ago by lmfinney .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install ngrx-enums 
SYNC missed versions from official npm registry.

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).

Current Tags

  • 0.0.11                                ...           latest (2 years ago)

11 Versions

  • 0.0.11                                ...           2 years ago
  • 0.0.10                                ...           2 years ago
  • 0.0.9                                ...           2 years ago
  • 0.0.8                                ...           2 years ago
  • 0.0.7                                ...           2 years ago
  • 0.0.6                                ...           2 years ago
  • 0.0.5                                ...           2 years ago
  • 0.0.4                                ...           2 years ago
  • 0.0.3                                ...           2 years ago
  • 0.0.2                                ...           2 years ago
  • 0.0.1                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (4)
Dev Dependencies (30)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |