-
Notifications
You must be signed in to change notification settings - Fork 19
Add basic hooks support (v4) #115
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import * as types from '@azure/functions'; | ||
import { AppStartContextInit } from '@azure/functions'; | ||
import { HookContext } from './HookContext'; | ||
|
||
export class AppStartContext extends HookContext implements types.AppStartContext { | ||
functionAppDirectory: string; | ||
|
||
constructor(init?: AppStartContextInit) { | ||
super(init); | ||
init = init || {}; | ||
this.functionAppDirectory = init.functionAppDirectory || 'unknown'; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import * as types from '@azure/functions'; | ||
import { AppTerminateContextInit } from '@azure/functions'; | ||
import { HookContext } from './HookContext'; | ||
|
||
export class AppTerminateContext extends HookContext implements types.AppTerminateContext { | ||
constructor(init?: AppTerminateContextInit) { | ||
super(init); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
/** | ||
* Based off of the Node worker | ||
* https://github.com/Azure/azure-functions-nodejs-worker/blob/bf28d9c5ad4ed22c5e42c082471d16108abee140/src/Disposable.ts | ||
*/ | ||
export class Disposable { | ||
static from(...inDisposables: { dispose(): any }[]): Disposable { | ||
let disposables: ReadonlyArray<{ dispose(): any }> | undefined = inDisposables; | ||
return new Disposable(function () { | ||
if (disposables) { | ||
for (const disposable of disposables) { | ||
if (disposable && typeof disposable.dispose === 'function') { | ||
disposable.dispose(); | ||
} | ||
} | ||
disposables = undefined; | ||
} | ||
}); | ||
} | ||
|
||
#callOnDispose?: () => any; | ||
|
||
constructor(callOnDispose: () => any) { | ||
this.#callOnDispose = callOnDispose; | ||
} | ||
|
||
dispose(): any { | ||
if (this.#callOnDispose instanceof Function) { | ||
this.#callOnDispose(); | ||
this.#callOnDispose = undefined; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import * as types from '@azure/functions'; | ||
import { HookContextInit, HookData } from '@azure/functions'; | ||
|
||
export class HookContext implements types.HookContext { | ||
#hookData: HookData; | ||
#appHookData: HookData; | ||
|
||
constructor(init?: HookContextInit) { | ||
init = init || {}; | ||
|
||
// It's important to use the original objects passed in init (no copies or clones) | ||
// because modifications to these objects are persisted by the worker | ||
this.#hookData = init.hookData || {}; | ||
this.#appHookData = init.appHookData || {}; | ||
} | ||
|
||
get(propertyName: string): unknown { | ||
return this.#hookData[propertyName]; | ||
} | ||
|
||
set(propertyName: string, value: unknown): void { | ||
this.#hookData[propertyName] = value; | ||
} | ||
|
||
getGlobal(propertyName: string): unknown { | ||
return this.#appHookData[propertyName]; | ||
} | ||
|
||
setGlobal(propertyName: string, value: unknown): void { | ||
this.#appHookData[propertyName] = value; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import * as types from '@azure/functions'; | ||
import { InvocationContext } from '../InvocationContext'; | ||
import { ReadOnlyError } from '../errors'; | ||
import { HookContext } from './HookContext'; | ||
|
||
export abstract class InvocationHookContext extends HookContext implements types.InvocationHookContext { | ||
#invocationContext: types.InvocationContext; | ||
|
||
constructor(init?: types.InvocationHookContextInit) { | ||
super(init); | ||
init = init || {}; | ||
this.#invocationContext = init.invocationContext || new InvocationContext({}); | ||
} | ||
|
||
get invocationContext(): types.InvocationContext { | ||
return this.#invocationContext; | ||
} | ||
|
||
set invocationContext(_value: types.InvocationContext) { | ||
throw new ReadOnlyError('invocationContext'); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import * as types from '@azure/functions'; | ||
import { PostInvocationCoreContext } from '@azure/functions'; | ||
import { InvocationHookContext } from './InvocationHookContext'; | ||
|
||
export class PostInvocationContext extends InvocationHookContext implements types.PostInvocationContext { | ||
#coreCtx: PostInvocationCoreContext; | ||
|
||
constructor(init?: types.PostInvocationContextInit) { | ||
super(init); | ||
init = init || {}; | ||
if (!init.coreContext) { | ||
init.coreContext = { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
result: typeof init.result === 'undefined' ? null : init.errorResult, | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
error: typeof init.errorResult === 'undefined' ? null : init.errorResult, | ||
inputs: init.args || [], | ||
}; | ||
} | ||
|
||
this.#coreCtx = init.coreContext; | ||
} | ||
|
||
get args(): any[] { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return this.#coreCtx.inputs; | ||
} | ||
|
||
set args(value: any[]) { | ||
// it's important to use the core context inputs | ||
// since changes to this array are persisted by the worker | ||
this.#coreCtx.inputs = value; | ||
} | ||
|
||
get result(): any { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return this.#coreCtx.result; | ||
} | ||
|
||
set result(value: any) { | ||
// it's important to use the core context result | ||
// since changes to this value are persisted by the worker | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
this.#coreCtx.result = value; | ||
} | ||
|
||
get errorResult(): any { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return this.#coreCtx.error; | ||
} | ||
|
||
set errorResult(value: any) { | ||
// it's important to use the core context result | ||
// since changes to this value are persisted by the worker | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
this.#coreCtx.error = value; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import * as types from '@azure/functions'; | ||
import { FunctionHandler, PreInvocationContextInit, PreInvocationCoreContext } from '@azure/functions'; | ||
import { InvocationHookContext } from './InvocationHookContext'; | ||
|
||
export class PreInvocationContext extends InvocationHookContext implements types.PreInvocationContext { | ||
#coreCtx: PreInvocationCoreContext; | ||
|
||
constructor(init?: PreInvocationContextInit) { | ||
super(init); | ||
init = init || {}; | ||
if (!init.coreContext) { | ||
init.coreContext = { | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
functionCallback: init.functionHandler || (() => {}), | ||
inputs: init.args || [], | ||
}; | ||
} | ||
this.#coreCtx = init.coreContext; | ||
} | ||
|
||
get functionHandler(): FunctionHandler { | ||
return this.#coreCtx.functionCallback; | ||
} | ||
|
||
set functionHandler(value: FunctionHandler) { | ||
// it's important to use the core context functionCallback | ||
// since changes to this value are persisted by the worker | ||
this.#coreCtx.functionCallback = value; | ||
} | ||
|
||
get args(): any[] { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return this.#coreCtx.inputs; | ||
} | ||
|
||
set args(value: any[]) { | ||
// it's important to use the core context inputs | ||
// since changes to this array are persisted by the worker | ||
this.#coreCtx.inputs = value; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm leaning towards this organization:
I'm not a big of "on". I feel like we could've easily done that for the trigger registrations (
app.onHttp
,app.onTimer
), and I don't want to be inconsistent and start using "on" for hooks. That being said, I still like grouping all the hooks together to make the Intellisense cleaner. Rather than group them with the "on" prefix, I think a more explicit "hook" group would make sense.It's a bit weird to have
hook
onapp
wheninput
/output
/trigger
aren't onapp
, but I feel like it's okay because the latter don't actually register anything.