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

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import { NgModule, ModuleWithProviders, InjectionToken } from '@angular/core';
import { NgxsModule, NGXS_PLUGINS } 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({
  imports: [NgxsModule]
})
export class NgxsReduxDevtoolsPluginModule {
  static forRoot(options?: NgxsDevtoolsOptions): ModuleWithProviders {
    return {
      ngModule: NgxsReduxDevtoolsPluginModule,
      providers: [
        {
          provide: NGXS_PLUGINS,
          useClass: NgxsReduxDevtoolsPlugin,
          multi: true
        },
        {
          provide: USER_OPTIONS,
          useValue: options
        },
        {
          provide: NGXS_DEVTOOLS_OPTIONS,
          useFactory: devtoolsOptionsFactory,
          deps: [USER_OPTIONS]
        }
      ]
    };
  }
}