-
Notifications
You must be signed in to change notification settings - Fork 46
Add finally callback to customFunction #516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
279eccc
e402a8d
35ccd49
d94142d
fb599bb
ad26e41
c4a0589
f45d7ec
9effd84
a331963
074ca1d
33fea2d
56204d6
b20cfd1
c7525e7
66a347c
162cb25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -60,6 +60,11 @@ export type Mod< | |||||||||
) => | ||||||||||
| Promise<{ ctx: ModCtx; args: ModMadeArgs }> | ||||||||||
| { ctx: ModCtx; args: ModMadeArgs }; | ||||||||||
finally?: (params: { | ||||||||||
ctx: Ctx & ModCtx; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
result?: any; | ||||||||||
error?: any; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
}) => void | Promise<void>; | ||||||||||
}; | ||||||||||
|
||||||||||
/** | ||||||||||
|
@@ -88,6 +93,7 @@ export const NoOp = { | |||||||||
input() { | ||||||||||
return { args: {}, ctx: {} }; | ||||||||||
}, | ||||||||||
finally() {}, | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
}; | ||||||||||
|
||||||||||
/** | ||||||||||
|
@@ -339,7 +345,20 @@ function customFnBuilder( | |||||||||
pick(allArgs, Object.keys(inputArgs)) as any, | ||||||||||
); | ||||||||||
const args = omit(allArgs, Object.keys(inputArgs)); | ||||||||||
return handler({ ...ctx, ...added.ctx }, { ...args, ...added.args }); | ||||||||||
const finalCtx = { ...ctx, ...added.ctx }; | ||||||||||
let result; | ||||||||||
let error; | ||||||||||
try { | ||||||||||
result = await handler(finalCtx, { ...args, ...added.args }); | ||||||||||
return result; | ||||||||||
} catch (e) { | ||||||||||
error = e; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe just call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you didn't do it, please do it |
||||||||||
throw e; | ||||||||||
} finally { | ||||||||||
if (mod.finally) { | ||||||||||
await mod.finally({ ctx: finalCtx, result, error }); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
} | ||||||||||
} | ||||||||||
}, | ||||||||||
}); | ||||||||||
} | ||||||||||
|
@@ -353,7 +372,20 @@ function customFnBuilder( | |||||||||
returns: fn.returns, | ||||||||||
handler: async (ctx: any, args: any) => { | ||||||||||
const added = await inputMod(ctx, args); | ||||||||||
return handler({ ...ctx, ...added.ctx }, { ...args, ...added.args }); | ||||||||||
const finalCtx = { ...ctx, ...added.ctx }; | ||||||||||
let result; | ||||||||||
let error; | ||||||||||
try { | ||||||||||
result = await handler(finalCtx, { ...args, ...added.args }); | ||||||||||
return result; | ||||||||||
} catch (e) { | ||||||||||
error = e; | ||||||||||
throw e; | ||||||||||
} finally { | ||||||||||
if (mod.finally) { | ||||||||||
await mod.finally({ ctx: finalCtx, result, error }); | ||||||||||
} | ||||||||||
} | ||||||||||
}, | ||||||||||
}); | ||||||||||
}; | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is useless. It doesn't test the library at all. Replace it with one that defines a function and does
testApi
etc. to try calling it - and finding a clever way to spy on the handler. Maybe calling the resulting handler directly via(myfn as any)._handler(ctx, args)
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move the tests out of their own file into here and delete this useless test