Skip to content

Conversation

ejizba
Copy link
Contributor

@ejizba ejizba commented Mar 1, 2022

Fixes #522. Importantly, this PR does not address all possible hook-related features, but it should allow for them in the future (hopefully near future). See that issue thread for more details on that discussion.

Sample usage:

import { registerHook } from '@azure/functions-core';

registerHook('preInvocation', (context: PreInvocationContext) => {
    // can completely change the inputs passed to a user's function
    context.inputs = ['hello'];
    // can write to `context.hookData` and read the value in a postInvocationHook
    context.hookData.value = 1;
    // can write to the _invocation_ context passed to a user's function
    context.invocationContext.dataForInvoc = 2;
});

const disposable = registerHook('postInvocation', (context: PostInvocationContext) => {
    // can read data from `context.hookData` set in the preInvocationHook
    if (context.hookData.value === 1) {
    }
    // can modify the error thrown by the function
    if (context.error) {
        context.error = new Error(`Add a prefix to error text: ${context.error.message}`);
    }
    // can completely change the result of a function
    context.result = 'world';
});

disposable.dispose(); // Can stop the hook from executing in the future

See workerTypes/index.d.ts for more details/docs. See here for why I picked the name I did and how I see this built-in module working in the new programming model. Ultimately, though, we could easily rename this module and do something completely different in the future.

@ejizba ejizba requested review from alrod and hossam-nasr March 1, 2022 01:21
Comment on lines 13 to 14
private _preInvocationHooks: HookCallback[] = [];
private _postInvocationHooks: HookCallback[] = [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the interest of type safety, I would support adding separate types for pre and post invocation hook callbacks, and reflecting that here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the separate types, but I ran into too many build/lint errors if I tried to reference them here. If you can come up with a simple/clean way to do that let me know, otherwise I don't think this is worth the effort

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I gave it a try but I couldn't get them to work either, without having to use different functions to retrieve/execute each hook. I guess it's another argument for having separate functions 😋

Copy link
Contributor Author

@ejizba ejizba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Hossam! Most of your comments are topics I've debated myself, so I'm glad to be ironing them out with someone else now 🙂

Base automatically changed from ej/async to v3.x March 11, 2022 01:01
@ejizba
Copy link
Contributor Author

ejizba commented Mar 14, 2022

Met with the app insights team today. Overall looks good to them, but we need to add the function callback to the hooks api somehow. Filed #553 to address separately

@ejizba ejizba merged commit 3c9383f into v3.x Mar 24, 2022
@ejizba ejizba deleted the ej/hooks2 branch March 24, 2022 00:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide function execution hooks

3 participants