Skip to content
Utility functions for NgRx
TypeScript JavaScript
Branch: master
Clone or download
timdeschryver fix: use the NgRx 8.5.2 types (#10)
BREAKING CHANGE:

This version is compatible to NgRx 8.5.2 and up
Latest commit 98f49f0 Dec 11, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
src
tests
.all-contributorsrc docs: add maartentibau as a contributor (#7) Aug 24, 2019
.eslintignore
.eslintrc.js
.gitattributes
.gitignore
.npmrc
.prettierignore
.prettierrc
README.md
jest.config.js
package-lock.json
package.json
tsconfig.json
tsconfig.spec.json
tslint.json

README.md

NgRx-etc

All Contributors

mutableOn

Without the mutableOn function our entityReducer would look something like this.

const entityReducer = createReducer<{ entities: Record<number, { id: number; name: string }> }>(
  {
    entities: {},
  },
  on(create, (state, { type, ...entity }) => ({ 
    ...state, 
    entities: { ...state.entities, [entity.id]: entity }
  }),
  on(update, (state, { id, newName }) => {
    const entity = state.entities[id]
  
    if (entity) {
      return { 
        ...state, 
        entities: { 
          ...state.entities, 
          [entity.id]: { ...entity, name: newName } 
        }
      }
    }
    
    return state;
  },
  on(remove, (state, { id }) => {
    const { [id]: removedEntity, ...rest } = state.entities;
    
    return { ...state, entities: rest };
  }),
)

With the mutableOn function we are able to directly perform state mutations in reducers with less noise, and more concise code.

const entityReducer = createReducer<{ entities: Record<number, { id: number; name: string }> }>(
  {
    entities: {},
  },
  mutableOn(create, (state, { type, ...entity }) => {
    state.entities[entity.id] = entity
  }),
  mutableOn(update, (state, { id, newName }) => {
    const entity = state.entities[id]
    if (entity) {
      entity.name = newName
    }
  }),
  mutableOn(remove, (state, { id }) => {
    delete state.entities[id]
  }),
)

Contributors

Thanks goes to these wonderful people (emoji key):

Tim Deschryver
Tim Deschryver

💻 ⚠️
Maarten Tibau
Maarten Tibau

📖

This project follows the all-contributors specification. Contributions of any kind welcome!

You can’t perform that action at this time.