-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added docs and switched to debug logging for compat reasons
- Loading branch information
1 parent
fca3b7e
commit 8d5741e
Showing
7 changed files
with
115 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,52 @@ | ||
import { IMessageStore } from "./message-store.interface"; | ||
import { Message } from "./message.type"; | ||
|
||
/** | ||
* Context passed to a message handler. | ||
* Used to access the message store or logger. | ||
*/ | ||
export type MessageHandlerContext = { | ||
logger: any; | ||
messageStore: IMessageStore; | ||
}; | ||
|
||
/** | ||
* A function that handles a particular message. | ||
* Called by the message dispatcher when a message is received during a subscription. | ||
* | ||
* @param message The message being handled. | ||
* @param context The context of the message handler. | ||
* @returns A promise that resolves when the message has been handled. | ||
* | ||
* @example | ||
* const addItemHandler: MessageHandlerFunc = async (message, context) => { | ||
* // Validate the message body. | ||
* if (!message.data.item) { | ||
* return; | ||
* | ||
* // Calculate current state | ||
* const currentState = await messageStore.project<ShoppingCart>('cart-123', shoppingCartProjection); | ||
* | ||
* // Handle the message | ||
* try { | ||
* const { item } = message.data; | ||
* currentState.addItem(item); | ||
* await context.messageStore.writeMessage('cart-123', { | ||
* id: uuid(), | ||
* type: 'ItemAddedToCart', | ||
* data: { item }, | ||
* metadata: message.metadata, | ||
* }); | ||
* } catch (error) { | ||
* await context.messageStore.writeMessage('cart-123', { | ||
* id: uuid(), | ||
* type: 'AddItemFailed', | ||
* data: { error }, | ||
* metadata: message.metadata, | ||
* }); | ||
* } | ||
* } | ||
*/ | ||
export type MessageHandlerFunc< | ||
T extends Message = Message | ||
> = (message: T, context: MessageHandlerContext) => Promise<void>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
|
||
export type TypePredicate<T> = (data: unknown) => data is T; |