-
Notifications
You must be signed in to change notification settings - Fork 506
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
worker code support Asynchronous injection #2016
Conversation
Codecov Report
❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more. @@ Coverage Diff @@
## master #2016 +/- ##
==========================================
- Coverage 83.97% 83.97% -0.01%
==========================================
Files 164 164
Lines 17847 17907 +60
==========================================
+ Hits 14987 15037 +50
- Misses 2860 2870 +10
... and 2 files with indirect coverage changes 📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
@@ -49,7 +50,9 @@ const Eventable = Base => | |||
if (l > 0) { | |||
for (let i = 0; i < l; i++) { | |||
if (handler === handlerChain[i].handler && handlerChain[i].context === context) { | |||
console.warn(this, `find '${eventsOn}' handler:`, handler, ' The old listener function will be removed'); | |||
if (!Browser.isTest) { |
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.
方便测试而已,测试环境开了,就不用log,可以让ci跑的更快和输出更加干净
// Dynamic Create Adapter | ||
//利用worker通信向每个workerPool里的每个worker注入新的code | ||
//注意注入的代码在worker code里不是明文的,是个匿名函数挂到adapters,代码层面是看不到改段代码的 | ||
export function createAdapter(key, cb) { |
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.
利用worker把用户动态注入的code发送到worker里,然后再worker里动态生成adapter,并挂到adpters上
onmessage = function (msg) { | ||
msg = msg.data; | ||
//createAdapter | ||
if (msg.messageType === 'createAdapter') { |
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.
如果是动态注入worker
@@ -45,6 +47,19 @@ const EMPTY_BUFFERS = []; | |||
export default class Actor { | |||
|
|||
constructor(workerKey) { | |||
this._delayMessages = []; |
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.
延迟消息,如果一个worker是动态创建的,在这个过程中,用户可能发送消息
|
||
created() { | ||
// handler delay messages | ||
this._delayMessages.forEach(message => { |
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.
处理延迟消息
@@ -73,7 +98,11 @@ export default class Actor { | |||
* @param {Function} cb - callback function when received message from worker thread | |||
*/ | |||
broadcast(data, buffers, cb) { | |||
cb = cb || function () {}; | |||
if (this.initializing) { |
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.
当worker动态创建时,拦截消息,并加入到延迟消息队列,等worker创建完成了,在发送到worker里
if (!workerPool) { | ||
return; | ||
} | ||
const workers = workerPool.workers || []; |
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.
必须是所有的worker,即每个worker里都要注入改adapter
src/core/worker/Actor.js
Outdated
//当第一个Actor初始化完成释放了worker pool里的每个worker资源,后续的Actor的消息通信才会被执行 | ||
if (workersHasCreated() && !hasCreated) { | ||
this.initializing = true; | ||
console.log(`Dynamic Create Adapter for worker:${workerKey}`); |
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.
改成 Injecting codes in worker with worker key: :${workerKey}
另外worker中的adapter存在三种状态: 没有初始化,正在初始化,完成初始化
这里只判断了没有初始化,还应该增加正在初始化的逻辑
fix #2015
fix #2017