Skip to content

[fix] Add stop function to ServerEvent js plugin#1133

Merged
ibelar merged 21 commits intodevelopfrom
fix/server-event-plugin
Apr 29, 2020
Merged

[fix] Add stop function to ServerEvent js plugin#1133
ibelar merged 21 commits intodevelopfrom
fix/server-event-plugin

Conversation

@ibelar
Copy link
Copy Markdown
Contributor

@ibelar ibelar commented Apr 25, 2020

Allow stopping of SSE event.

More info on demos/sse.php

@ibelar ibelar requested a review from abbadon1334 April 25, 2020 16:34
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 25, 2020

Codecov Report

Merging #1133 into develop will decrease coverage by 0.06%.
The diff coverage is 45.45%.

Impacted file tree graph

@@              Coverage Diff              @@
##             develop    #1133      +/-   ##
=============================================
- Coverage      75.83%   75.77%   -0.07%     
- Complexity      2534     2538       +4     
=============================================
  Files            130      130              
  Lines           6221     6230       +9     
=============================================
+ Hits            4718     4721       +3     
- Misses          1503     1509       +6     
Impacted Files Coverage Δ Complexity Δ
src/jsSSE.php 85.71% <45.45%> (-8.74%) 24.00 <1.00> (+4.00) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 34cc411...c8ce46a. Read the comment docs.

@mvorisek mvorisek self-requested a review April 25, 2020 17:28
@abbadon1334
Copy link
Copy Markdown
Collaborator

abbadon1334 commented Apr 25, 2020

The problem i mentioned, was related to browser locking when window unload event is trigger.
I solved in this way :

window.addEventListener('beforeunload', function(event) {
that.source.close();
});

but i think that the listener must be removed.

this is an example of the code to reproduce the issue :

        View::addTo($app)
            ->setElement('footer')
            ->addClass('ui basic segment mini')
            ->set('footer');

        $sse = jsSSE::addTo($app, ['showLoader' => false]);
        $sse->set(function () use ($sse) {

            $count = 0;
            ignore_user_abort(true); // i think this must be added to the jsSSE class
            while(!connection_aborted()) { // and this check too, obviously without the while
                sleep(1);
                $sse->send(new jsExpression('console.log('.$count.')'));
                $count++;
            }
        });

        $app->html->template->appendHTML('HEAD', '<script type="application/javascript">' . $sse->jsRender() . '</script>');

This code without that change prevent the browser reload

@abbadon1334 abbadon1334 requested a review from mvorisek April 25, 2020 18:26
@mvorisek
Copy link
Copy Markdown
Member

@ibelar , @abbadon1334 Travis is failing probably not due to this PR, but should be fixed to prevent false positives, can you look at it?

@abbadon1334
Copy link
Copy Markdown
Collaborator

@ibelar , @abbadon1334 Travis is failing probably not due to this PR, but should be fixed to prevent false positives, can you look at it?

restart the build and it worked, sometime travis drink beer on saturday, that's it.

@mvorisek
Copy link
Copy Markdown
Member

restart the build and it worked, sometime travis drink beer on saturday, that's it.

I know, but that is not a long term solution, there is probably some expected but not synchronized/event-based check.

@ibelar
Copy link
Copy Markdown
Contributor Author

ibelar commented Apr 26, 2020

@mvorisek - @abbadon1334

I have update the pr. Will now check if connection need to be keepAlive.
When keepAlive is set to false, it will behave as before (default)
When set to true, it will keep executing, even after user stop execution

I have also add a function that you can use when user stop execution.
Consider this. If use cancel, it will still run until counter reach a minimum value.

Class Counter
{
    public $count = 0;
}

$counter = new Counter();

$sse = \atk4\ui\jsSSE::addTo($app, ['showLoader' => false, 'keepAlive' => true]);

$sse->onAborted(function($t, $c) {
    if ($c->count > 10) {
        //minimum is reach
        exit();
    }

}, $counter);

$sse->set(function () use ($sse, $counter) {
    $test = true;
    while($test) { // and this check too, obviously without the while
        sleep(1);
        $sse->send(new \atk4\ui\jsExpression('console.log('.$counter->count.')'));
        $counter->count++;
        if ($counter->count > 100) {
            $test = false;
        }
    }
});

Copy link
Copy Markdown
Collaborator

@abbadon1334 abbadon1334 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM except the call to exit() which is okish, but even if is already dead the interaction with the user, calling exit there will not trigger the beforeExit hook

src/jsSSE.php Outdated
/*
* Implements a class that can be mapped into arbitrary JavaScript expression.
*/
// Implements a class that can be mapped into arbitrary JavaScript expression.
Copy link
Copy Markdown
Member

@mvorisek mvorisek Apr 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ibelar use /** (two stars) for proper phpdoc

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Let's merge this in so it get include in next release.

@ibelar ibelar merged commit ff825b6 into develop Apr 29, 2020
@ibelar ibelar deleted the fix/server-event-plugin branch April 29, 2020 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants