Skip to content

Commit

Permalink
Always assume browser SSE support (#2189)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored Mar 26, 2024
1 parent 0c4153d commit e9d25ce
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 41 deletions.
2 changes: 1 addition & 1 deletion js/src/plugins/server-event.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default class AtkServerEventPlugin extends AtkPlugin {
const element = this.$el;
const hasLoader = this.settings.showLoader;

this.source = new EventSource(this.settings.url + '&__atk_sse=1');
this.source = new EventSource(this.settings.url);
if (hasLoader) {
element.addClass('loading');
}
Expand Down
2 changes: 1 addition & 1 deletion public/js/atkjs-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -1677,7 +1677,7 @@ class AtkServerEventPlugin extends _atk_plugin__WEBPACK_IMPORTED_MODULE_2__["def
main() {
const element = this.$el;
const hasLoader = this.settings.showLoader;
this.source = new EventSource(this.settings.url + '&__atk_sse=1');
this.source = new EventSource(this.settings.url);
if (hasLoader) {
element.addClass('loading');
}
Expand Down
2 changes: 1 addition & 1 deletion public/js/atkjs-ui.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/atkjs-ui.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/atkjs-ui.min.js.map

Large diffs are not rendered by default.

55 changes: 23 additions & 32 deletions src/JsSse.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ class JsSse extends JsCallback

private string $lastSentId = '';

/** @var bool Allows us to fall-back to standard functionality of JsCallback if browser does not support SSE. */
public $browserSupport = false;

/** @var bool Show Loader when doing SSE. */
public $showLoader = false;

Expand All @@ -27,17 +24,6 @@ class JsSse extends JsCallback
/** @var \Closure(string): void|null Custom function for outputting (instead of echo). */
public $echoFunction;

#[\Override]
protected function init(): void
{
parent::init();

if ($this->getApp()->tryGetRequestQueryParam('__atk_sse')) {
$this->browserSupport = true;
$this->initSse();
}
}

#[\Override]
public function jsExecute(): JsBlock
{
Expand All @@ -61,12 +47,15 @@ public function set($fx = null, $args = null)
throw new \TypeError('$fx must be of type Closure');
}

return parent::set(static function (Jquery $chain) use ($fx, $args) {
return parent::set(function (Jquery $chain) use ($fx, $args) {
$this->initSse();

// TODO replace EventSource to support POST
// https://github.com/Yaffle/EventSource
// https://github.com/mpetazzoni/sse.js
// https://github.com/EventSource/eventsource
// https://github.com/byjg/jquery-sse

return $fx($chain, ...array_values($args ?? []));
});
}
Expand Down Expand Up @@ -96,10 +85,15 @@ protected function initSse(): void
*/
public function send(JsExpressionable $action, bool $success = true): void
{
if ($this->browserSupport) {
$ajaxec = $this->getAjaxec($action);
$this->sendEvent('', $this->getApp()->encodeJson(['success' => $success, 'atkjs' => $ajaxec->jsRender()]), 'atkSseAction');
}
$ajaxec = $this->getAjaxec($action);
$this->sendEvent(
'',
$this->getApp()->encodeJson([
'success' => $success,
'atkjs' => $ajaxec->jsRender(),
]),
'atkSseAction'
);
}

/**
Expand All @@ -109,21 +103,18 @@ public function send(JsExpressionable $action, bool $success = true): void
public function terminateAjax(JsBlock $ajaxec, $msg = null, bool $success = true): void
{
$ajaxecStr = $ajaxec->jsRender();

if ($this->browserSupport) {
if ($ajaxecStr !== '') {
$this->sendEvent(
'',
$this->getApp()->encodeJson(['success' => $success, 'atkjs' => $ajaxecStr]),
'atkSseAction'
);
}

// no further output please
$this->getApp()->terminate();
if ($ajaxecStr !== '') {
$this->sendEvent(
'',
$this->getApp()->encodeJson([
'success' => $success,
'atkjs' => $ajaxecStr,
]),
'atkSseAction'
);
}

$this->getApp()->terminateJson(['success' => $success, 'atkjs' => $ajaxecStr]);
$this->getApp()->terminate();
}

protected function flush(): void
Expand Down
8 changes: 4 additions & 4 deletions tests/DemosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,10 +425,10 @@ public function testDemoJsonResponse(string $path, ?string $expectedExceptionMes

public static function provideDemoSseResponseCases(): iterable
{
yield ['_unit-test/sse.php?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'see_test=ajax&' . Callback::URL_QUERY_TARGET . '=1&__atk_sse=1'];
yield ['_unit-test/console.php?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'console_test=ajax&' . Callback::URL_QUERY_TARGET . '=1&__atk_sse=1'];
yield ['_unit-test/console_run.php?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'console_test=ajax&' . Callback::URL_QUERY_TARGET . '=1&__atk_sse=1'];
yield ['_unit-test/console_exec.php?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'console_test=ajax&' . Callback::URL_QUERY_TARGET . '=1&__atk_sse=1'];
yield ['_unit-test/sse.php?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'see_test=ajax&' . Callback::URL_QUERY_TARGET . '=1'];
yield ['_unit-test/console.php?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'console_test=ajax&' . Callback::URL_QUERY_TARGET . '=1'];
yield ['_unit-test/console_run.php?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'console_test=ajax&' . Callback::URL_QUERY_TARGET . '=1'];
yield ['_unit-test/console_exec.php?' . Callback::URL_QUERY_TRIGGER_PREFIX . 'console_test=ajax&' . Callback::URL_QUERY_TARGET . '=1'];
}

/**
Expand Down

0 comments on commit e9d25ce

Please sign in to comment.