Skip to content

Commit

Permalink
feat(messaging): add breakError to the config (#77) (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xdy1579883916 authored Nov 14, 2024
1 parent 3a9c065 commit ea87d13
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ tsconfig.vitest-temp.json
/.cache
.output
.wxt
.idea
4 changes: 4 additions & 0 deletions packages/messaging/src/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ export function defineGenericMessanging<
removeRootListener = config.addRootListener(message => {
// Validate the message object
if (typeof message.type != 'string' || typeof message.timestamp !== 'number') {
// #77 When the message is invalid, we stop processing the message using return or throw an error (default)
if (config.breakError) {
return;
}
const err = Error(
`[messaging] Unknown message format, must include the 'type' & 'timestamp' fields, received: ${JSON.stringify(
message,
Expand Down
7 changes: 7 additions & 0 deletions packages/messaging/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ export interface BaseMessagingConfig {
* @default console
*/
logger?: Logger;

/**
* Whether to break an error when an invalid message is received.
*
* @default undefined
*/
breakError?: boolean;
}

export interface NamespaceMessagingConfig extends BaseMessagingConfig {
Expand Down

0 comments on commit ea87d13

Please sign in to comment.