Skip to content

Latest commit

 

History

History
62 lines (48 loc) · 1.7 KB

no-cyclic-action.md

File metadata and controls

62 lines (48 loc) · 1.7 KB

Avoid cyclic actions in effects and epics (no-cyclic-action)

This rule effects failures for effects and epics that emit actions that would pass their ofType filter. Such actions are cyclic and, upon emission, immediately re-trigger the effect or epic.

Rule details

Examples of incorrect code for this rule:

actions.pipe(
  ofType("SOMETHING"),
  map(() => ({ type: "SOMETHING" }))
);

Examples of correct code for this rule:

actions.pipe(
  ofType("SOMETHING"),
  map(() => ({ type: "SOMETHING_ELSE" }))
);

This rule can be used with effects and epics, so it makes no attempt to discern whether or not dispatching is disabled for an NgRx effect. That is, code like this will effect (🙈) a failure:

someEffect = createEffect(() =>
  this.actions$.pipe(
    ofType("SOMETHING"),
    tap(() => console.log("do something")),
  ),
  { dispatch: false }
);

Instead, you can use the the RxJS ignoreElements operator:

someEffect = createEffect(() =>
  this.actions$.pipe(
    ofType("SOMETHING"),
    tap(() => console.log("do something")),
    ignoreElements()
  )
);

Or you can use an ESLint inline comment to disable the rule for a specific effect.

Options

This rule accepts a single option which is an object with an observable property that is a regular expression used to match an effect or epic's actions observable. The default observable regular expression should match most effect and epic action sources.

{
  "rxjs/no-cyclic-action": [
    "error",
    { "observable": "[Aa]ction(s|s\\$|\\$)$" }
  ]
}