Skip to content

Commit 821017f

Browse files
authored
Merge pull request #254 from underctrl-io/refactor
refactor: replace StopEventPropagationError with CommandKitErrorCodes for event handling and reorganize signal imports
2 parents 3b08681 + f9c5abc commit 821017f

File tree

9 files changed

+39
-42
lines changed

9 files changed

+39
-42
lines changed

packages/commandkit/src/analytics/analytics-engine.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ export class AnalyticsEngine {
5151
* @param provider The analytics provider to register.
5252
*/
5353
public registerProvider(provider: AnalyticsProvider) {
54-
warnUnstable('analytics');
5554
this.#provider = provider;
5655
}
5756

packages/commandkit/src/app/commands/Context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
} from './MessageCommandParser';
1919
import { CommandKitEnvironment } from '../../context/environment';
2020
import { GenericFunction, getContext } from '../../context/async-context';
21-
import { exitMiddleware, redirect } from '../middleware/signals';
21+
import { exitMiddleware, redirect } from '../interrupt/signals';
2222
import {
2323
LoadedCommand,
2424
ResolvableCommand,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { CommandKit } from '../../CommandKit';
33
import { ListenerFunction } from '../../events/CommandKitEventsChannel';
44
import { Logger } from '../../logger/Logger';
55
import { toFileURL } from '../../utils/resolve-file-url';
6-
import { StopEventPropagationError } from '../../utils/utilities';
76
import { runInEventWorkerContext } from '../events/EventWorkerContext';
87
import { ParsedEvent } from '../router';
98
import { CommandKitEventDispatch } from '../../plugins';
9+
import { CommandKitErrorCodes, isErrorType } from '../../utils/error-codes';
1010

1111
/**
1212
* Represents an event listener with its configuration.
@@ -207,7 +207,7 @@ export class AppEventsHandler {
207207
await listener.handler(...args);
208208
} catch (e) {
209209
// Check if this is a stop propagation signal
210-
if (e instanceof StopEventPropagationError) {
210+
if (isErrorType(e, CommandKitErrorCodes.StopEvents)) {
211211
Logger.debug(
212212
`Event propagation stopped for ${name}${
213213
namespace ? ` of namespace ${namespace}` : ''
@@ -274,7 +274,7 @@ export class AppEventsHandler {
274274
executedOnceListeners.add(listener.handler);
275275
} catch (e) {
276276
// Check if this is a stop propagation signal
277-
if (e instanceof StopEventPropagationError) {
277+
if (isErrorType(e, CommandKitErrorCodes.StopEvents)) {
278278
Logger.debug(
279279
`Event propagation stopped for ${name}${
280280
namespace ? ` of namespace ${namespace}` : ''

packages/commandkit/src/app/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export * from './handlers/AppCommandHandler';
22
export * from './commands/Context';
33
export * from './commands/MessageCommandParser';
4-
export * from './middleware/signals';
4+
export * from './interrupt/signals';
55
export * from './register/CommandRegistrar';
66

77
export {

packages/commandkit/src/app/middleware/signals.ts renamed to packages/commandkit/src/app/interrupt/signals.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
createCommandKitError,
44
isCommandKitError,
55
} from '../../utils/error-codes';
6+
import { eventWorkerContext } from '../events/EventWorkerContext';
67

78
/**
89
* Cancel upcoming middleware execution.
@@ -17,6 +18,14 @@ export function exitMiddleware(): never {
1718
/**
1819
* Rethrow the error if it is a CommandKit error.
1920
* @param error The error to rethrow.
21+
* @example try {
22+
* doSomething();
23+
* } catch(e) {
24+
* // do something
25+
*
26+
* // throw the error if it's a commandkit error
27+
* rethrow(e)
28+
* }
2029
*/
2130
export function rethrow(error: unknown): void {
2231
if (isCommandKitError(error)) throw error;
@@ -28,3 +37,23 @@ export function rethrow(error: unknown): void {
2837
export function redirect(): never {
2938
throw createCommandKitError(CommandKitErrorCodes.ForwardedCommand);
3039
}
40+
41+
/**
42+
* Stops event propagation. This function should be called inside an event handler
43+
* to prevent further event handling.
44+
* @example // src/app/events/messageCreate/handler.ts
45+
* import { stopEvents } from 'commandkit';
46+
*
47+
* export default async function messageCreateHandler() {
48+
* console.log('Message created');
49+
* // Stop further event propagation
50+
* stopEvents();
51+
* }
52+
*/
53+
export function stopEvents(): never {
54+
if (!eventWorkerContext.getStore()) {
55+
throw new Error('stopEvents() may only be called inside an event handler');
56+
}
57+
58+
throw createCommandKitError(CommandKitErrorCodes.StopEvents);
59+
}

packages/commandkit/src/components/common/element.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ export interface FragmentElementProps {
8282
export function Fragment(
8383
props: FragmentElementProps,
8484
): CommandKitElementData[ElementType][] {
85-
warnUnstable('CommandKit JSX');
86-
8785
return Array.isArray(props.children)
8886
? props.children.flat()
8987
: [props.children];
@@ -101,7 +99,6 @@ export function createElement(
10199
props: Record<string, unknown>,
102100
...children: any[]
103101
): CommandKitElement<ElementType> {
104-
warnUnstable('CommandKit JSX');
105102
return type({ ...props, children: props.children ?? children });
106103
}
107104

packages/commandkit/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ export {
2121
getSourceDirectories,
2222
devOnly,
2323
debounce,
24-
stopEvents,
25-
StopEventPropagationError,
2624
defer,
2725
} from './utils/utilities';
26+
export * from './app/interrupt/signals';
2827
export type { CommandKitHMREvent } from './utils/dev-hooks';
2928
export * from './utils/constants';
3029
export * from './app/events/EventWorkerContext';

packages/commandkit/src/utils/error-codes.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export const CommandKitErrorCodes = {
1818
* Error code for plugin capture handle.
1919
*/
2020
PluginCaptureHandle: Symbol('kPluginCaptureHandle'),
21+
/**
22+
* Error code for event interruption signal
23+
*/
24+
StopEvents: Symbol('kStopEvents'),
2125
} as const;
2226

2327
/**

packages/commandkit/src/utils/utilities.ts

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -166,37 +166,6 @@ export function devOnly<T extends (...args: any[]) => any>(fn: T): T {
166166
return f as T;
167167
}
168168

169-
/**
170-
* Custom error for stopping event propagation.
171-
*/
172-
export class StopEventPropagationError extends Error {
173-
constructor() {
174-
super('Event propagation stopped');
175-
this.name = 'StopEventPropagationError';
176-
}
177-
}
178-
179-
/**
180-
* Stops event propagation. This function should be called inside an event handler
181-
* to prevent further event handling.
182-
* @throws {StopEventPropagationError}
183-
* @example // src/app/events/messageCreate/handler.ts
184-
* import { stopEvents } from 'commandkit';
185-
*
186-
* export default async function messageCreateHandler() {
187-
* console.log('Message created');
188-
* // Stop further event propagation
189-
* stopEvents();
190-
* }
191-
*/
192-
export function stopEvents(): never {
193-
if (!eventWorkerContext.getStore()) {
194-
throw new Error('stopEvents() may only be called inside an event handler');
195-
}
196-
197-
throw new StopEventPropagationError();
198-
}
199-
200169
/**
201170
* Represents a simple proxy object that mirrors a target object.
202171
*/

0 commit comments

Comments
 (0)