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

Improve callbacks url generation 2 #1651

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
Open

Conversation

@ibelar
Copy link
Contributor

ibelar commented Jul 22, 2021

Tks! I fixed test and also added a behat test for url

tests/CallbackTest.php Outdated Show resolved Hide resolved
@ibelar
Copy link
Contributor

ibelar commented Jul 22, 2021

I can update the PR using a static property for runningCallback. Then we can grab argument from it when need.


class Callback 
{
   
    protected static $runningCallbacks = [];

    public static function getRunningCallbackArgs(): array
    {
        $args = [];
        foreach (self::$runningCallbacks as $cb) {
            $args[$cb->urlTrigger] = $cb->getTriggeredValue();
        }

        return $args;
    }

    public function set($fx = null, $args = null)
    {
        if ($this->isTriggered() && $this->canTrigger()) {
            self::$runningCallbacks[] = $this;
            return $fx(...($args ?? []));
        }
    }
}

This way, we do not need to check for Get value and also do not need public const URL_QUERY_TRIGGER_PREFIX

@mvorisek What do you think?

@ibelar
Copy link
Contributor

ibelar commented Jul 22, 2021

I have made the changes. Feel free to revert if you do not like it.

@mvorisek
Copy link
Member Author

I can update the PR using a static property for runningCallback. Then we can grab argument from it when need.

I belive running callbacks can be obtained quite easily by traversing the parents /wo the need to persist them in a static array, I will impl. that later.

src/View.php Show resolved Hide resolved
@@ -32,6 +32,12 @@ protected function init(): void
parent::init();

$this->cb = $this->add([Callback::class, 'urlTrigger' => $this->urlTrigger ?: $this->name]);
unset($this->{'urlTrigger'});
Copy link
Member Author

Choose a reason for hiding this comment

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

this is a very bad hack to dedup urlTrigger property stored at 2 places, but let's keep is for now, related with atk4/core#245 , otherwise this property can not be set from seed

@ibelar
Copy link
Contributor

ibelar commented Jul 26, 2021 via email

@mvorisek mvorisek force-pushed the improve_callback_url branch 8 times, most recently from f271757 to 428165b Compare July 27, 2021 09:15
$isTerminated = true;
}

if ($isTerminated && $v->isTriggered() && $v->canTrigger()) {
Copy link
Member Author

Choose a reason for hiding this comment

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

@ibelar does $v->canTrigger() make sense here and any idea how to fix the remaining failing Behat tests?

Copy link
Contributor

Choose a reason for hiding this comment

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

@ibelar does $v->canTrigger() make sense here and any idea how to fix the remaining failing Behat tests?

Yes, canTrigger method will check for a reload. Normally, JsCallback should not run during a reload.

Copy link
Contributor

@ibelar ibelar Jul 27, 2021

Choose a reason for hiding this comment

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

I see why the test is failing. ViewTester reload is missing Callback arguments. Thus when reload is call, the callback method is not run.

I think the reason is that ViewTester is added directly to Modal, while the callback is added to Button

$button->on('click', $vp1Modal->show());

Resulting in

App -> Button -> Cb
    -> Modal -> ViewTester (reload)

Thus the callback is not part of the Modal render tree... missing its arguments when reload URL is rendered.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thus the callback is not part of the Modal render tree...

any idea how to solve it?

Copy link
Member Author

@mvorisek mvorisek Aug 19, 2021

Choose a reason for hiding this comment

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

@ibelar the issue is here:

'uri' => $this->view->jsUrl(['__atk_reload' => $this->view->name]),

  1. in Improve callbacks url generation #1646 you have removed mergeStickyArgsFromChildView which allowed to delegate sticky args from it's children.
  2. in this PR, sticky args are collected from the render tree, but the URL must be obtained thru terminating callback
  3. here, hovewer the URL is obtained thru $this->view which is placed correctly within the Modal render tree, but the Modal is not delegating it's callback sticky args as mergeStickyArgsFromChildView was removed

To better understand the issue, I have pushed a debug code in 53a0352#diff-0741d95fd53e7a9a50ab8b371b0d621c3eaf3cb460c79dfe5a886deaec0814a9R68 , when you open the test URL, the expected and actual value must be the same.

I belive, mergeStickyArgsFromChildView is needed. Or how else should the $this->view->jsUrl(... in JsReload work?

With 29ecd83#diff-c5bb4a733c5ddd3bb0744ef4c29a8fd1e0bd5999fcf06b7f3cfef4c3d1926ce3R640-R668 Behat passes.

Here is a summary of problems to solve:

  1. traverse all callbacks that CAN trigger (can be solved easily but by brute force by analysis whole reachable render tree) - temporary implemented by analysis stack trace
  2. identify THE LAST TERMINATING callback - temporary implemented by pretending that all Modals can terminate - I need a help with this, as terminating callback must be identified from the render tree (not by the user requested terminating callback from URL, as it can differ)

@mvorisek mvorisek marked this pull request as ready for review July 27, 2021 11:07
@ibelar
Copy link
Contributor

ibelar commented Jul 27, 2021 via email

@mvorisek mvorisek force-pushed the improve_callback_url branch 4 times, most recently from aa40cd5 to 20f5f44 Compare September 30, 2021 21:09
@mvorisek mvorisek force-pushed the improve_callback_url branch 3 times, most recently from b1e9b14 to 982c631 Compare October 7, 2021 00:00
@mvorisek mvorisek added the MAJOR label Apr 8, 2022
@mvorisek mvorisek force-pushed the improve_callback_url branch 4 times, most recently from 86ea86e to c592b8f Compare August 28, 2022 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants