Plugins in imessage-kit operate in three distinct dispatch modes depending on the hook being used. This determines how errors are handled and whether a plugin can stop an operation.
1. Interrupting (Sequential, Fail-fast)
Hooks: onBeforeMessageQuery, onBeforeChatQuery, onBeforeSend.
- Behavior: Plugins run in order (
pre -> normal -> post). The first plugin to throw an error will abort the surrounding SDK operation (e.g., getMessages, listChats, or send). - Error Handling: Remaining plugins are not called. The caller receives an
IMessageError where the code is DATABASE for queries or SEND for sends, and the cause is the plugin's original error. - Use Case: Use these as gates for authentication, rate limiting, or content policy enforcement.
2. Sequential (Observing)
Hooks: onInit, onError, onDestroy.
- Behavior: Plugins run one at a time.
- Error Handling: If a plugin throws, the error is captured, logged, and reported to the
onError hook. The surrounding SDK lifecycle continues; a single plugin failure cannot crash the SDK.
3. Parallel (Observing)
Hooks: onAfterMessageQuery, onAfterChatQuery, onAfterSend, onIncomingMessage, onFromMe.
- Behavior: All matching plugins run concurrently, and their promises are awaited as a group.
- Error Handling: Individual failures are reported to
onError, but the query result or incoming message still propagates to the caller and other plugins.
Note: Hook return values are ignored. Plugins cannot rewrite requests or results; they can only observe or interrupt via throwing errors.