Skip to content

Commit

Permalink
fix: action handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
izatop committed Jul 20, 2022
1 parent dc04f6e commit 0c59410
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/app/src/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class Application<C extends Context> {
return this.#unit.run(route.action, freezedState);
}

public on(handlers: ActionTransactionHandlers): void {
public on(handlers: ActionTransactionHandlers<C>): void {
this.#unit.on(handlers);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/Commander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class Commander<C extends CommandContext> {
public static async execute<C extends CommandContext>(
context: ContextArg<C>,
routes: IRoute<Action<C, any, IRunnable | void>>[] = [],
handlers: ActionTransactionHandlers = {}): Promise<IRunnable | void> {
handlers: ActionTransactionHandlers<C> = {}): Promise<IRunnable | void> {
const command = new this<C>(await Application.factory<C>(context, routes));
command.#application.on(handlers);

Expand Down
8 changes: 4 additions & 4 deletions packages/unit/src/Unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class Unit<C extends Context> {
protected readonly logger!: Logger;

readonly #context: ApplyContext<C>;
readonly #handlers: ActionTransactionHandlers = {};
readonly #handlers: ActionTransactionHandlers<C> = {};

protected constructor(context: ApplyContext<C>) {
this.#context = context;
Expand Down Expand Up @@ -46,7 +46,7 @@ export class Unit<C extends Context> {
return Context.apply(syncContext);
}

public on(handlers: ActionTransactionHandlers): void {
public on(handlers: ActionTransactionHandlers<C>): void {
Object.assign(this.#handlers, handlers);
}

Expand Down Expand Up @@ -75,13 +75,13 @@ export class Unit<C extends Context> {

private watch<T>(action: string, run: () => Promise<T> | T): Promise<T> {
const {start, error} = this.#handlers;
const finish = start?.(action);
const finish = start?.(action, this.context);

return Promise.resolve(run())
.finally(finish)
.catch((reason) => {
this.logger.error(toError(reason, "Unexpected error").message, reason);
error?.(reason);
error?.(reason, this.context);

throw reason;
});
Expand Down
6 changes: 3 additions & 3 deletions packages/unit/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface IShadowState<T> {
getShadowState(): T;
}

export type ActionTransactionHandlers = {
start?: (action: string) => () => void;
error?: (reason: unknown) => void;
export type ActionTransactionHandlers<C> = {
start?: (action: string, context: ApplyContext<C>) => () => void;
error?: (reason: unknown, context: ApplyContext<C>) => void;
};

0 comments on commit 0c59410

Please sign in to comment.