|
7 | 7 | use SlackPhp\Framework\{Context, Listener}; |
8 | 8 |
|
9 | 9 | /** |
10 | | - * Base class for listener classes that may have both sync (pre-ack) and async (post-ack) logic. |
| 10 | + * Base class for listener classes that may have both sync (pre-"ack") and async (post-"ack") logic. |
11 | 11 | * |
12 | | - * Note: Does not automatically defer. This allows flexibility to decide if deferring is needed. In many cases, the |
13 | | - * logic that happens prior to the ack represents a complete handling of the event. If additional logic must be |
14 | | - * performed after the ack, then $context->defer() should be called in the handleAck() method implementation. |
| 12 | + * By default, the context is set to defer, so that post-ack logic will be executed. In some cases, a complete handling |
| 13 | + * of an event can be done prior to the ack. In that case, the `handleAck()` method can call `$content->defer(false);`. |
15 | 14 | */ |
16 | 15 | abstract class Base implements Listener |
17 | 16 | { |
18 | 17 | public function handle(Context $context): void |
19 | 18 | { |
| 19 | + // Handle async logic, if executed post-ack. |
20 | 20 | if ($context->isAcknowledged()) { |
21 | 21 | $this->handleAfterAck($context); |
22 | | - } else { |
23 | | - $this->handleAck($context); |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + // Handle sync logic, if executed pre-ack. |
| 26 | + $context->defer(true); |
| 27 | + $this->handleAck($context); |
| 28 | + if (!$context->isAcknowledged()) { |
| 29 | + $context->ack(); |
24 | 30 | } |
25 | 31 | } |
26 | 32 |
|
27 | 33 | /** |
28 | 34 | * Handles application logic that must be preformed prior to the "ack" and Slack's 3-second timeout. |
29 | 35 | * |
30 | | - * By default, this does an ack. You should override this method with your own implementation to do more. |
| 36 | + * By default, this does nothing. You should override this method with your own implementation. |
31 | 37 | * |
32 | 38 | * @param Context $context |
33 | 39 | */ |
34 | 40 | protected function handleAck(Context $context): void |
35 | 41 | { |
36 | | - $context->ack(); |
| 42 | + // No-op. Override as needed. |
37 | 43 | } |
38 | 44 |
|
39 | 45 | /** |
40 | 46 | * Handles application logic that can or must happen after the "ack" and is not subject to Slack's 3-second timeout. |
41 | 47 | * |
42 | | - * By default, this does nothing. You should override this method with your own implementation if you use defer(). |
| 48 | + * By default, this does nothing. You should override this method with your own implementation. |
43 | 49 | * |
44 | 50 | * @param Context $context |
45 | 51 | */ |
46 | 52 | protected function handleAfterAck(Context $context): void |
47 | 53 | { |
48 | | - // No-op. |
| 54 | + // No-op. Override as needed. |
49 | 55 | } |
50 | 56 | } |
0 commit comments