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

possible race condition: unicorn:model looses data when saved too quickly. #684

Open
nerdoc opened this issue Mar 22, 2024 · 1 comment
Open

Comments

@nerdoc
Copy link
Contributor

nerdoc commented Mar 22, 2024

Especially when connections are slow, but even on localhost, the model is not fast enough to send the data to the backend, when the model saving method is called shortly after the last keypress. See this example:

class PersonView(UnicornView):
    person = Person.objects.none()

    def mount(self):
        self.person = Person.objects.get(
            pk=self.component_kwargs["pk"]
        )

    def save(self):
        self.person.save()
<div>
  <input type="text" unicorn:model="person.name" unicorn.keyup.enter="save">
</div>

When you run that component, write a longer string like "sdhfkjhdsfkjhkjdsf" and quickly press Enter, the saved model's name field will only contain approximately "sdhfkjhds".

I want to use unicorn for "inline" editing - a <input> replaces the text when in edit_mode and is saved automatically if in any of the input fields the user presses Enter. A quick and responsive UI is important, and data loss is very bad, in any case.

@nerdoc nerdoc changed the title unicorn:model looses data possible race condition: unicorn:model looses data when saved too quickly. Mar 22, 2024
@nerdoc nerdoc changed the title possible race condition: unicorn:model looses data when saved too quickly. possible race condition: unicorn:model looses data when saved too quickly. Mar 22, 2024
@adamghill
Copy link
Owner

My hypothesis has been that multiple requests are getting sent and come back from the server in a different order, so forcing requests into a queue was my attempt to solve for this. Have you tried enabling serial?

I have always felt like there should be a more elegant solution in the JavaScript of unicorn (maybe an event bus or something?), but replicating this and building a test suite which ensures everything works as expected has been a challenge, unfortunately.

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

No branches or pull requests

2 participants