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

[Question] Avoiding data collision #2680

Closed
zimt28 opened this issue May 7, 2019 · 10 comments
Closed

[Question] Avoiding data collision #2680

zimt28 opened this issue May 7, 2019 · 10 comments

Comments

@zimt28
Copy link

zimt28 commented May 7, 2019

I want to use Riot for building dynamic invoices which can have multiple pages.

My app consists of two components: app and page. app holds the state and just renders a list of pages and a page renders a list of items. Each page checks, if it can render the items it was given – if not, it asks to remove an item, until it can show all assigned items:

onMounted(props, state) {
  constContentFrame = this.$("[data-content-frame]")

  if (checkOverflow(contentFrame)) {
    props.removeItemFromPage(props.currentPage)
  }
},
onUpdated(props, state) {
  constContentFrame = this.$("[data-content-frame]")

  if (checkOverflow(contentFrame)) {
    props.removeItemFromPage(props.currentPage)
  }
}

The app component then takes the last item of the page and pushes it to the next page:

removeItemFromPage(pageNumber) {
  const nextPageNumber = pageNumber + 1
  const pages = [...this.state.pages]
  const lastItem = pages[pageNumber - 1].items.pop()

  if (nextPageNumber > this.state.pages.length) {
    pages.push({pageNumber: nextPageNumber, items: []})
  }

  pages[pageNumber].items.unshift(lastItem)

  var _this = this
  setTimeout(() => { _this.update({ pages: pages}) }, 0)
}

This works quite well, until the items need more than two pages: Some of the items will then disappear. This is probably caused by the removeItemFromPage function which reads the current state and sets a modified state, every additional page might call this function at the same time, so changes made between read and update are lost.

Question: Is there a way to make sure that the state cannot be changed between read & write?
The whole code can be found here: https://gist.github.com/zimt28/6b7886ee7fa42af5d7cc540a7e404b4c

Thanks!

@zimt28 zimt28 changed the title [Question] Avoid data collision [Question] Avoiding data collision May 7, 2019
@GianlucaGuarini
Copy link
Member

I am on it. I will check it asap

@GianlucaGuarini
Copy link
Member

Ok I have spotted a bug here. Attributes will not passed to children components in a each directive. I will fix the compiler output

GianlucaGuarini added a commit to riot/compiler that referenced this issue May 9, 2019
@GianlucaGuarini
Copy link
Member

This issue was solved in riot@4.0.0-rc.9 thanks for reporting it!

@zimt28
Copy link
Author

zimt28 commented May 9, 2019

I've just tried using riot@4.0.0-rc.9/riot+compiler.min.js but am still running into the issue of missing items. Is there anything else I can do to fix it?

-> https://gist.github.com/zimt28/6b7886ee7fa42af5d7cc540a7e404b4c reproduces it

@GianlucaGuarini
Copy link
Member

Have you tried?
<script src="https://unpkg.com/riot@next/riot+compiler.min.js"></script> -> <script src="https://unpkg.com/riot@4.0.0-rc.9/riot+compiler.min.js"></script>

@zimt28
Copy link
Author

zimt28 commented May 9, 2019

Yes, I didn't update the Gist but riot@next also delivers the v4.0.0-rc.9 file

@GianlucaGuarini
Copy link
Member

This works quite well, until the items need more than two pages: Some of the items will then disappear.

I see 3 pages and I am not sure I understand the problem:

ezgif-2-378a48b90155

I have found initially a riot@4 bug of communication between parent child but now it works everything fine. I can't help you any further here and I don't see any riot bug. Please try posting your issue on stackoverflow providing more info about your browser as well.
Good luck and thanks for this post

@zimt28
Copy link
Author

zimt28 commented May 11, 2019

Sorry - I think I failed to describe the actual issue. If you look at your screen video, you see three items on page three: 19, 24, 27. Items 20, 21, 22 ,23, 25, 26 are suddenly missing, even though they should be on page three. I assume this is an issue with riot or at least with the way riot works.

@zimt28
Copy link
Author

zimt28 commented May 17, 2019

@GianlucaGuarini Sorry to ping you again, but could you re-evaluate if it‘s a Riot issue or not? I think we weren‘t talking about the same problem before.

@GianlucaGuarini
Copy link
Member

GianlucaGuarini commented May 17, 2019

It's not a Riot.js issue you will have the same problem with any framework. It's an application issue. Please consider updating your code handling your data differently preparing your page chunks before they get passed to your components. I am sorry I can fix here only Riot.js issues but I am not available for consulting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants