-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Trigger error from failing Collection.create with wait:true #4265
Conversation
Amazingly, this test passes.
I think it's better to get an error message rendered twice rather than not at all. Calling |
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.
Makes sense to me
Thanks, both! I realized something after submitting the PR. Even in the old situation, someCollection.create({...props}, {wait: true}).on('error', ...); In other words, listening for the event is already possible, just not on the collection. With that in mind, do you still consider the change worthwhile? |
Also what about |
I think it's worthwhile to make sure the error event triggers as expected, because error cases are often not as tested as success cases so someone might not notice the error even never fires in their code. |
Not firing. That depends on the collection forwarding the event again. |
Imho, sending the error event twice should be considered a bug. If it's not that much of a hassle, I think I would opt to go the extra mile to make sure it only fires once. If it turns out it will actually be a significat hassle to make sure it only fires once, perhaps that's a sign the status quo is actually more correct than the fix. Rephrasing the question slightly, if I would have to choose which is less wrong:
I'd say the first option 1 seems significantly less wrong to me. |
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.
@Rayraz thanks for chiming in. It's not much of a hassle to prevent double event forwarding (less than one line of code), but it is somewhat costly performancewise. I will highlight the considerations with code comments below:
I can see why you would be hesitant to introduce the If de-duplicating the event is going to be expensive no matter what, perhaps that's a sign that the status quo is a sensible default behavior. I think the argument could be made that having a collection forward events for a model it doesn't actually own is a nice-to-have rather than a must. If it's an expensive nice-to-have, would it be so terrible to hide this type of event-forwarding behind a feature flag? The 'unexpectedness' of the default behavior could simply be documented. For people who are already being mindful about performance due to working with large datasets, this is probably just fine: someCollection.create({...props}, {wait: true}).on('error', callback); The view.listenToOnce(someCollection.create({...props}, {wait: true}), 'error', callback); |
This is not the case. Lines 996 to 1003 in 3b37e6a
No. It only prevents worse bugs. It is cheap, though.
Agreed.
I'm open to either documenting the unexpected behavior or silently fixing it (like this PR attempts to do) (although it should be mentioned in the change log). In my opinion, adding features would be too much honor for a corner case like this. |
The If you really don't want to add the |
I don't have a strong opinion on the @alanhamlett have your thoughts on the matter evolved? |
TBH the |
This fixes #4262.
The bug arose because the transaction with the backend is mediated through
model.save
and the'error'
event is initially triggered by the model. Normally, the collection will forward all events from the model, but withwait: true
, the model is not yet added to the collection when the event triggers, so the collection is not listening, either. I addressed this by special-casing the'error'
event in this particular scenario.I determined that a similar fix is not required for the
'sync'
event on successfulcreate
. In that case, the model is still added to the collection before the event triggers.I foresee two possible criticisms on the solution:
'request'
event. However, since the timing of this event is fully predictable (i.e., just before the end of the call tocreate
), users do not need to listen for it in order to match the timing of a callback. Adding special case code for the'request'
event as well would further inflate the code size.collection.create
with a model that is already in the collection. In that case, withwait: true
, I expect that the collection will forward the'error'
event twice. If this scenario is considered problematic enough, I am willing to address it.On the other hand, someone might also argue that this issue does not need fixing at all. The old behavior, while surprising, could be argued to be consistent, since the collection cannot be expected to forward events from a model it does not yet contain.
Reviews and discussion welcome!
CC @paulfalgout @ogonkov @alanhamlett @recursivk