Discussion to have
Various classes are tightly coupled with the global God M object.
For example Skin.js binds events to global scroll events.
Just advice: It may be useful to instead move away from it being global in the first place. Aside from DOM events (which should be bound on document or window directly), anything else surely can belong to an actual object instead of an anonymous global one?
For example, the events emitted by Skin, could be emitted _on_ the Skin itself (by mixing in EventEmitter). The only inconvenience that this introduces is that if there is an on() listener outside the skin module, that module must have a reference to the Skin object. if that is the case, that may in itself uncover debt that may be worth exploring first. If not, or if a reference to Skin is no problem, then making an emitter might be a better solution for you.
We expose the following global events (some throttled):
- resize (emitted by Skin.js used by ImageOverlay)
- scroll (emitted by Skin.js used by InfiniteScroll and skins.minerva.backtotop)
- before-section-toggled (emitted by Toggler and used by Skin.js)
- section-toggled (emitted by Toggler and used by Skin.js; e.g., lazily loading images)
- talk-discussion-added (emitted by TalkSectionAddOverlay.js and used by TalkOverlay)
- talk-added-wo-overlay (emitted by TalkSectionAddOverlay and subscribed to in skins.minerva.talk/init.js)
- watched (emitted by WatchstarGateway used by nothing)
- edit-preview (emitted inside EditorOverlay)
- category-added (emitted by CategoryAddOverlay and used inside CategoryOverlay and resources/skins.minerva.options/categories.js)
Goal: Stop mw.mobileFrontend from being an EventEmitter
- Make TalkOverlay events talk-added-wo-overlay and talk-discussion-added local to the TalkOverlay
- Remove unused events WatchstarGateway (watched), EditorOverlay (edit-preview) - https://gerrit.wikimedia.org/r/363658
- Make event category-added local to component shared by both CategoryAddOverlay and CategoryOverlay (Page ?)
- Stop mw.mobileFrontend from being an EventEmitter
Code review session (3rd August)
We discussed how a global EventEmitter may not be a bad thing but how our existing usages may be inappropriate. We touched upon how we might want to use mw.hook instead for places it truly is needed.
Stephen: Very reasonable pattern for global events consumed by multiple parts. Things like configuration changes, etc.
Jon: Used in places for shortcuts, wild wild west
Sam: Likes event buses and decoupling producers and consumers. About M.on usages, the bus should be injected instead of used from the global. Makes more sense and testing better
Piotr: Why not mw.hook?
Jon: Implemented before that was in core
Sam: There is mw.track too
Joa: Mainly used as a shortcut to connect to parts of state. Avoids refactors and state ownership design work
First, I am sorry. This card touched a lot of different files in MF and Minerva. Here are the patches that were merged for reference:
MF Remove usage of global event emitter from Nearby
MF Remove usage of global event emitter from Talk Overlays and Minerva Remove usage of global event emitter from skins.minerva.talk/init.js
MF Remove usage of global event emitter from MF and Minerva Remove usage of global event emitter from Minerva
MF Remove moduleLoader's event bus
The following were areas of MF and Minerva that were touched and thus might be good to check on the Mobile site:
- Talk Overlays
- Category Overlays
- Photo List
- Image Overlay
- Toggle (section toggle, TOC toggle)
- Back to top scroll button