A chain reaction system

by Eugene Trounev

HTML

<h1>
  Hooks
</h1>
<p>
  A chain reaction system
</p>
<p>
  Execute a series of links in a chain for an action and return the final result as a promise
</p>
<h2>
  Actions
</h2>
<pre>
  <b>Actions</b>
  actions.document.failed ({response})
  actions.document.received ({response})
  actions.document.markup ({document})
  actions.document.index ({index})
  actions.document.toc ({list})
  
  actions.navigation ({from, to, prevented})
  
  actions.ui.style ({style})
  actions.ui.rendered ({state})
  actions.ui.rendered.sidebar ()
  actions.ui.rendered.sidebar.header ()
  actions.ui.rendered.sidebar.toc ({toc})
  actions.ui.rendered.sidebar.footer ()
  actions.ui.rendered.document ({document})
  actions.ui.rendered.document.header ({header})
  actions.ui.rendered.document.section ({id, type, content})
  actions.ui.rendered.document.article ({article})
  actions.ui.rendered.document.footer ()
  actions.ui.rendered.document.aside ()
  
  
  <b>Filters</b>
  filters.document.receive ({response})
  filters.document.markup ({document})
  filters.document.index ({index})
  filters.document.toc ({list})
  
  filters.navigation ({from, to, prevent})
  
  filters.ui.style({style})
  filters.ui.header.plugins ({plugins: []})
  filters.ui.sidebar.header.plugins ({plugins: []})
  filters.ui.sidebar.toc({toc})
  filters.ui.sidebar.toc.item({item})
  filters.ui.sidebar.footer.plugins ({plugins: []})
  filters.ui.document.header({header})
  filters.ui.document.header.plugins ({...article,plugins: []})
  filters.ui.document.article.plugins ({...article,plugins: []})
  filters.ui.document.article ({data})
  filters.ui.document.section.plugins ({...section,plugins: []})
  filters.ui.document.section ({section})
  filters.ui.document.footer.plugins ({...document,plugins: []})
  filters.ui.document.aside.plugins ({...document,plugins: []})
  filters.ui.footer ({ plugins: []})
  
  <b>Triggers</b>
  rtigger.document.fetch(documentId)
  rtigger.document.parse({document})
  
 ...

JavaScript 1.7

/**
 * A chain reaction system
 *  Execute a series of links in a chain for an action and return the final result as a promise
 * @object Hooks
 */
let _hooks = {
  "_": {
    n: "general",
    i: "generic aciton namespace",
    c: []
  }
};
const _link = p => Promise.resolve(p);
const Hooks = {
  /**
   * Register action others can hook into
   *
   * @param {string} - action name (example: "ui.render")
   * @param {string} [description=""] - action description that gives others a brief information about what the action is intended for
   * @memberof Hooks
   */
  register(action, description = "") {
    if (this.exists(action)) throw "Hook already registered in the arae requested";
    _hooks[action] = {
      n: action, // name
      i: description, // description
      c: [] // chain of links
    };
  },
  /**
   * Check is action has been registered
   *
   * @param {string} action - action name
   * @returns {boolean} - returns true if action with the provided name has been registered, otherwise returns false
   * @memberof Hooks
   */
  exists(action) {
    return typeof _hooks[action] === "object";
  },
  /**
   * Returns short description for the action
   *
   * @param {string} action - action name
   * @returns {string} - returns a short description for an action
   * @memberof Hooks
   */
  info(action) {
    if (!this.exists(action)) throw "Hook for this action is not regitered";
    return _hooks[action].i;
  },
  /**
   * List available actions with descriptions
   *
   * @returns {Array} - returns a list of registered actions actions in format [{name: "name", description: "description", chain: [Link{link: function.toString(), priority: 0, description: "description"}, ...]}, ...]
   * @memberof Hooks
   */
  list() {
    return Object.keys(_hooks).map(
      key => ({
        name: _hooks[key].n,
        description: _hooks[key].i,
        chain: _hooks[key].c.map(({
          l,
          i,
          d
        }) => ({
          link: l.name ||...