import { NgModule , ModuleWithProviders , NgZone , InjectionToken } from '@angular/core' ; import { SignalR } from '../services/signalr' ; import { SignalRConfiguration } from '../services/signalr.configuration' ; const SIGNALR_CONFIGURATION = new InjectionToken < SignalRConfiguration > ( 'SIGNALR_CONFIGURATION' ) ; export function createSignalr ( configuration , zone ) { const jConnectionFn = getJConnectionFn ( ) ; return new SignalR ( configuration , zone , jConnectionFn ) ; } function getJConnectionFn ( ) { const jQuery = getJquery ( ) ; const hubConnectionFn = jQuery . hubConnection ; if ( hubConnectionFn == null ) { throw new Error ( 'Signalr failed to initialize. Script \'jquery.signalR.js\' is missing. Please make sure to include \'jquery.signalR.js\' script.' ) ; } return hubConnectionFn ; } function getJquery ( ) { const jQuery = ( window as any ) . jQuery ; if ( jQuery == null ) { throw new Error ( 'Signalr failed to initialize. Script \'jquery.js\' is missing. Please make sure to include jquery script.' ) ; } return jQuery ; } @ NgModule ( { providers : [ { provide : SignalR , useValue : SignalR } ] } ) export class SignalRModule { public static forRoot ( getSignalRConfiguration ) { return { ngModule : SignalRModule , providers : [ { provide : SIGNALR_CONFIGURATION , useFactory : getSignalRConfiguration } , { deps : [ SIGNALR_CONFIGURATION , NgZone ] , provide : SignalR , useFactory : ( createSignalr ) } ] , } ; } public static forChild ( ) { throw new Error ( 'forChild method not implemented' ) ; } }