mikeflynn / egg.js

A simple javascript library to add easter eggs to web pages.

Home Page:https://mikeflynn.github.io/egg.js/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

What's the difference between the callback and addHook()?

jkjustjoshing opened this issue · comments

Why would you put some things in addHook and some things in the main callback? Why wouldn't you just omit the addHook call and put it all inside the main callback?

Thanks!

There is a one-to-one relationship between easter egg keyCodes (e.g. 'up,down,left,right') and the supplied callback. There is a one-to-many relationship between an easter egg and hooks. When an easter egg is triggered, the supplied callback is executed and then every hook that has been added is executed.

Perhaps you have a few easter eggs and if all are executed, some final easter egg will be revealed; or as the documentation states, perhaps you have multiple easter eggs and you want them all to add a google analytics event or send a tweet. Placing that logic in a hook will keep you from having to repeat the code to perform such actions.

@mikeflynn please correct me if I'm wrong or missed something.

Thanks for the question @jkjustjoshing

Yup, that was the reasoning. Tracking or cleanup code makes the most sense for hooks as they run after every egg.