DeuxHuitHuit / framework.js-modules

Collections of modules we frequently use with our framework, https://github.com/DeuxHuitHuit/framework.js
https://deuxhuithuit.github.io/
Other
3 stars 8 forks source link

Add window-events #501

Open f-elix opened 3 years ago

f-elix commented 3 years ago

window-events.js aims to eventually replace site-notifier.js.

Like site-notifier.js, it is made to centralize all window events in one place to avoid having the same event registered multiple times by various modules, improving global performance.

The difference is that no event is registered until at least one module registers it, so it is a bit more economical. It is also more flexible. Finally, site-notifier has to notify the whole array of modules (which can get quite large), whereas window-events only has to through the handlers associated with one event, which is bound to be faster.

Usage:

App.fx.notify('window.on', { event: 'resize', handler: onResize });

In this case, window-events.js checks if the 'resize' event is already registered. If it is, it will add the handler to an array of handlers. If not, it will create a 'resize' property in the listeners object and initiate a new array with the handler in it.

To remove the listener:

App.fx.notify('window.off', { event: 'resize', handler: onResize });

window-events.js will remove the handler, and if no other handler is present for this event after that, the event will be unregistered from the window.