Uh oh!
There was an error while loading. Please reload this page.
feat: enable plugin mechanism for async func - #70
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.
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.tsis NO need to exist, changes inoptions.tsis redundant, simply adding some plugin related fields inOpenFunctionCotextis good enough. (Check Context structure in plugin mechanism for reference as well) - Nevertheless, we need some
Pluginclass to deal with plugin instance so as to hold data and provide instance methods likegetshown 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/prePluginFuncsand 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.errorshould 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.
YADROOKIE
commented
Jul 21, 2022
Signed-off-by: yad <459647480@qq.com>
…unctions-framework-nodejs into feature/plugin-async
Signed-off-by: yad <459647480@qq.com>
YADROOKIE
commented
Jul 22, 2022
Im refactor code, but lodash collection utilities is aysnc ,so I still use for loop to ensure plugin hook sync ; |
webup
commented
Jul 22, 2022
Oh, you are right, may try |
Signed-off-by: yad <459647480@qq.com>
Signed-off-by: yad <459647480@qq.com>
YADROOKIE
commented
Jul 25, 2022
improve the code & add unit test |
webup
left a comment
There was a problem hiding this comment.
@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? :)
| } | null> { | ||
| try { | ||
| const functionModulePath = getFunctionModulePath(codeLocation); | ||
| console.log(functionModulePath); |
There was a problem hiding this comment.
Don't forget to remove console.log, or use debug instead.
| } | ||
| } | ||
| ); | ||
| if ( | ||
| options.context && | ||
| options.context.prePlugins && | ||
| options.context.postPlugins |
There was a problem hiding this comment.
- 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.
| options.context.prePlugins && | ||
| options.context.postPlugins | ||
| ) { | ||
| options.context.prePlugins.forEach(item => { |
There was a problem hiding this comment.
As mentioned in our last call, use lodash utilities instead to ensure safe invocation and returns.
There was a problem hiding this comment.
Use plugin instead of item, that's more meaningful.
| try { | ||
| // load plugin js files | ||
| const instances: Map<string, Plugin> = new Map(); |
There was a problem hiding this comment.
Why not simply using {} instead of Map?
There was a problem hiding this comment.
It can clear type
So why not using Record?
| static Name = "error-miss-get-plugin" | ||
| execPreHook(ctx){ | ||
| console.log(`-----------error plugin pre hook-----------`) |
There was a problem hiding this comment.
Please make sure or indent contains only 2 spaces.
| @@ -0,0 +1,33 @@ | |||
| const { resolve } = require("path") | |||
There was a problem hiding this comment.
Use import instead of require, let's keep the same coding style.
| constructor(){ | ||
| console.log(`init demo plugins`) | ||
| } | ||
| sleep(){ |
There was a problem hiding this comment.
Better to make it as a helper function out of the class.
| async execPreHook(ctx){ | ||
| console.log(`-----------demo plugin pre hook-----------`) | ||
| ctx['pre'] = 'pre-exec'; | ||
| await this.sleep() |
There was a problem hiding this comment.
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.
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.
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.
In this case, it's better to extract common test data into a separate file for reuse.
There was a problem hiding this comment.
it seem still have conflict in when data in a common file
There was a problem hiding this comment.
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.
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>
webup
left a comment
There was a problem hiding this comment.
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>
webup
commented
Aug 4, 2022
@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