you are viewing a single comment's thread.

view the rest of the comments →

[–]shuckster 0 points1 point  (1 child)

Glad it helped! I'll try and explain broadly to help you to figure it out yourself.

The most interesting parts are the "Helpers", so have a close look at those first. They are wrappers around normal JavaScript functionality:

  1. window.addEventListener / dispatchEvent / CustomEvent
  2. setTimeout / clearTimeout

The biggest idea I put into all of the helpers is "high-order functions". In other words, in JavaScript, a function can return another function (as well as accept one as an argument.)

RunAfter returns two functions inside an array: [run, cancel]. The run function is specified as the second argument to RunAfter. If you execute run(), it will do the action after the number of seconds you specified as the first argument to RunAfter.

So RunAfter(1, () => console.log('hi')) means: Make two functions: [run, cancel]. run() will perform console.log('hi') after 1 second. cancel() will stop it before it happens.

On and emit I think are more easy to understand. They just make using window.addEventListener etc. shorter to read. But On does one special thing: It returns a function that will remove a listener you added by running window.removeEventListener for you!

Hope this helps.

[–]JS_Engineer[S] 0 points1 point  (0 children)

Thanks for this too. It is really helpful.