The below works fine and my event listener gets the custom event because it's dispatched the event from the window and my event listener is listening for loading on the window, all good.
const MyLib = mylib();
  function mylib() {
    const res = {
      init: (data) => {
        let loading = new CustomEvent('loading', {detail: { loading: true }});
        window.dispatchEvent(loading);
      }
    }
  return res;
}
event listener
 window.addEventListener('loading', handleLoading);
How can I change it to MyLib.addEventListener instead of window.addEventListener?
and..
 window.dispatchEvent(loading); to  MyLib.dispatchEvent(loading);
The error I get is TypeError: MyLib.addEventListener is not a function
The answer below works in a class, but id like to know if this is possible without using a class.
 
     
    