/***
 * Copyright (c) 2015 Alex Grant (@localnerve), LocalNerve LLC
 * Copyrights licensed under the BSD License. See the accompanying LICENSE file for terms.
 *
 * Handlers to fulfill service worker message commands.
 */
'use strict';

var toolbox = require('sw-toolbox');
var backgrounds = require('./backgrounds');
var routes = require('./routes');
var idb = require('./idb');
var content = require('./content');
var debug = require('./debug')('handler');

var commands = {
  init: init,
  dumpCache: dumpCache
};

/**
 * Handle init command.
 * Gets executed every app load (once per session).
 * Installs background and route fetch handling, additional precaching.
 *
 * @param {Object} payload - Initial store state
 * @param {Function} responder - Function to call to resolve the message
 */
function init (payload, responder) {
  debug(toolbox.options, 'init command handler');

  idb.put('init', 'stores', payload)
  .then(function () {
    return content.storeOnlineContent(payload);
  })
  .then(function () {
    return backgrounds(payload);
  })
  .then(function () {
    return routes(payload);
  })
  .then(function () {
    responder({
      error: null
    });
  })
  .catch(function (error) {
    debug(toolbox.options, 'init failed', error);
    responder({
      error: error.toString()
    });
  });
}

// This is just for dumpCache
var helpers = require('sw-toolbox/lib/helpers');
/**
 * Dump cache governed by sw-toolbox.
 *
 * TODO: this is just for debugging, make conditional soon
 */
function dumpCache () {
  helpers.openCache().then(function (cache) {
    cache.keys().then(function (keys) {
      console.log('[sw-toolbox] cache');
      keys.forEach(function (key) {
        cache.match(key).then(function (value) {
          console.log('[sw cache] key:', key, ' value:', value);
        });
      });
    });
  });
}

/**
 * Handle unknown commands
 */
function unknownCommand (payload, responder) {
  responder({
    error: 'Unknown command received by service worker.'
  });
}

module.exports = function run (command, payload, responder) {
  var handler = commands[command] || unknownCommand;
  return handler(payload, responder);
};

@@ -539,16 +539,19 @@
 ommand.%0A
+ *%0A
  * Gets 
@@ -594,16 +594,65 @@
 ssion).%0A
+ * Gets executed at the beginning of sw load.%0A *%0A
  * Insta
@@ -715,16 +715,79 @@
 aching.%0A
+ * Updates the initial stores and content stored in IndexedDB.%0A
  *%0A * @p
@@ -1990,16 +1990,141 @@
 ommands%0A
+ *%0A * @param %7BObject%7D payload - Initial store state%0A * @param %7BFunction%7D responder - Function to call to resolve the message%0A
  */%0Afunc
@@ -2246,16 +2246,41 @@
  %7D);%0A%7D%0A%0A
+/**%0A * Run a command%0A */%0A
 module.e

update comments