The event handling registration there is kind of dynamic in nature, even though I think it's not used 99% of the time for that feature. This makes it opaque to me how the event queue is handled though, the table with multiple handlers per event type is passed around like a shopping cart (and other metaphors

This all seems like a diagonal sushi bus base with spaghetti builds on the side. I think storing not just data but functions too in "global" would greatly help to reduce the complexity. Is there a way though? As far as I understand, Factorio will error when you try to save a function in "global" because the serpent library cannot serialize functions.
Maybe ChatGPT can help?