mirror of
https://github.com/bitwarden/browser
synced 2025-12-20 10:13:31 +00:00
[PM-7489] Introduce MessageSender & MessageListener (#8709)
* Introduce MessageSender * Update `messageSenderFactory` * Remove Comment * Use BrowserApi * Update Comment * Rename to CommandDefinition * Add More Documentation to MessageSender * Add `EMPTY` helpers and remove NoopMessageSender * Calm Down Logging * Limit Logging On Known Errors * Use `messageStream` Parameter Co-authored-by: Matt Gibson <mgibson@bitwarden.com> * Add eslint rules * Update Error Handling Co-authored-by: Cesar Gonzalez <cesar.a.gonzalezcs@gmail.com> * Delete Lazy Classes In Favor of Observable Factories * Remove Fido Messages --------- Co-authored-by: Matt Gibson <mgibson@bitwarden.com> Co-authored-by: Cesar Gonzalez <cesar.a.gonzalezcs@gmail.com>
This commit is contained in:
@@ -1,34 +0,0 @@
|
||||
import {
|
||||
BroadcasterService as BroadcasterServiceAbstraction,
|
||||
MessageBase,
|
||||
} from "../abstractions/broadcaster.service";
|
||||
|
||||
export class BroadcasterService implements BroadcasterServiceAbstraction {
|
||||
subscribers: Map<string, (message: MessageBase) => void> = new Map<
|
||||
string,
|
||||
(message: MessageBase) => void
|
||||
>();
|
||||
|
||||
send(message: MessageBase, 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: MessageBase) => void) {
|
||||
this.subscribers.set(id, messageCallback);
|
||||
}
|
||||
|
||||
unsubscribe(id: string) {
|
||||
if (this.subscribers.has(id)) {
|
||||
this.subscribers.delete(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import { Subscription } from "rxjs";
|
||||
|
||||
import { BroadcasterService, MessageBase } from "../abstractions/broadcaster.service";
|
||||
import { MessageListener, MessageSender } from "../messaging";
|
||||
|
||||
/**
|
||||
* Temporary implementation that just delegates to the message sender and message listener
|
||||
* and manages their subscriptions.
|
||||
*/
|
||||
export class DefaultBroadcasterService implements BroadcasterService {
|
||||
subscriptions = new Map<string, Subscription>();
|
||||
|
||||
constructor(
|
||||
private readonly messageSender: MessageSender,
|
||||
private readonly messageListener: MessageListener,
|
||||
) {}
|
||||
|
||||
send(message: MessageBase, id?: string) {
|
||||
this.messageSender.send(message?.command, message);
|
||||
}
|
||||
|
||||
subscribe(id: string, messageCallback: (message: MessageBase) => void) {
|
||||
this.subscriptions.set(
|
||||
id,
|
||||
this.messageListener.allMessages$.subscribe((message) => {
|
||||
messageCallback(message);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
unsubscribe(id: string) {
|
||||
const subscription = this.subscriptions.get(id);
|
||||
subscription?.unsubscribe();
|
||||
this.subscriptions.delete(id);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
import { MessagingService } from "../abstractions/messaging.service";
|
||||
|
||||
export class NoopMessagingService implements MessagingService {
|
||||
send(subscriber: string, arg: any = {}) {
|
||||
// Do nothing...
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user