ngx-ngrx-component-store-debug-tools
TypeScript icon, indicating that this package has built-in type declarations

0.0.7 • Public • Published

Debug decorators and utilities for @ngrx/component-store

Try out the demo app

On StackBlitz

Open the console to see the logs:

Open in StackBlitz

Locally

Clone the repo, then ng serve demo

Installation

NPM

npm install ngx-ngrx-component-store-debug-tools

Yarn

yarn add ngx-ngrx-component-store-debug-tools --dev

Usage

The demo app has three Card component, each has its own Store instance.

@LogState decorator

After every state change, this decorator displays the current state of the store and the changes since the last state change.

const logLevel = 'debug';

export interface User {
  name: string | null
}

export interface CardState {
  user: User
}

@Injectable()
@LogState({ logLevel })
export class CardStore extends ComponentStore<CardState> {

  constructor() {
    super({
      user: {
        name: null
      }
    });
  }

  readonly updateUser = this.updater((state: CardState, user: User): CardState => {
    return {
      ...state,
      user
    };
  });
}

When the component creates this store, the decorator shows its initial state:

console

After we call store.updateUser({ user: 'Ben' }), the decorator shows the new state and the diff:

console

@LogUpdater decorator

This decorator shows the parameter of an updater on the console:

  @LogUpdater({ logLevel })
  readonly updateUser = this.updater((state: CardState, user: User): CardState => {
    return {
      ...state,
      user
    };
  });

After we call store.updateUser({ user: 'Ben' }), the decorator logs the updater call and its parameter on the console:

console

@LogEffect decorator

This decorator shows the trigger of an effect on the console.

  protected fakeServiceFetchUser$(id: number): Observable<User> {
    return of({
      name: 'Frank'
    }).pipe(delay(1000));
  } 

  @LogEffect({ logLevel })
  readonly fetchUserFromServer = this.effect((id$: Observable<number>) => {
    return id$.pipe(
      switchMap((id) => this.fakeServiceFetchUser$(id).pipe(
        tapResponse(
          (user) => this.updateUser(user),
          (error: unknown) => console.error(error),
        ),
      )),
    );
  });

After we call store.fetchUserFromServer(1), the decorator logs the effect call and its parameter on the console:

console

Since the effect calls the updater, the updater call and the state change are also visible.

Store IDs

If we use a separate store for every component instance and create multiple component instances, each of them has an unique id (0001, 0002, 0003):

console

Log levels

The following console log levels are supported, adjustable by the decorator's logLevel parameter:

  • 'log'
  • 'info'
  • 'debug'
  • 'trace'
  • 'off' and undefined: these turns off the logging

Install

npm i ngx-ngrx-component-store-debug-tools

DownloadsWeekly Downloads

4

Version

0.0.7

License

MIT

Unpacked Size

63 kB

Total Files

13

Last publish

Collaborators

  • gergelyszerovay