ngrx-graph
TypeScript icon, indicating that this package has built-in type declarations

0.0.10 • Public • Published

ngrx-graph

Motivation:

Working with a very big NgRx store in an angular application will lead to having lots of actions/effects and lots of interactions between components/actions/reducers. It gets very tedious very quickly to follow an action from the start to the end, and it is very easy to miss an action dispatched in an effect somewhere along the chain of actions.

This packages, tries to collect all actions/components/reducers participating in a particular flow and generate dot files for that flow, with the idea that following a graph visually is easier than following effects and actions in code.

It is also possible to see the whole net with all actions/components/reducers, but that is more important is to follow a particular action from the start to the end (the optional argument)

How it works

This package generates dot files representing the interaction between ngrx actions, components, effects and reducers.

Dot files can be then used to generate graphs using Graphviz, so this needs to be installed first, e.g:

for file in *.dot; do; dot -Tsvg $file -o "${file%.*}".svg; rm $file; done

The first run will generate a json file (see --structureFile flag), which is used for the next runs if the flag --force was not set as cache. If this file exists, source code will not be parsed for actions, the recorded structure will be taken from that json file. This speeds up the process considerably.

Graph Keys
Component component
Action component
Action in focus component
Nested Action component
Reducer component
Examples

Case 1:

Input:

// actions
export const action1 = createAction('Action1');
export const action2 = createAction('Action2');
export const action3 = createAction('Action3');

// component
@Component()
export class FirstComponent {
  onEvent() {
    this.store.dispatch(action1());
  }
}

// effects
@Injectable()
export class ExampleEffects {
  effect1$ = createEffect(() =>
    this.actions$.pipe(
      ofType(action1),
      switchMap(() => [action2(), action3()]),
    ),
  );
}

// reducer
const firstReducer = createReducer(
  on(action3, () => {
    // ...
  }),
);

Output:

npx ngrx-graph -j -f
npx ngrx-graph action1
npx ngrx-graph action3

Case 2 (nested actions):

Input:

// actions
export const nestedAction = createAction(
  'NestedAction',
  props<{ action: Action }>(),
);
export const action1 = createAction('Action1');
export const action2 = createAction('Action2');
export const action3 = createAction('Action3');

// component
@Component()
export class FirstComponent {
  onEvent() {
    this.store.dispatch(nestedAction({ action: action1() }));
  }
}

// effects
@Injectable()
export class ExampleEffects {
  effect1$ = createEffect(() =>
    this.actions$.pipe(
      ofType(action1),
      switchMap(() => [nestedAction1({ action: action2() }), action3()])),
    ),
  );

  effect2$ = createEffect(() =>
    this.actions$.pipe(
      ofType(nestedAction1),
      map(({ action }) => nestedAction2( { action: action()})),
    ),
  );

  effect3$ = createEffect(() =>
    this.actions$.pipe(
      ofType(nestedAction2),
      map(({ action }) => action())),
    ),
  );
}

// reducer
const firstReducer = createReducer(
  on(action3, () => {
    // ...
  }),
)

Output:

npx ngrx-graph -j -f
npx ngrx-graph action1
npx ngrx-graph action3
Usage
$ npm install -g ngrx-graph
$ ngrx-graph COMMAND
running command...
$ ngrx-graph (--version)
ngrx-graph/0.0.10 linux-x64 node-v16.19.1
$ ngrx-graph --help [COMMAND]
USAGE
  $ ngrx-graph COMMAND
...
Commands

ngrx-graph graph [ACTION]

Generate NgRx actions graph

USAGE
  $ ngrx-graph graph [ACTION] [-f] [-j] [-a] [-d <value>] [-o <value>] [-s <value>]

ARGUMENTS
  ACTION  Action of interest. It will be ignored if --jsonOnly is used

FLAGS
  -a, --all                    Generate the whole graph for all actions and connected component, effects and reducers.
                               It will be ignored if --jsonOnly is used
  -d, --srcDir=<value>         [default: current directory] Source directory to grab actions from, usually the directory
                               with package.json in it
  -f, --force                  Force regenrating the graph structure
  -j, --jsonOnly               Generate only the structure json file, can be combined with --structureFile option. It
                               overrides --all and [ACTION]
  -o, --outputDir=<value>      [default: /tmp] Destination directory, where to save the generated files
  -s, --structureFile=<value>  [default: ngrx-graph.json] Then name of the structure json file, Path is taken from
                               --outputDir option

DESCRIPTION
  Generate NgRx actions graph

EXAMPLES
  $ ngrx-graph graph

See code: dist/commands/graph/index.ts

ngrx-graph help [COMMANDS]

Display help for ngrx-graph.

USAGE
  $ ngrx-graph help [COMMANDS] [-n]

ARGUMENTS
  COMMANDS  Command to show help for.

FLAGS
  -n, --nested-commands  Include all nested commands in the output.

DESCRIPTION
  Display help for ngrx-graph.

See code: @oclif/plugin-help

Status:

This project is still young and it encourages collaborations. If you have an ideas/questions/fixes please do not hesitate to open an issue or provide a pull request.

I work on this on my own free time only.

Package Sidebar

Install

npm i ngrx-graph

Weekly Downloads

1

Version

0.0.10

License

MIT

Unpacked Size

41.7 kB

Total Files

30

Last publish

Collaborators

  • anajjar