-
-
Notifications
You must be signed in to change notification settings - Fork 78.9k
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
fix: Data.js must not overwrite exising element instance #37442
base: main
Are you sure you want to change the base?
Conversation
bed83bf
to
b5bed16
Compare
I haven't checked the details of the PR but are we supposed to have this random behavior with a lot of errors displayed in the console while running the JS tests?
|
Since I'm the one to blame for the implementation, I wanted to put in my two cents. With regards to the overwriting behavior questioned here #37245 (comment), this was in line with the prior spec: bootstrap/js/tests/unit/dom/data.spec.js Lines 30 to 44 in 088c727
About this PR. The fix is breaking the expectation imho (if i'm not mistaken) e.g.: const instance1 = new Component(element);
const instance2 = new Component(element);
Component.getInstance(element) === instance1 // true
Component.getInstance(element) === instance2 // false This could be considered a breaking change. The second instance won't be set into the registry but it is still being initialized. As such, if someone is expecting My first proposal for the implementation was throwing an error but it was decided against doing it that way, in risk of breaking people's code (#32180 (comment)). On a side note - even with the fix or with the implementation that is in the main branch, there is the possibility to instantiate various components on the same element, even though only one is stored in the registry. If we really would like to enforce some certain behavior, i'd rather propose to throw an error. That way the instantiation of another component would be cancelled, instead of just showing a message and preventing the registry to be updated. Changing the PR to throw an error could be considered a breaking change, and won't really fix the issue mentioned in #37245. So another approach could be to Update: Pushed a draft PR with the solution mentioned here - see #37584 Another approach could be to always return an instance saved in the registry if someone tries to reinitialize a component that already was bound on an element. This could eventually render the |
This update calls dispose() on a component instance automatically in case there was already an instance stored in the registy. rel: twbs#37442
Thank you @alpadev for your answer on this. I prefer your first approach (throw exception), but for sure would be a breaking change. |
@@ -21,7 +21,7 @@ export default { | |||
|
|||
// make it clear we only want one instance per element | |||
// can be removed later when multiple key/instances are fine to be used | |||
if (!instanceMap.has(key) && instanceMap.size !== 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case of same key, could just silently return, without overriding it
Description
Bootstrap's Instances registry, by design, doesn't support multiple instances per element. However, in the rare case we are trying to re-initiate the same component for second time, it wrongly overrides the already existing instance
Details at #37245
Motivation & Context
Accidentally wrong usage of components constructor (ex:
new Toast
), may lead to a variety of runtime errors and side effects that are not expectedType of changes
Checklist
npm run lint
)Related issues
closes #37245
rel: #37500