Coder Social home page Coder Social logo

ngrx-etc's Introduction

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]
  }),
)

mutableReducer

For when you want to go all-in! Does work with the NgRx on method, as well as the mutableOn method. The only difference is that it's needed to return the state with the on method.

const entityReducer = createMutableReducer<{ entities: Record<number, { id: number; name: string }> }>(
  {
    entities: {},
  },
  on(create, (state, { type, ...entity }) => {
    state.entities[entity.id] = entity

    // explicit return state with `on`
    return state
  }),
  on(update, (state, { id, newName }) => {
    const entity = state.entities[id]
    if (entity) {
      entity.name = newName
    }

    // explicit return state with `on`
    return state
  }),
  mutableOn(remove, (state, { id }) => {
    delete state.entities[id]
    // don't have to return state with `mutableOn`
  }),
)

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Tim Deschryver

πŸ’» ⚠️

Maarten Tibau

πŸ“–

xiansheng lu

πŸ“–

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

ngrx-etc's People

Contributors

allcontributors[bot] avatar maartentibau avatar teksidot avatar timdeschryver avatar xianshenglu avatar

Stargazers

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

Watchers

 avatar  avatar

ngrx-etc's Issues

V4.0.0 Release

Hi Tim!

Will you please release 4.0.0 on NPM? I've upgraded to angular 9 / ngrx 9, and for some reason pointing my package.json directly to github to get the latest code doesn't work.

Thanks for this lib, it really improves the ergonomics of writing reducers.

Error: Angry over 'on' vs 'On'

Hello,
Thanks for this awesome library! But I'm getting the following issue on install at run time (angular 11):
Error: node_modules/ngrx-immer/esm/store/index.d.ts:1:18 - error TS2724: '"../../../@ngrx/store/ngrx-store"' has no exported member named 'On'. Did you mean 'on'?

Reference Weilder Issue
I'm unclear as to whether this means this will be updated for ngrx-etc or not. Is there a workaround in the mean time?

The automated release is failing 🚨

🚨 The automated release from the master branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you could benefit from your bug fixes and new features.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can resolve this πŸ’ͺ.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here is some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


Missing package.json file.

A package.json file at the root of your project is required to release on npm.

Please follow the npm guideline to create a valid package.json file.


Good luck with your project ✨

Your semantic-release bot πŸ“¦πŸš€

Cannot assign to read only property

Hello!

I'm using your package in my project. And I got this error today:
ERROR TypeError: Cannot assign to read only property 'isActive' of object '[object Object]'
How can I fix it?

My reducer:

export interface DatasetInfoState {
  datasetInfo: DatasetPublisherInfo;
  callState: CallState;
}

export const initialState: DatasetInfoState = {
  datasetInfo: null,
  callState: LoadingState.INIT,
};

const datasetInfoReducer = createReducer(
  initialState,
  mutableOn(fromActions.DatasetInfoActions.publishDatasetSuccess, (draft: DatasetInfoState) => {
    draft.callState = LoadingState.LOADED;
    draft.datasetInfo.isActive = true;
  }),

  mutableOn(fromActions.DatasetInfoActions.unpublishDatasetSuccess, (draft: DatasetInfoState) => {
    draft.callState = LoadingState.LOADED;
    draft.datasetInfo.isActive = false;
  })
);

export function reducer(state: DatasetInfoState | undefined, action: Action) {
  return datasetInfoReducer(state, action);
}

Angular 9.1.3
Ngrx 9.0.0
Ngrx-etc 4.0.0

Expose original state object as parameter to reducer function

Hello :)

I have done something very similar by myself (basically just called it immerOn), and just found out, that there is this repo here. One thing that is missing for me, is that I have no access to the original store object in the reducer function when using mutableOn.

As this is supported with the plain immer.js produce function, we might loose some functionality if we do not expose it.

Why would we need the original state?

There are use-cases where we want to check if something on the draft has been changed in comparison to the initial state.
Example https://stackblitz.com/edit/typescript-tkmn83

So my suggestion would basically be this change:

export interface MutableOnReducer<S, C extends ActionCreator[], D = Draft<S>> {
  (state: D, action: ActionType<C[number]>): void
}

==> 

export interface MutableOnReducer<S, C extends ActionCreator[], D = Draft<S>> {
  (state: D, action: ActionType<C[number]>, original?: S): void
}

in order to support usecases, where the original state was used.

I'd be willing to create a PR if the change is welcomed :)

Benedikt

Deprecation in favour of ngrx-immer

I had some troubles with ngrx v11. mutableOn always defined the type of the state as unknown. A switch to ngrx-immer solved the problem at once.

What is the difference between these two libraries? If they serve the same purpose, wouldn't it be better to deprecate one?

@ngrx/data 8.5x breaking change

Looks like there's an issue when upgrading to @ngrx/data 8.5.2

ERROR in ../../../node_modules/ngrx-etc/correlate/correlate-action.d.ts:2:23 - error TS2305: Module '"/node_modules/@ngrx/store/src/models"' has no exported member 'DisallowTypeProperty'.

2 import { TypedAction, **DisallowTypeProperty**, FunctionWithParametersType } from '@ngrx/store/src/models';
                        ~~~~~~~~~~~~~~~~~~~~

I've downgraded back to 8.4x for now.
Let me know if i can help.

Do we need to add a license file?

Hi, thanks for providing this great library. I am really enjoying this.

I just found that we have "license": "MIT", in the package.json while we didn't have the license file. So, do we need to add a license file?

Unable to build with 5.0.0

Getting the following error building with the latest version 5.0.0.

> Executing task: npm run build <

> [email protected] build C:\VSCode\2020\git-hub-tim\ngrx-task
> ng build

ERROR in src/app/root-store/user-store/reducer.ts:9:27 - error TS7016: Could not find a declaration file for module 'ngrx-etc'. 'C:/VSCode/2020/git-hub-tim/ngrx-task/node_modules/ngrx-etc/ngrx-etc.js' implicitly has an 'any' type.
  Try `npm install @types/ngrx-etc` if it exists or add a new declaration (.d.ts) file containing `declare module 'ngrx-etc';`      

9 import { mutableOn } from 'ngrx-etc';

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.