Reworking synthetic event handling
The syntehtic event handling in ES Module Shims kind of grew organically one test case at a time. Each one seemed sensible for users, but together we've ended up in a place where the synthetic event model isn't really fully well-defined, and with issues like https://github.com/guybedford/es-module-shims/pull/506 still coming up.
So to take a step back and try to think about a better refactoring here.
The current status is that we re-trigger domready (complete state only), the document DOMContentLoaded and the window load event.
This came out of issues respectively - https://github.com/guybedford/es-module-shims/issues/99, https://github.com/guybedford/es-module-shims/issues/161.
We individually track all of these, but the tracking has some issues in that it is not always correct with various edge cases including error cases.
So I'd love to try and simplify this support some more:
- I'd like to remove DOMContentLoaded event synthesis since in my latest testing browsers typically even for async module scripts this fires before the module script. I've never really understood the idea that the polyfill should refire DOMContentLoaded in that it's not got anything to do with the DOM load, it's entirely about the actual interactive -> loaded state transition that is "recreated" by the polyfill. The pollyfill doesn't have anything to do with changing how dom parsing happens or when it completes. It was only added because of the vehement interest originally in https://github.com/guybedford/es-module-shims/issues/99 but perhaps that was misguided.
- It would be nice if we could use the readystate for all event tracking with just a readystate change listener, since that gives us the same information as the DOMContentLoaded and load events anyway. Perhaps we might still need both a load listener as well to avoid firing before the native event though.
- In the case of async module scripts, the top-level load event should probably not even be refired at all - it's only the defer scripts and the existence of at least one static defer script that should cause the refiring.
Overall it should be possible to end up with a simpler correct model using less code to do it.
Would value any thoughts on the above before undertaking further work though.