Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(messaging): add breakError to the config (#77) #81

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading