1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-14 23:33:31 +00:00

[PM-8292] Fixup ForegroundSyncService (#9292)

* Change `object` to `Record<string, unknown>`

* Change `object` to `Record<string, unknown>` Pt. 2

* Update ForegroundSyncService

- Manage finish message in the listener to more gaurantee a message back
- Make the timeout much longer
- Allow it to throw if the background sync service threw

---------

Co-authored-by: Cesar Gonzalez <cesar.a.gonzalezcs@gmail.com>
This commit is contained in:
Justin Baur
2024-05-29 12:12:58 -04:00
committed by GitHub
parent beb930902a
commit a6df923416
23 changed files with 328 additions and 86 deletions

View File

@@ -12,7 +12,7 @@ describe("helpers", () => {
});
it("can get the command from a message definition", () => {
const commandDefinition = new CommandDefinition<object>("myCommand");
const commandDefinition = new CommandDefinition<Record<string, unknown>>("myCommand");
const command = getCommand(commandDefinition);
@@ -22,9 +22,9 @@ describe("helpers", () => {
describe("tag integration", () => {
it("can tag and identify as tagged", async () => {
const messagesSubject = new Subject<Message<object>>();
const messagesSubject = new Subject<Message<Record<string, unknown>>>();
const taggedMessages = messagesSubject.asObservable().pipe(tagAsExternal);
const taggedMessages = messagesSubject.asObservable().pipe(tagAsExternal());
const firstValuePromise = firstValueFrom(taggedMessages);
@@ -39,7 +39,7 @@ describe("helpers", () => {
describe("isExternalMessage", () => {
it.each([null, { command: "myCommand", test: "object" }, undefined] as Message<
Record<string, unknown>
>[])("returns false when value is %s", (value: Message<object>) => {
>[])("returns false when value is %s", (value: Message<Record<string, unknown>>) => {
expect(isExternalMessage(value)).toBe(false);
});
});

View File

@@ -1,8 +1,10 @@
import { MonoTypeOperatorFunction, map } from "rxjs";
import { map } from "rxjs";
import { Message, CommandDefinition } from "./types";
import { CommandDefinition } from "./types";
export const getCommand = (commandDefinition: CommandDefinition<object> | string) => {
export const getCommand = (
commandDefinition: CommandDefinition<Record<string, unknown>> | string,
) => {
if (typeof commandDefinition === "string") {
return commandDefinition;
} else {
@@ -16,8 +18,8 @@ export const isExternalMessage = (message: Record<PropertyKey, unknown>) => {
return message?.[EXTERNAL_SOURCE_TAG] === true;
};
export const tagAsExternal: MonoTypeOperatorFunction<Message<object>> = map(
(message: Message<object>) => {
export const tagAsExternal = <T extends Record<PropertyKey, unknown>>() => {
return map((message: T) => {
return Object.assign(message, { [EXTERNAL_SOURCE_TAG]: true });
},
);
});
};

View File

@@ -11,7 +11,7 @@ import { Message, CommandDefinition } from "./types";
* or vault data changes and those observables should be preferred over messaging.
*/
export class MessageListener {
constructor(private readonly messageStream: Observable<Message<object>>) {}
constructor(private readonly messageStream: Observable<Message<Record<string, unknown>>>) {}
/**
* A stream of all messages sent through the application. It does not contain type information for the
@@ -28,7 +28,9 @@ export class MessageListener {
*
* @param commandDefinition The CommandDefinition containing the information about the message type you care about.
*/
messages$<T extends object>(commandDefinition: CommandDefinition<T>): Observable<T> {
messages$<T extends Record<string, unknown>>(
commandDefinition: CommandDefinition<T>,
): Observable<T> {
return this.allMessages$.pipe(
filter((msg) => msg?.command === commandDefinition.command),
) as Observable<T>;

View File

@@ -3,9 +3,9 @@ import { CommandDefinition } from "./types";
class MultiMessageSender implements MessageSender {
constructor(private readonly innerMessageSenders: MessageSender[]) {}
send<T extends object>(
send<T extends Record<string, unknown>>(
commandDefinition: string | CommandDefinition<T>,
payload: object | T = {},
payload: Record<string, unknown> | T = {},
): void {
for (const messageSender of this.innerMessageSenders) {
messageSender.send(commandDefinition, payload);
@@ -26,7 +26,10 @@ export abstract class MessageSender {
* @param commandDefinition
* @param payload
*/
abstract send<T extends object>(commandDefinition: CommandDefinition<T>, payload: T): void;
abstract send<T extends Record<string, unknown>>(
commandDefinition: CommandDefinition<T>,
payload: T,
): void;
/**
* A legacy method for sending messages in a non-type safe way.
@@ -38,12 +41,12 @@ export abstract class MessageSender {
* @param payload Extra contextual information regarding the message. Be aware that this payload may
* be serialized and lose all prototype information.
*/
abstract send(command: string, payload?: object): void;
abstract send(command: string, payload?: Record<string, unknown>): void;
/** Implementation of the other two overloads, read their docs instead. */
abstract send<T extends object>(
abstract send<T extends Record<string, unknown>>(
commandDefinition: CommandDefinition<T> | string,
payload: T | object,
payload: T | Record<string, unknown>,
): void;
/**

View File

@@ -5,11 +5,11 @@ import { MessageSender } from "./message.sender";
import { Message, CommandDefinition } from "./types";
export class SubjectMessageSender implements MessageSender {
constructor(private readonly messagesSubject: Subject<Message<object>>) {}
constructor(private readonly messagesSubject: Subject<Message<Record<string, unknown>>>) {}
send<T extends object>(
send<T extends Record<string, unknown>>(
commandDefinition: string | CommandDefinition<T>,
payload: object | T = {},
payload: Record<string, unknown> | T = {},
): void {
const command = getCommand(commandDefinition);
this.messagesSubject.next(Object.assign(payload ?? {}, { command: command }));

View File

@@ -5,9 +5,9 @@ declare const tag: unique symbol;
* alonside `MessageSender` and `MessageListener` for providing a type
* safe(-ish) way of sending and receiving messages.
*/
export class CommandDefinition<T extends object> {
export class CommandDefinition<T extends Record<string, unknown>> {
[tag]: T;
constructor(readonly command: string) {}
}
export type Message<T extends object> = { command: string } & T;
export type Message<T extends Record<string, unknown>> = { command: string } & T;