mirror of
https://github.com/bitwarden/browser.git
synced 2024-11-23 11:56:00 +01:00
refactor broadcaster service
This commit is contained in:
parent
f072258d5c
commit
b41a38afbb
@ -8,6 +8,7 @@ import {
|
|||||||
Component,
|
Component,
|
||||||
ComponentFactoryResolver,
|
ComponentFactoryResolver,
|
||||||
NgZone,
|
NgZone,
|
||||||
|
OnDestroy,
|
||||||
OnInit,
|
OnInit,
|
||||||
ViewChild,
|
ViewChild,
|
||||||
ViewContainerRef,
|
ViewContainerRef,
|
||||||
@ -38,6 +39,8 @@ import { UserService } from 'jslib/abstractions/user.service';
|
|||||||
|
|
||||||
import { ConstantsService } from 'jslib/services/constants.service';
|
import { ConstantsService } from 'jslib/services/constants.service';
|
||||||
|
|
||||||
|
const BroadcasterSubscriptionId = 'AppComponent';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-root',
|
selector: 'app-root',
|
||||||
styles: [],
|
styles: [],
|
||||||
@ -78,7 +81,7 @@ export class AppComponent implements OnInit {
|
|||||||
window.onscroll = () => this.recordActivity();
|
window.onscroll = () => this.recordActivity();
|
||||||
window.onkeypress = () => this.recordActivity();
|
window.onkeypress = () => this.recordActivity();
|
||||||
|
|
||||||
this.broadcasterService.subscribe((message: any) => {
|
this.broadcasterService.subscribe(BroadcasterSubscriptionId, (message: any) => {
|
||||||
this.ngZone.run(async () => {
|
this.ngZone.run(async () => {
|
||||||
switch (message.command) {
|
switch (message.command) {
|
||||||
case 'loggedIn':
|
case 'loggedIn':
|
||||||
@ -107,6 +110,10 @@ export class AppComponent implements OnInit {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ngOnDestroy() {
|
||||||
|
this.broadcasterService.unsubscribe(BroadcasterSubscriptionId);
|
||||||
|
}
|
||||||
|
|
||||||
private async logOut(expired: boolean) {
|
private async logOut(expired: boolean) {
|
||||||
const userId = await this.userService.getUserId();
|
const userId = await this.userService.getUserId();
|
||||||
|
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -41,6 +41,7 @@ import { MessagingService } from 'jslib/abstractions/messaging.service';
|
|||||||
import { SyncService } from 'jslib/abstractions/sync.service';
|
import { SyncService } from 'jslib/abstractions/sync.service';
|
||||||
|
|
||||||
const SyncInterval = 6 * 60 * 60 * 1000; // 6 hours
|
const SyncInterval = 6 * 60 * 60 * 1000; // 6 hours
|
||||||
|
const BroadcasterSubscriptionId = 'VaultComponent';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-vault',
|
selector: 'app-vault',
|
||||||
@ -72,7 +73,7 @@ export class VaultComponent implements OnInit, OnDestroy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async ngOnInit() {
|
async ngOnInit() {
|
||||||
this.broadcasterService.subscribe((message: any) => {
|
this.broadcasterService.subscribe(BroadcasterSubscriptionId, (message: any) => {
|
||||||
this.ngZone.run(async () => {
|
this.ngZone.run(async () => {
|
||||||
let detectChanges = true;
|
let detectChanges = true;
|
||||||
|
|
||||||
@ -145,7 +146,7 @@ export class VaultComponent implements OnInit, OnDestroy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ngOnDestroy() {
|
ngOnDestroy() {
|
||||||
this.broadcasterService.unsubscribe();
|
this.broadcasterService.unsubscribe(BroadcasterSubscriptionId);
|
||||||
}
|
}
|
||||||
|
|
||||||
async load() {
|
async load() {
|
||||||
|
@ -16,6 +16,6 @@ export class DesktopMessagingService implements MessagingService {
|
|||||||
send(subscriber: string, arg: any = {}) {
|
send(subscriber: string, arg: any = {}) {
|
||||||
const message = Object.assign({}, { command: subscriber }, arg);
|
const message = Object.assign({}, { command: subscriber }, arg);
|
||||||
ipcRenderer.send('messagingService', message);
|
ipcRenderer.send('messagingService', message);
|
||||||
this.broadcasterService.emit(message);
|
this.broadcasterService.send(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user