balderdashy/sails — pull request #1219 by ragulka (2013-12-19 12:22:45+00:00)
Title: Hook dependency graph

As a result of the conversion in #1039 I am presenting: hook dependency graph! It allows users to defined various dependencies between hooks. For example, a hook may need to wait for another hook to be loaded before it can be loaded, or a hook may define that it needs to load before another hook.

This change should be totally backwards-compatible.

Each hook can now register the following properties:
```javascript
// string or an array - the hook should be loaded before these (if they are enabled)
loadBefore: 'csrf',
// string or array - the hook should load after these (if they are enabled)
loadAfter: ['cors', 'pubsub'],
// string or array - the hook needs these hooks to be enabled. If not, sails will throw an error
dependencies: ['orm', 'pubsub']
```

If none of these are provided, it doesn't break anything - sails will load the hook in normal flow.

Another change that this PR brings is that I had to do away with `userhook` hook. The way that worked up until now did not allow user-defined hooks to register themselves to load before any of the built-in hooks.

Instead of defining a separate `userhook` to load user-defined hooks, I decided to always load `moduleloader` as the first hook (if enabled) and then build the dictionary of user hooks and mix them in with built-in hooks as the second step.

Once these are done, ALL hooks, both built-in and user-defined go through preparation and initialization, which builds the acyclic load/dependency graph which is given to `async.auto` to actually load and initialize the hooks.

All-in-all, this PR places most of the heavy-lifting to `loadHooks.js` to make adding hooks easier and more powerful.