Skip to content
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

消息处理器调用链的一些看法 #1454

Open
FlyingBlazer opened this issue Jan 14, 2019 · 4 comments

Comments

Projects
None yet
3 participants
@FlyingBlazer
Copy link

commented Jan 14, 2019

我用的环境

PHP 版本:7.1
overtrue/wechat 版本:4.1.17
是否使用了框架?框架名称:Laravel 5.7

问题及现象

  1. 通常情况下需要EchoStrHandler处理的消息并不需要开发者来处理,而使用FinallyResult时这个消息仍会进入消息处理器的调用链中,如果改为TerminateResult是不是更好。
  2. \EasyWeChat\Kernel\Traits\Observable::notify里switch语句中的break 2是否应该改为break 3,这样才能符合文档中的如果中间某一个 handler 返回值 false, 则将终止整个调用链,不会调用后续的 handlers
  3. \EasyWeChat\Kernel\Traits\Observable::notify能否只用一层循环,相应的对\EasyWeChat\Kernel\Traits\Observable::$handlers的结构进行一定的修改,现在在一些特殊情况下无法保证处理器的执行顺序是按照push的顺序进行的,第二点中的问题也可以一并解决。

第三点中$handlers的结构大概可以改成以下的样子:

[
    0 => ['\First\Handler', Message::TEXT],
    1 => ['\Second\Handler', Message::TEXT|Message::IMAGE],
    2 => ['\Third\Handler', Message::TEXT],
    3 => ['\Fourth\Handler', '*'],
]
@overtrue

This comment has been minimized.

Copy link
Owner

commented Jan 16, 2019

@mingyoung 是不是你上次的修改有问题 😆

@mingyoung

This comment has been minimized.

Copy link
Collaborator

commented Jan 16, 2019

@FlyingBlazer 现在的问题是并没有按照 push 的顺序去处理么?

@FlyingBlazer

This comment has been minimized.

Copy link
Author

commented Jan 17, 2019

顺序的问题可以按照以下进行分析

按以下顺序进行push

$server->push('Handler 1', Message::TEXT);
$server->push('Handler 2', Message::TEXT|Message::EVENT);
$server->push('Handler 3', Message::TEXT);

此时\EasyWeChat\Kernel\Traits\Observable::$handlers中为

[
    Message::TEXT => [
        'Handler 1',
        'Handler 3'
    ],
    Message::TEXT|Message::EVENT => [
        'Handler 2'
    ]
]

如果有一条文本消息进入,则会按1->3->2的顺序执行,而不是1->2->3,因为notify方法中先对handlers进行了遍历,再对其中每一项进行遍历

@FlyingBlazer

This comment has been minimized.

Copy link
Author

commented Jan 17, 2019

最开始我是在检查前两点的问题,翻代码的时候才发现第三点的问题

@overtrue overtrue referenced this issue Feb 15, 2019

Open

[5.0] 规划 #1468

0 of 23 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.