1
0
mirror of https://github.com/bitwarden/desktop.git synced 2024-11-24 11:55:50 +01:00

refactor broadcaster service

This commit is contained in:
Kyle Spearrin 2018-02-10 15:22:07 -05:00
parent f072258d5c
commit b41a38afbb
4 changed files with 44 additions and 6 deletions

View File

@ -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();

View File

@ -1,3 +1,33 @@
import { EventEmitter } from '@angular/core';
import { Injectable } from '@angular/core';
export class BroadcasterService extends EventEmitter<any> { }
@Injectable()
export class BroadcasterService {
subscribers: Map<string, (message: any) => any> = new Map<string, (message: any) => 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);
}
}
}

View File

@ -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() {

View File

@ -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);
}
}