view js/modules/cache.js @ 100:c486d35fad27

added more mocks; black-box now talks to a really simple fake bugzilla 'server'.
author Atul Varma <avarma@mozilla.com>
date Tue, 27 Apr 2010 23:06:29 -0700
parents 00d23d6d41b4
children
line wrap: on
line source

Require.modules["cache/html5"] = function(exports, require) {
  exports.create = function create(name, storage) {
    var cache = storage.getItem(name);

    if (cache)
      cache = JSON.parse(cache);
    else
      cache = {};

    return {
      set: function set(key, value) {
        cache[key] = value;

        // Remove the key first, to get around a strange iPad
        // issue: http://stackoverflow.com/questions/2603682/is-anyone-else-receiving-a-quota-exceeded-err-on-their-ipad-when-accessing-locals
        storage.removeItem(name);

        // TODO: We should really catch QUOTA_EXCEEDED_ERR here,
        // which could be thrown if the user is in private
        // browsing mode.
        storage.setItem(name, JSON.stringify(cache));
      },

      get: function get(key) {
        return cache[key];
      },

      clear: function clear() {
        storage.removeItem(name);
        cache = {};
      }
    };
  };
};