Skip to content

Commit b52644d

Browse files
committed
Improvements to Base and WIP listeners
Dogfooding helps.
1 parent 79837c9 commit b52644d

File tree

6 files changed

+78
-96
lines changed

6 files changed

+78
-96
lines changed

composer.lock

Lines changed: 39 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Context.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ public function withAppConfig(AppConfig $config): self
122122
/**
123123
* Sets the callback function for acks.
124124
*
125-
* This does is not needed for all server implementations, but it is, it Should be set by the Server implementation,
126-
* and should not be explicitly provided.
125+
* This is not needed for all server implementations, but if it is, it should be set by the Server implementation,
126+
* and should not be explicitly provided otherwise.
127127
*
128128
* @param callable $callback
129129
* @return $this

src/Listeners/Async.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* If a sync listener is not provided, then it defaults to an "ack". Either way, defer() is automatically used so that
1313
* the primary (async) listener will be established correctly for post-"ack" execution.
1414
*/
15-
class Async implements Listener
15+
class Async extends Base
1616
{
1717
private Listener $asyncListener;
1818
private ?Listener $syncListener;
@@ -27,13 +27,13 @@ public function __construct(Listener $asyncListener, ?Listener $syncListener = n
2727
$this->syncListener = $syncListener ?? new Ack();
2828
}
2929

30-
public function handle(Context $context): void
30+
protected function handleAck(Context $context): void
3131
{
32-
if ($context->isAcknowledged()) {
33-
$this->asyncListener->handle($context);
34-
} else {
35-
$this->syncListener->handle($context);
36-
$context->defer();
37-
}
32+
$this->syncListener->handle($context);
33+
}
34+
35+
protected function handleAfterAck(Context $context): void
36+
{
37+
$this->asyncListener->handle($context);
3838
}
3939
}

src/Listeners/Base.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,50 @@
77
use SlackPhp\Framework\{Context, Listener};
88

99
/**
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.
1111
*
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);`.
1514
*/
1615
abstract class Base implements Listener
1716
{
1817
public function handle(Context $context): void
1918
{
19+
// Handle async logic, if executed post-ack.
2020
if ($context->isAcknowledged()) {
2121
$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();
2430
}
2531
}
2632

2733
/**
2834
* Handles application logic that must be preformed prior to the "ack" and Slack's 3-second timeout.
2935
*
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.
3137
*
3238
* @param Context $context
3339
*/
3440
protected function handleAck(Context $context): void
3541
{
36-
$context->ack();
42+
// No-op. Override as needed.
3743
}
3844

3945
/**
4046
* Handles application logic that can or must happen after the "ack" and is not subject to Slack's 3-second timeout.
4147
*
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.
4349
*
4450
* @param Context $context
4551
*/
4652
protected function handleAfterAck(Context $context): void
4753
{
48-
// No-op.
54+
// No-op. Override as needed.
4955
}
5056
}

src/Listeners/Dual.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)