Skip to content

Commit 7f9ebe0

Browse files
committed
✨ feat(core): let inspectors able to return a callback function
1 parent 6cad06b commit 7f9ebe0

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

packages/core/src/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export type InspectorContext<C extends Commands = Commands> = HandlerContext<C>
5656
flags: FallbackType<TypeFlag<NonNullable<C[keyof C]["flags"]>>["flags"], Dict<any>>
5757
};
5858
export type Inspector<C extends Commands = Commands> = InspectorFn<C> | InspectorObject<C>;
59-
export type InspectorFn<C extends Commands = Commands> = (ctx: InspectorContext<C>, next: () => void) => void;
59+
export type InspectorFn<C extends Commands = Commands> = (ctx: InspectorContext<C>, next: () => void) => (() => void) | void;
6060
export interface InspectorObject<C extends Commands = Commands> {
6161
enforce?: "pre" | "post"
6262
fn: InspectorFn<C>

packages/core/src/utils.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,19 @@ export function compose(inspectors: Inspector[]) {
126126
...inspectorMap.normal,
127127
...inspectorMap.post,
128128
];
129+
129130
return (getCtx: () => InspectorContext) => {
130-
return dispatch(0);
131+
const callbacks: (() => void)[] = [];
132+
dispatch(0);
133+
for (let i = callbacks.length - 1; i >= 0; i--) {
134+
callbacks[i]();
135+
}
131136
function dispatch(i: number): void {
132137
const inspector = mergedInspectorFns[i];
133-
return inspector(getCtx(), dispatch.bind(null, i + 1));
138+
const cb = inspector(getCtx(), dispatch.bind(null, i + 1));
139+
if (cb) {
140+
callbacks.push(cb);
141+
}
134142
}
135143
};
136144
}

0 commit comments

Comments
 (0)