amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

Change amp-bind to use custom events instead of MutationObservers

Open kmh287 opened this issue 9 years ago • 6 comments

kmh287 avatar Apr 10 '17 20:04 kmh287

@choumx Do you see a benefit to using events vs just having each element with dynamic content call some method directly on the bind service to inform it of updates?

kmh287 avatar Apr 28 '17 15:04 kmh287

Events are more flexible (can have multiple listeners) and simpler since you won't need to check for existence of the amp-bind extension.

dreamofabear avatar Apr 28 '17 15:04 dreamofabear

We're only creating these events for bind to listen to them, right? Also, won't bindForDoc just return a promise that never resolves if bind isn't installed?

EDIT: Looks like bindForDoc assumes the service exists.

kmh287 avatar Apr 28 '17 15:04 kmh287

Yes and yes. Abstractly, loose coupling is a better fit here.

dreamofabear avatar Apr 28 '17 16:04 dreamofabear

Which is an argument for continuing to use mutation observers, really. (I think we discussed this, but would like to put it on the issue) The current implementation relies on a MutationObserver to tell us which elements have been added or removed. Switching to custom events would require individual elements to fire events that include which elements have been added and removed, pushing bind business logic out to other elements and making developer error far more likely.

kmh287 avatar Apr 28 '17 16:04 kmh287

There's currently a race between bind's DOM scan and amp-list's buildCallback (which instantiates the dynamic container element).

dreamofabear avatar Jun 02 '17 16:06 dreamofabear