1 |
|
2 |
|
3 |
|
4 | import { AppStartHandler, AppTerminateHandler, PostInvocationHandler, PreInvocationHandler } from '@azure/functions';
|
5 | import * as coreTypes from '@azure/functions-core';
|
6 | import { Disposable } from '../utils/Disposable';
|
7 | import { tryGetCoreApiLazy } from '../utils/tryGetCoreApiLazy';
|
8 | import { AppStartContext } from './AppStartContext';
|
9 | import { AppTerminateContext } from './AppTerminateContext';
|
10 | import { PostInvocationContext } from './PostInvocationContext';
|
11 | import { PreInvocationContext } from './PreInvocationContext';
|
12 |
|
13 | function registerHook(hookName: string, callback: coreTypes.HookCallback): coreTypes.Disposable {
|
14 | const coreApi = tryGetCoreApiLazy();
|
15 | if (!coreApi) {
|
16 | console.warn(
|
17 | `WARNING: Skipping call to register ${hookName} hook because the "@azure/functions" package is in test mode.`
|
18 | );
|
19 | return new Disposable(() => {
|
20 | console.warn(
|
21 | `WARNING: Skipping call to dispose ${hookName} hook because the "@azure/functions" package is in test mode.`
|
22 | );
|
23 | });
|
24 | } else {
|
25 | return coreApi.registerHook(hookName, callback);
|
26 | }
|
27 | }
|
28 |
|
29 | export function appStart(handler: AppStartHandler): Disposable {
|
30 | return registerHook('appStart', (coreContext) => {
|
31 | return handler(new AppStartContext(coreContext));
|
32 | });
|
33 | }
|
34 |
|
35 | export function appTerminate(handler: AppTerminateHandler): Disposable {
|
36 | return registerHook('appTerminate', (coreContext) => {
|
37 | return handler(new AppTerminateContext(coreContext));
|
38 | });
|
39 | }
|
40 |
|
41 | export function preInvocation(handler: PreInvocationHandler): Disposable {
|
42 | return registerHook('preInvocation', (coreContext) => {
|
43 | return handler(new PreInvocationContext(coreContext));
|
44 | });
|
45 | }
|
46 |
|
47 | export function postInvocation(handler: PostInvocationHandler): Disposable {
|
48 | return registerHook('postInvocation', (coreContext) => {
|
49 | return handler(new PostInvocationContext(coreContext));
|
50 | });
|
51 | }
|