Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ngrx/effects

Package Overview
Dependencies
Maintainers
4
Versions
144
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ngrx/effects

Side effect model for @ngrx/store

  • 8.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
122K
decreased by-77.4%
Maintainers
4
Weekly downloads
 
Created

What is @ngrx/effects?

@ngrx/effects is a library for managing side effects in Angular applications using the Redux pattern. It allows you to isolate side effects from your components and services, making your code more predictable and easier to test.

What are @ngrx/effects's main functionalities?

Handling Side Effects

This code demonstrates how to handle side effects using @ngrx/effects. The `loadItems$` effect listens for the `loadItems` action, calls a service to fetch data, and then dispatches either a success or failure action based on the result.

import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { of } from 'rxjs';
import { catchError, map, mergeMap } from 'rxjs/operators';
import { MyService } from './my-service';
import * as MyActions from './my-actions';

@Injectable()
export class MyEffects {
  loadItems$ = createEffect(() => this.actions$.pipe(
    ofType(MyActions.loadItems),
    mergeMap(() => this.myService.getAll().pipe(
      map(items => MyActions.loadItemsSuccess({ items })),
      catchError(() => of(MyActions.loadItemsFailure()))
    ))
  ));

  constructor(
    private actions$: Actions,
    private myService: MyService
  ) {}
}

Combining Multiple Effects

This code demonstrates how to combine multiple effects in a single class. The `loadItems$` effect handles loading items, while the `updateItem$` effect handles updating an item. Both effects listen for their respective actions and call the appropriate service methods.

import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { of } from 'rxjs';
import { catchError, map, mergeMap } from 'rxjs/operators';
import { MyService } from './my-service';
import * as MyActions from './my-actions';

@Injectable()
export class MyEffects {
  loadItems$ = createEffect(() => this.actions$.pipe(
    ofType(MyActions.loadItems),
    mergeMap(() => this.myService.getAll().pipe(
      map(items => MyActions.loadItemsSuccess({ items })),
      catchError(() => of(MyActions.loadItemsFailure()))
    ))
  ));

  updateItem$ = createEffect(() => this.actions$.pipe(
    ofType(MyActions.updateItem),
    mergeMap(action => this.myService.update(action.item).pipe(
      map(() => MyActions.updateItemSuccess({ item: action.item })),
      catchError(() => of(MyActions.updateItemFailure()))
    ))
  ));

  constructor(
    private actions$: Actions,
    private myService: MyService
  ) {}
}

Error Handling

This code demonstrates how to handle errors in effects. The `loadItems$` effect catches any errors that occur during the service call and dispatches a `loadItemsFailure` action with the error information.

import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { of } from 'rxjs';
import { catchError, map, mergeMap } from 'rxjs/operators';
import { MyService } from './my-service';
import * as MyActions from './my-actions';

@Injectable()
export class MyEffects {
  loadItems$ = createEffect(() => this.actions$.pipe(
    ofType(MyActions.loadItems),
    mergeMap(() => this.myService.getAll().pipe(
      map(items => MyActions.loadItemsSuccess({ items })),
      catchError(error => of(MyActions.loadItemsFailure({ error })))
    ))
  ));

  constructor(
    private actions$: Actions,
    private myService: MyService
  ) {}
}

Other packages similar to @ngrx/effects

Keywords

FAQs

Package last updated on 10 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc