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

Race condition with loadOriginalValues #1298

Closed
3 tasks done
gitgitwhat opened this issue Feb 18, 2024 · 3 comments
Closed
3 tasks done

Race condition with loadOriginalValues #1298

gitgitwhat opened this issue Feb 18, 2024 · 3 comments

Comments

@gitgitwhat
Copy link

Prerequisites

  • I am running the latest version
  • I checked the documentation and found no answer
  • I checked to make sure that this issue has not already been filed

💥 Demo Page

Not something I can easily demo in JSBin.

Explanation

  • What is the expected behavior?
    There appears to be a race condition with loadOriginalValues during loading. Here's what I'm seeing. My PHP page generates a number of addTags calls to populate a Tagify list. There is also a lot of other dynamically generated Javascript that that gets created and executed. The tags get added but then the original values observer executes for the first time and removes all the existing tags via this.removeAllTags(); on line 2599.

To fix this problem, I just need to change the position of my Tagify addTag initialization to run before all other generated Javascript. So, it seems like there's a race condition, under heavy Javascript load, with tags being added and the initial original value check.

  • What is happening instead?
    Existing tags should be saved if they exist and added back instead they are just simply cleared.

  • What error message are you getting?
    None

@yairEO
Copy link
Owner

yairEO commented Mar 7, 2024

why would PHP generate addTags function calls? you should render the page with the input elements already filled with their correct value. do not call addTags unless you are doing an async call to the server AFTER the page has been rendered in order to fetch the fields' data.

@gitgitwhat
Copy link
Author

Fundamentally, it doesn't matter why. The issue is still valid. The library shouldn't clear any values that already exist in the list.

@yairEO
Copy link
Owner

yairEO commented Mar 15, 2024

The Tagify component and the (hidden) input element it's fundamentally bound to must always be in-sync. if some code somehow manipulates the input element then the Tagify component will adapt accordingly:

https://github.com/yairEO/tagify/blob/master/src/parts/events.js#L680-L688

The observeOriginalInputValue function runs every 500ms

You should make sure your code does not change the Tagified input's value in any way.
on Tagify should handle the input field once it has been "tagified".

Make sure you don't have a form reset code running after input fields have been tagified which is a possibility if you say all your tags are removed.

@yairEO yairEO closed this as completed Mar 16, 2024
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