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

Does the Render type have the parameter order wrong? #812

Closed
TastyPi opened this issue Dec 8, 2022 · 5 comments · Fixed by #814
Closed

Does the Render type have the parameter order wrong? #812

TastyPi opened this issue Dec 8, 2022 · 5 comments · Fixed by #814

Comments

@TastyPi
Copy link

TastyPi commented Dec 8, 2022

I'm not sure if I'm just misunderstanding something or if they are actually the wrong way round, but it took me a while to figure out what the issue was so this at least might help someone figure it out. The Render type is defined as (newElement: E, currentElement: E) => void, but it seems like it should be (currentElement: E, newElement: E) => void. Let me explain.

For background: We use Front's live chat widget, and their library adds an iframe to the body than can never be reloaded. This means we need to keep the same body element as well. The original workaround we tried used morphdom, but then we ran into this issue of Stimulus controllers not reloading. So we decided to implement a custom renderer that keeps the body and iframe but copies everything else across.

This is the first attempt:

  event.detail.render = (newElement, currentElement) => {
    // Remove all the old elements except the Front iframe
    Array.from(currentElement.children).forEach((element) => {
      if (element.id !== FRONT_ELEMENT_ID) {
        element.remove()
      }
    })

    // Move the new elements across
    Array.from(newElement.children).forEach((element) => currentElement.appendChild(element))
  }

This doesn't work, it looks like it just removes all the children without copying the new ones across. I assumed the newElement was the newly rendered body and the currentElement was the body currently in the DOM. Turns out they're the other way round, and swapping the parameter order made it start working - it looked like it was just removing elements because the elements were being moved the wrong way.

The naming of these parameters is very confusing to me, how could the existing DOM be considered the newElement? Hence this issue, are the parameters named the wrong way round or am I misunderstanding the meaning of the terms new and current?

@TastyPi
Copy link
Author

TastyPi commented Dec 8, 2022

@seanpdoyle who introduced the Render type in #431

@TastyPi
Copy link
Author

TastyPi commented Dec 8, 2022

It looks like it should be defined (currentElement, newElement)

this.renderElement(this.currentElement, this.newElement)

@seanpdoyle
Copy link
Contributor

@TastyPi thank you for opening this issue.

Yes, the order of the arguments in the Render signature are incorrect, and were introduced in that incorrect order:

https://github.com/hotwired/turbo/pull/431/files#diff-d554b20c605a72c68b2d429ff9915cca4afa8e118026f56ede93382d130382a7R11

Luckily, the default implementations and the tests that exercise that feature are in the correct order.

The signature's argument order was inspired by morphdom's, with the latter argument serving as overrides for the prior argument.

Fortunately (and unfortunately!), custom rendering isn't yet covered by Turbo's documentation website, so we have an opportunity to document it thoroughly and correctly.

In the meantime, I'll open a pull request to swap the order of the arguments for TypeScript's sake.

seanpdoyle added a commit to seanpdoyle/turbo that referenced this issue Dec 8, 2022
Closes [hotwired#812][]

The order of the arguments in the `Render` signature are incorrect, and
[were introduced in that incorrect order][incorrect].

Luckily, the default implementations and the tests that exercise that
feature are in the correct order.

The signature's argument order was inspired by
[morphdom's](https://github.com/patrick-steele-idem/morphdom#usage),
with the latter argument serving as overrides for the prior argument.

Custom rendering isn't yet covered by Turbo's documentation website, so
we have an opportunity to document it thoroughly and correctly, starting
with TypeScript's understanding of the interface.

[hotwired#812]: hotwired#812
[incorrect]: https://github.com/hotwired/turbo/pull/431/files#diff-d554b20c605a72c68b2d429ff9915cca4afa8e118026f56ede93382d130382a7R11
@seanpdoyle
Copy link
Contributor

I've opened #814 to resolve this.

seanpdoyle added a commit to seanpdoyle/turbo that referenced this issue Dec 8, 2022
Closes [hotwired#812][]

The order of the arguments in the `Render` signature are incorrect, and
[were introduced in that incorrect order][incorrect].

Luckily, the default implementations and the tests that exercise that
feature are in the correct order.

The signature's argument order was inspired by
[morphdom's](https://github.com/patrick-steele-idem/morphdom#usage),
with the latter argument serving as overrides for the prior argument.

Custom rendering isn't yet covered by Turbo's documentation website, so
we have an opportunity to document it thoroughly and correctly, starting
with TypeScript's understanding of the interface.

[hotwired#812]: hotwired#812
[incorrect]: https://github.com/hotwired/turbo/pull/431/files#diff-d554b20c605a72c68b2d429ff9915cca4afa8e118026f56ede93382d130382a7R11
seanpdoyle added a commit to seanpdoyle/turbo that referenced this issue Dec 8, 2022
Closes [hotwired#812][]

The order of the arguments in the `Render` signature are incorrect, and
[were introduced in that incorrect order][incorrect].

Luckily, the default implementations and the tests that exercise that
feature are in the correct order.

The signature's argument order was inspired by
[morphdom's](https://github.com/patrick-steele-idem/morphdom#usage),
with the latter argument serving as overrides for the prior argument.

Custom rendering isn't yet covered by Turbo's documentation website, so
we have an opportunity to document it thoroughly and correctly, starting
with TypeScript's understanding of the interface.

[hotwired#812]: hotwired#812
[incorrect]: https://github.com/hotwired/turbo/pull/431/files#diff-d554b20c605a72c68b2d429ff9915cca4afa8e118026f56ede93382d130382a7R11
@TastyPi
Copy link
Author

TastyPi commented Dec 8, 2022

Some documentation would be great, I've just found that my custom rendering logic breaks back/forward navigation and I have no idea why.

seanpdoyle added a commit to seanpdoyle/turbo that referenced this issue Dec 8, 2022
Closes [hotwired#812][]

The order of the arguments in the `Render` signature are incorrect, and
[were introduced in that incorrect order][incorrect].

Luckily, the default implementations and the tests that exercise that
feature are in the correct order.

The signature's argument order was inspired by
[morphdom's](https://github.com/patrick-steele-idem/morphdom#usage),
with the latter argument serving as overrides for the prior argument.

Custom rendering isn't yet covered by Turbo's documentation website, so
we have an opportunity to document it thoroughly and correctly, starting
with TypeScript's understanding of the interface.

[hotwired#812]: hotwired#812
[incorrect]: https://github.com/hotwired/turbo/pull/431/files#diff-d554b20c605a72c68b2d429ff9915cca4afa8e118026f56ede93382d130382a7R11
seanpdoyle added a commit to seanpdoyle/turbo that referenced this issue Dec 8, 2022
Closes [hotwired#812][]

The order of the arguments in the `Render` signature are incorrect, and
[were introduced in that incorrect order][incorrect].

Luckily, the default implementations and the tests that exercise that
feature are in the correct order.

The signature's argument order was inspired by
[morphdom's](https://github.com/patrick-steele-idem/morphdom#usage),
with the latter argument serving as overrides for the prior argument.

Custom rendering isn't yet covered by Turbo's documentation website, so
we have an opportunity to document it thoroughly and correctly, starting
with TypeScript's understanding of the interface.

[hotwired#812]: hotwired#812
[incorrect]: https://github.com/hotwired/turbo/pull/431/files#diff-d554b20c605a72c68b2d429ff9915cca4afa8e118026f56ede93382d130382a7R11
seanpdoyle added a commit to seanpdoyle/turbo that referenced this issue Dec 9, 2022
Closes [hotwired#812][]

The order of the arguments in the `Render` signature are incorrect, and
[were introduced in that incorrect order][incorrect].

Luckily, the default implementations and the tests that exercise that
feature are in the correct order.

The signature's argument order was inspired by
[morphdom's](https://github.com/patrick-steele-idem/morphdom#usage),
with the latter argument serving as overrides for the prior argument.

Custom rendering isn't yet covered by Turbo's documentation website, so
we have an opportunity to document it thoroughly and correctly, starting
with TypeScript's understanding of the interface.

[hotwired#812]: hotwired#812
[incorrect]: https://github.com/hotwired/turbo/pull/431/files#diff-d554b20c605a72c68b2d429ff9915cca4afa8e118026f56ede93382d130382a7R11
seanpdoyle added a commit to seanpdoyle/turbo that referenced this issue Dec 10, 2022
Closes [hotwired#812][]

The order of the arguments in the `Render` signature are incorrect, and
[were introduced in that incorrect order][incorrect].

Luckily, the default implementations and the tests that exercise that
feature are in the correct order.

The signature's argument order was inspired by
[morphdom's](https://github.com/patrick-steele-idem/morphdom#usage),
with the latter argument serving as overrides for the prior argument.

Custom rendering isn't yet covered by Turbo's documentation website, so
we have an opportunity to document it thoroughly and correctly, starting
with TypeScript's understanding of the interface.

[hotwired#812]: hotwired#812
[incorrect]: https://github.com/hotwired/turbo/pull/431/files#diff-d554b20c605a72c68b2d429ff9915cca4afa8e118026f56ede93382d130382a7R11
@dhh dhh closed this as completed in #814 Dec 23, 2022
dhh pushed a commit that referenced this issue Dec 23, 2022
* Correct `Render<E>` method signature argument order

Closes [#812][]

The order of the arguments in the `Render` signature are incorrect, and
[were introduced in that incorrect order][incorrect].

Luckily, the default implementations and the tests that exercise that
feature are in the correct order.

The signature's argument order was inspired by
[morphdom's](https://github.com/patrick-steele-idem/morphdom#usage),
with the latter argument serving as overrides for the prior argument.

Custom rendering isn't yet covered by Turbo's documentation website, so
we have an opportunity to document it thoroughly and correctly, starting
with TypeScript's understanding of the interface.

[#812]: #812
[incorrect]: https://github.com/hotwired/turbo/pull/431/files#diff-d554b20c605a72c68b2d429ff9915cca4afa8e118026f56ede93382d130382a7R11

* Update `@playwright/test` dependency

Our [CI][] is currently failing while executing `yarn run playwright
install --with-deps`.

This commit updates the dependency in an effort to resolve whatever
underlying dependency issues are causing the failure.

[CI]: https://github.com/hotwired/turbo/actions/runs/3664875382/jobs/6195570624#step:6:1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants