Skip to content
This repository has been archived by the owner on Nov 6, 2019. It is now read-only.

Commit

Permalink
allow a message hook to also be an object
Browse files Browse the repository at this point in the history
  • Loading branch information
sccolbert committed Feb 6, 2017
1 parent f8f118d commit 5f6f882
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions src/core/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,21 +174,14 @@ interface IMessageHandler {


/**
* A function which intercepts messages sent to a message handler.
*
* @param handler - The target handler of the message.
*
* @param msg - The message to be sent to the handler.
*
* @returns `true` if the message should continue to be processed
* as normal, or `false` if processing should cease immediately.
* An object which intercepts messages sent to a message handler.
*
* #### Notes
* A message hook is useful for intercepting or spying on messages
* sent to message handlers which were either not created by the
* consumer, or when subclassing the handler is not feasible.
*
* If the function returns `false`, no other message hooks will be
* If `messageHook` returns `false`, no other message hooks will be
* invoked and the message will not be delivered to the handler.
*
* If all installed message hooks return `true`, the message will
Expand All @@ -197,7 +190,30 @@ interface IMessageHandler {
* **See also:** [[installMessageHook]] and [[removeMessageHook]]
*/
export
type MessageHook = (handler: IMessageHandler, msg: Message) => boolean;
interface IMessageHook {
/**
* Intercept a message sent to a message handler.
*
* @param handler - The target handler of the message.
*
* @param msg - The message to be sent to the handler.
*
* @returns `true` if the message should continue to be processed
* as normal, or `false` if processing should cease immediately.
*/
messageHook(handler: IMessageHandler, msg: Message): boolean;
}


/**
* A type alias for message hook object or function.
*
* #### Notes
* The signature and semantics of a message hook function are the same
* as the `messageHook` method of [[IMessageHook]].
*/
export
type MessageHook = IMessageHook | ((handler: IMessageHandler, msg: Message) => boolean);


/**
Expand Down Expand Up @@ -471,7 +487,11 @@ namespace MessageLoop {
function invokeHook(hook: MessageHook, handler: IMessageHandler, msg: Message): boolean {
let result: boolean;
try {
result = hook(handler, msg);
if (typeof hook === 'function') {
result = hook(handler, msg);
} else {
result = hook.messageHook(handler, msg);
}
} catch (err) {
result = true;
console.error(err);
Expand Down

0 comments on commit 5f6f882

Please sign in to comment.