Skip to content

Commit f38ba1f

Browse files
committed
feat: add a way to stop event propagation
1 parent d6bfb72 commit f38ba1f

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

packages/commandkit/src/app/handlers/AppEventsHandler.ts

+23
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { CommandKit } from '../../CommandKit';
22
import { ListenerFunction } from '../../events/CommandKitEventsChannel';
33
import { Logger } from '../../logger/Logger';
44
import { toFileURL } from '../../utils/resolve-file-url';
5+
import { StopEventPropagationError } from '../../utils/utilities';
56
import { ParsedEvent } from '../router';
67

78
export type EventListener = {
@@ -107,6 +108,17 @@ export class AppEventsHandler {
107108
try {
108109
await listener.handler(...args);
109110
} catch (e) {
111+
// Check if this is a stop propagation signal
112+
if (e instanceof StopEventPropagationError) {
113+
Logger.debug(
114+
`Event propagation stopped for ${name}${
115+
namespace ? ` of namespace ${namespace}` : ''
116+
}`,
117+
);
118+
break; // Stop executing remaining listeners
119+
}
120+
121+
// Otherwise log the error as usual
110122
Logger.error(
111123
`Error handling event ${name}${
112124
namespace ? ` of namespace ${namespace}` : ''
@@ -127,6 +139,17 @@ export class AppEventsHandler {
127139
await listener.handler(...args);
128140
executedOnceListeners.add(listener.handler);
129141
} catch (e) {
142+
// Check if this is a stop propagation signal
143+
if (e instanceof StopEventPropagationError) {
144+
Logger.debug(
145+
`Event propagation stopped for ${name}${
146+
namespace ? ` of namespace ${namespace}` : ''
147+
}`,
148+
);
149+
break; // Stop executing remaining listeners
150+
}
151+
152+
// Otherwise log the error as usual
130153
Logger.error(
131154
`Error handling event ${name}${
132155
namespace ? ` of namespace ${namespace}` : ''

packages/commandkit/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export {
2121
getSourceDirectories,
2222
devOnly,
2323
debounce,
24+
stopEvents,
25+
StopEventPropagationError,
2426
} from './utils/utilities';
2527
export type { CommandKitHMREvent } from './utils/dev-hooks';
2628
export * from './utils/constants';

packages/commandkit/src/utils/utilities.ts

+18
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,21 @@ export function devOnly<T extends (...args: any[]) => any>(fn: T): T {
110110

111111
return f as T;
112112
}
113+
114+
/**
115+
* Custom error for stopping event propagation.
116+
*/
117+
export class StopEventPropagationError extends Error {
118+
constructor() {
119+
super('Event propagation stopped');
120+
this.name = 'StopEventPropagationError';
121+
}
122+
}
123+
124+
/**
125+
* Stops event propagation.
126+
* @throws {StopEventPropagationError}
127+
*/
128+
export function stopEvents(): never {
129+
throw new StopEventPropagationError();
130+
}

0 commit comments

Comments
 (0)