wnr / element-resize-detector

Optimized cross-browser resize listener for elements.
MIT License
1.32k stars 118 forks source link

only one instance got notify when Multiple ERD instance bind to same element #75

Closed zuomingcai closed 7 years ago

zuomingcai commented 7 years ago

only one instance got notify when Multiple instance bind to same element

wnr commented 7 years ago

Hi, can you define instance?

If you mean having multiple functions listening to the element, this should work and is covered in the following test case https://github.com/wnr/element-resize-detector/blob/master/test/element-resize-detector_test.js#L198

zuomingcai commented 7 years ago

I created two elementResizeDetectorMakers,both listen to the same element,only one listener got notified.

wnr commented 7 years ago

I see, thanks for the clarification. This should be possible and I'll look into it. However, please note that you won't get as good performance if you use different instances for the elements - since operations cannot be synchronised then.

wnr commented 7 years ago

I have now analysed this, and have come to the conclusion that this is too complex to support. An element is something globally mutable, and having two instances of systems that want to mutate this is complex in terms of syncing. This is a common limitation, you won't be able to have two React instances rendering to the same div. React ensures that this doesn't happen by defining React globally. element-resize-detector leaves it up to you whether you want to use dependency injection or a singleton pattern to define it globally. Please note that you can still have multiple instances running in the same application, the limitation is that they cannot control the same elements. The complexity price of supporting this is too high compared to the benefits in my opinion.