diff --git a/src/app/app.component.ts b/src/app/app.component.ts index e6eac033..9dd5f88d 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -8,6 +8,7 @@ import { Component, ComponentFactoryResolver, NgZone, + OnDestroy, OnInit, ViewChild, ViewContainerRef, @@ -38,6 +39,8 @@ import { UserService } from 'jslib/abstractions/user.service'; import { ConstantsService } from 'jslib/services/constants.service'; +const BroadcasterSubscriptionId = 'AppComponent'; + @Component({ selector: 'app-root', styles: [], @@ -78,7 +81,7 @@ export class AppComponent implements OnInit { window.onscroll = () => this.recordActivity(); window.onkeypress = () => this.recordActivity(); - this.broadcasterService.subscribe((message: any) => { + this.broadcasterService.subscribe(BroadcasterSubscriptionId, (message: any) => { this.ngZone.run(async () => { switch (message.command) { case 'loggedIn': @@ -107,6 +110,10 @@ export class AppComponent implements OnInit { }); } + ngOnDestroy() { + this.broadcasterService.unsubscribe(BroadcasterSubscriptionId); + } + private async logOut(expired: boolean) { const userId = await this.userService.getUserId(); diff --git a/src/app/services/broadcaster.service.ts b/src/app/services/broadcaster.service.ts index 659a9554..fe747e7b 100644 --- a/src/app/services/broadcaster.service.ts +++ b/src/app/services/broadcaster.service.ts @@ -1,3 +1,33 @@ -import { EventEmitter } from '@angular/core'; +import { Injectable } from '@angular/core'; -export class BroadcasterService extends EventEmitter { } +@Injectable() +export class BroadcasterService { + subscribers: Map any> = new Map any>(); + + send(message: any, id?: string) { + if (id != null) { + if (this.subscribers.has(id)) { + this.subscribers.get(id)(message); + } + return; + } + + this.subscribers.forEach((value) => { + value(message); + }); + } + + subscribe(id: string, messageCallback: (message: any) => any) { + if (this.subscribers.has(id)) { + return; + } + + this.subscribers.set(id, messageCallback); + } + + unsubscribe(id: string) { + if (this.subscribers.has(id)) { + this.subscribers.delete(id); + } + } +} diff --git a/src/app/vault/vault.component.ts b/src/app/vault/vault.component.ts index 132b7f56..d757e27b 100644 --- a/src/app/vault/vault.component.ts +++ b/src/app/vault/vault.component.ts @@ -41,6 +41,7 @@ import { MessagingService } from 'jslib/abstractions/messaging.service'; import { SyncService } from 'jslib/abstractions/sync.service'; const SyncInterval = 6 * 60 * 60 * 1000; // 6 hours +const BroadcasterSubscriptionId = 'VaultComponent'; @Component({ selector: 'app-vault', @@ -72,7 +73,7 @@ export class VaultComponent implements OnInit, OnDestroy { } async ngOnInit() { - this.broadcasterService.subscribe((message: any) => { + this.broadcasterService.subscribe(BroadcasterSubscriptionId, (message: any) => { this.ngZone.run(async () => { let detectChanges = true; @@ -145,7 +146,7 @@ export class VaultComponent implements OnInit, OnDestroy { } ngOnDestroy() { - this.broadcasterService.unsubscribe(); + this.broadcasterService.unsubscribe(BroadcasterSubscriptionId); } async load() { diff --git a/src/services/desktopMessaging.service.ts b/src/services/desktopMessaging.service.ts index 30cfb91e..a814c41f 100644 --- a/src/services/desktopMessaging.service.ts +++ b/src/services/desktopMessaging.service.ts @@ -16,6 +16,6 @@ export class DesktopMessagingService implements MessagingService { send(subscriber: string, arg: any = {}) { const message = Object.assign({}, { command: subscriber }, arg); ipcRenderer.send('messagingService', message); - this.broadcasterService.emit(message); + this.broadcasterService.send(message); } }