import { APP_INITIALIZER , InjectionToken , ModuleWithProviders , NgModule } from '@angular/core' ; import { NgxsModule } from '@ngxs/store' ; import { NgxsWebsocketPluginOptions , NGXS_WEBSOCKET_OPTIONS , noop } from './symbols' ; import { WebSocketHandler } from './websocket-handler' ; import { WebSocketSubject } from './websocket-subject' ; export function websocketOptionsFactory ( options ) { return { reconnectInterval : 5000 , reconnectAttempts : 10 , typeKey : 'type' , deserializer ( e ) { return JSON . parse ( e . data ) ; } , serializer ( value ) { return JSON . stringify ( value ) ; } , ... options , } ; } export const USER_OPTIONS = new InjectionToken ( 'USER_OPTIONS' ) ; @ NgModule ( { imports : [ NgxsModule ] , } ) export class NgxsWebsocketPluginModule { static forRoot ( options ? ) { return { ngModule : NgxsWebsocketPluginModule , providers : [ WebSocketSubject , WebSocketHandler , { provide : USER_OPTIONS , useValue : options , } , { provide : NGXS_WEBSOCKET_OPTIONS , useFactory : websocketOptionsFactory , deps : [ USER_OPTIONS ] , } , { provide : APP_INITIALIZER , useFactory : noop , deps : [ WebSocketHandler ] , multi : true , } , ] , } ; } }