diff --git a/package-lock.json b/package-lock.json index 0fea1d8..ed12e30 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "ngx-mqtt", - "version": "6.0.0-alpha.9", + "version": "6.0.0-alpha.11", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 86ab45d..5f9d47d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ngx-mqtt", - "version": "6.0.0-alpha.9", + "version": "6.0.0-alpha.11", "description": "ngx mqtt client library", "main": "bundles/ngx-mqtt.min.js", "module": "./src/index.js", diff --git a/src/mqtt.module.ts b/src/mqtt.module.ts index 928d185..c0c609f 100644 --- a/src/mqtt.module.ts +++ b/src/mqtt.module.ts @@ -17,24 +17,19 @@ export const MQTT_SERVICE_OPTIONS: IMqttServiceOptions = { path: '' }; -export const MqttServiceConfig = new InjectionToken('NgxMqttServiceConfig'); -export const MqttClientService = new InjectionToken('NgxMqttClientService'); +export function mqttServiceFactory() { + return new MqttService(MQTT_SERVICE_OPTIONS); +} @NgModule() export class MqttModule { - static forRoot(config: IMqttServiceOptions, client?: MqttClient): ModuleWithProviders { + static forRoot(providedService: any = { + provide: MqttService, + useFactory: mqttServiceFactory + }): ModuleWithProviders { return { ngModule: MqttModule, - providers: [ - { - provide: MqttServiceConfig, - useValue: config - }, - { - provide: MqttClientService, - useValue: client - } - ] + providers: [providedService] }; } } \ No newline at end of file diff --git a/src/mqtt.service.ts b/src/mqtt.service.ts index 55f44d7..a25266b 100644 --- a/src/mqtt.service.ts +++ b/src/mqtt.service.ts @@ -24,16 +24,13 @@ import { IPublishOptions } from './mqtt.model'; -import { MqttModule, MqttServiceConfig, MqttClientService } from './index'; +import { MqttModule } from './index'; /** * With an instance of MqttService, you can observe and subscribe to MQTT in multiple places, e.g. in different components, * to only subscribe to the broker once per MQTT filter. * It also handles proper unsubscription from the broker, if the last observable with a filter is closed. */ -@Injectable({ - providedIn: 'root', -}) export class MqttService { /** a map of all mqtt observables by filter */ public observables: { [filter: string]: Observable } = {}; @@ -63,8 +60,8 @@ export class MqttService { * @param client an instance of IMqttClient */ constructor( - @Inject(MqttServiceConfig) private options: IMqttServiceOptions, - @Inject(MqttClientService) private client?: IMqttClient + private options: IMqttServiceOptions, + private client?: IMqttClient ) { if (options.connectOnCreate !== false) { this.connect({}, client); diff --git a/tests/mqtt.service.spec.ts b/tests/mqtt.service.spec.ts index 9376c0f..7b865d7 100644 --- a/tests/mqtt.service.spec.ts +++ b/tests/mqtt.service.spec.ts @@ -1,18 +1,5 @@ import { inject, TestBed } from '@angular/core/testing'; import { MqttService } from '../src/mqtt.service'; -import { MqttModule } from '../src/mqtt.module'; - -describe('TestService', () => { - beforeEach(() => { - TestBed.configureTestingModule({ - imports: [MqttModule.forRoot({})] - }); - }); - - it('should be created', inject([MqttService], (service: MqttService) => { - expect(service).toBeTruthy(); - })); -}); describe('MqttService', () => { it('is defined', () => {