-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
feat(singleton): support async create function #2382
Conversation
@@ -48,10 +52,23 @@ class Singleton { | |||
} | |||
|
|||
createInstance(config) { | |||
// async creator only support createInstanceAsync | |||
assert(!is.asyncFunction(this.create), |
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.
有一个可以讨论的地方,其实可以判断 this.create 来决定 createInstance 方法是返回创建后的实例还是返回一个 promise,这样就不需要 createInstanceAsync 方法了,但是 createInstance 方法就不统一了,也不好。所以我倾向于新增一个 createInstanceAsync 方法来应对这种特殊场景。
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.
主要还是兼容问题,如果没有问题我倾向于 createInstance 就是一个 async function
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.
就是因为有兼容问题才这样的,createInstance 不能直接返回一个 async function
lib/egg.js
Outdated
*/ | ||
addSingleton(name, create) { | ||
const options = {}; | ||
options.name = name; | ||
options.create = create; | ||
options.app = this; | ||
const singleton = new Singleton(options); | ||
singleton.init(); | ||
const done = this.readyCallback(`singleton:${name}`); |
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 beforeStart?
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.
改了
@@ -48,10 +52,23 @@ class Singleton { | |||
} | |||
|
|||
createInstance(config) { | |||
// async creator only support createInstanceAsync | |||
assert(!is.asyncFunction(this.create), |
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.
主要还是兼容问题,如果没有问题我倾向于 createInstance 就是一个 async function
Codecov Report
@@ Coverage Diff @@
## master #2382 +/- ##
==========================================
+ Coverage 99.61% 99.61% +<.01%
==========================================
Files 29 29
Lines 773 783 +10
==========================================
+ Hits 770 780 +10
Misses 3 3
Continue to review full report at Codecov.
|
合并了,后面补一下文档,这个就不 pick 到 1.x 了 |
feat(singleton): support async create function (#2382)
Checklist
npm test
passesAffected core subsystem(s)
Description of change
config.client
to be async functionsingleton.createInstanceAsync
when create method is async function