-
Notifications
You must be signed in to change notification settings - Fork 6
feat: enable plugin mechanism for async func #70
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
Conversation
Signed-off-by: yad <459647480@qq.com>
Thanks for creating a pull request! A maintainer will review your changes shortly. Please don't be discouraged if it takes a while. |
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.
Seems there would be some structural level code refactoring, so let me put all my comments at below trying to give you a whole picture.
Below items are listed in priority, from the most to least important.
-
Where comes the
PLUGIN_CONTEXT
?- Taking context_test.go for reference, all of the context data are stored in
FUNC_CONTEXT
. - Accordingly,
plugin_cotext.ts
is NO need to exist, changes inoptions.ts
is redundant, simply adding some plugin related fields inOpenFunctionCotext
is good enough. (Check Context structure in plugin mechanism for reference as well) - Nevertheless, we need some
Plugin
class to deal with plugin instance so as to hold data and provide instance methods likeget
shown in this demo.
- Taking context_test.go for reference, all of the context data are stored in
-
How to store plugins?
- A map structure is already an good structure to hold and access both plugin name as key and function instance as value, so why we have to need
prePlugins
/prePluginFuncs
and the like to hold keys and values separately? - BTW, in ts, Record is a more standard type to hold objects, rather than
Map
.
- A map structure is already an good structure to hold and access both plugin name as key and function instance as value, so why we have to need
-
Try to take more advantage of well-known Node.js libraries
console.error
should be replaced bydebug
(could search usage in files)for (let)
loop should be replaced by Lodash collection utilities which will help ensure the safe execution and returns
-
Finally, pay a little attention to import sequence, usually we group imports from the same level together, and common sequence is the farther the earlier. For example, node built-in libs -> 3rd-party libs -> files in grandparent folder -> files in parent folder -> side-by-side files, etc.
Signed-off-by: yad <459647480@qq.com>
…unctions-framework-nodejs into feature/plugin-async
Signed-off-by: yad <459647480@qq.com>
Im refactor code, but lodash collection utilities is aysnc ,so I still use for loop to ensure plugin hook sync ; |
Oh, you are right, may try |
Signed-off-by: yad <459647480@qq.com>
Signed-off-by: yad <459647480@qq.com>
improve the code & add unit test |
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.
@YADROOKIE I'm thinking why shouldn't we fallback all methods for a user plugin function, so that no need to do lots of fields & methods checking?
It looks better to attach default get
/ execXXX
methods to plugin function prototype instead rudely telling user you must have blablabla otherwise your function wouldn't work. What do you think? :)
src/loader.ts
Outdated
@@ -92,6 +95,7 @@ export async function getUserFunction( | |||
} | null> { | |||
try { | |||
const functionModulePath = getFunctionModulePath(codeLocation); | |||
console.log(functionModulePath); |
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.
Don't forget to remove console.log
, or use debug
instead.
src/options.ts
Outdated
@@ -134,7 +134,6 @@ const FunctionContextOption = new ConfigurableOption( | |||
} | |||
} | |||
); | |||
|
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.
Why remove this line?
src/loader.ts
Outdated
if ( | ||
options.context && | ||
options.context.prePlugins && | ||
options.context.postPlugins |
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.
- Take better advantage of optional chaining, i.e.
options.context?.postPlugins
- Why we should ensure both pre and post plugins existed? Seems it's better to make another helper function to encapsulate common logics and handle pre and post plugins separately.
src/loader.ts
Outdated
options.context.prePlugins && | ||
options.context.postPlugins | ||
) { | ||
options.context.prePlugins.forEach(item => { |
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.
As mentioned in our last call, use lodash
utilities instead to ensure safe invocation and returns.
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.
Use plugin
instead of item
, that's more meaningful.
src/loader.ts
Outdated
|
||
try { | ||
// load plugin js files | ||
const instances: Map<string, Plugin> = new Map(); |
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.
Why not simply using {}
instead of Map
?
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.
It can clear type
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.
It can clear type
So why not using Record
?
test/data/plugins/errorMissGet.js
Outdated
static Name = "error-miss-get-plugin" | ||
|
||
execPreHook(ctx){ | ||
console.log(`-----------error plugin pre hook-----------`) |
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.
Please make sure or indent contains only 2 spaces.
test/data/plugins/plugindemo.js
Outdated
@@ -0,0 +1,33 @@ | |||
const { resolve } = require("path") |
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.
Use import
instead of require
, let's keep the same coding style.
test/data/plugins/plugindemo.js
Outdated
constructor(){ | ||
console.log(`init demo plugins`) | ||
} | ||
sleep(){ |
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.
Better to make it as a helper function out of the class.
test/data/plugins/plugindemo.js
Outdated
async execPreHook(ctx){ | ||
console.log(`-----------demo plugin pre hook-----------`) | ||
ctx['pre'] = 'pre-exec'; | ||
await this.sleep() |
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.
Please ensure all sentences in the project ends with a semicolon.
import {FrameworkOptions} from '../../src/options'; | ||
import assert = require('assert'); | ||
|
||
const TEST_CONTEXT: OpenFunctionContext = { |
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.
Too much alike with async_server
, it's better to combine the test cases to make better use of the common data and logics.
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.
At first I was combine the test cases with async_server ,but it will conflict make it single file finally
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.
In this case, it's better to extract common test data into a separate file for reuse.
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.
it seem still have conflict in when data in a common file
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.
it seem still have conflict in when data in a common file
Please describe the problem or details, it's hard to provide help based on "it seems".
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 was in the wrong position before , it is ok now
Signed-off-by: yad <459647480@qq.com>
Signed-off-by: yad <459647480@qq.com>
…d replace Map Signed-off-by: yad <459647480@qq.com>
Signed-off-by: yad <459647480@qq.com>
Signed-off-by: yad <459647480@qq.com>
Signed-off-by: yad <459647480@qq.com>
Signed-off-by: yad <459647480@qq.com>
Signed-off-by: yad <459647480@qq.com>
Signed-off-by: yad <459647480@qq.com>
Signed-off-by: yad <459647480@qq.com>
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.
Generally speaking, problems addressed in the latest review have been resolved, so it's okay to make a merge.
Signed-off-by: yad <459647480@qq.com>
@all-contributors please add @YADROOKIE for code contribution |
I've put up a pull request to add @YADROOKIE! 🎉 |
complete async function plugin
Signed-off-by: yad 459647480@qq.com