packages/devtools-plugin/src/devtools.module.ts

Summary

Maintainability
B
4 hrs
Test Coverage
A
90%
import {
  NgModule,
  ModuleWithProviders,
  InjectionToken,
  EnvironmentProviders,
  makeEnvironmentProviders
} from '@angular/core';
import { withNgxsPlugin } from '@ngxs/store';

import { NgxsDevtoolsOptions, NGXS_DEVTOOLS_OPTIONS } from './symbols';
import { NgxsReduxDevtoolsPlugin } from './devtools.plugin';

export function devtoolsOptionsFactory(options: NgxsDevtoolsOptions) {
  return {
    name: 'NGXS',
    ...options
  };
}

export const USER_OPTIONS = new InjectionToken('USER_OPTIONS');

@NgModule()
export class NgxsReduxDevtoolsPluginModule {
  static forRoot(
    options?: NgxsDevtoolsOptions
  ): ModuleWithProviders<NgxsReduxDevtoolsPluginModule> {
    return {
      ngModule: NgxsReduxDevtoolsPluginModule,
      providers: [
        withNgxsPlugin(NgxsReduxDevtoolsPlugin),
        {
          provide: USER_OPTIONS,
          useValue: options
        },
        {
          provide: NGXS_DEVTOOLS_OPTIONS,
          useFactory: devtoolsOptionsFactory,
          deps: [USER_OPTIONS]
        }
      ]
    };
  }
}

export function withNgxsReduxDevtoolsPlugin(
  options?: NgxsDevtoolsOptions
): EnvironmentProviders {
  return makeEnvironmentProviders([
    withNgxsPlugin(NgxsReduxDevtoolsPlugin),
    {
      provide: USER_OPTIONS,
      useValue: options
    },
    {
      provide: NGXS_DEVTOOLS_OPTIONS,
      useFactory: devtoolsOptionsFactory,
      deps: [USER_OPTIONS]
    }
  ]);
}