b9dc48316a1fb575aa2f88a1667bc4c02d7a316a84a2d238cf32a3c6173bb3074080b6719f733b8c0756a368225b76e9ed4166687be32a4485b6a32c0d4000 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * Copyright (c) 2013 ESHA Research
  3. * Dual licensed under the MIT and GPL licenses:
  4. * http://www.opensource.org/licenses/mit-license.php
  5. * http://www.gnu.org/licenses/gpl.html
  6. *
  7. * Bind handlers to quota errors:
  8. * store.quota(function(e, area, key, str) {
  9. * console.log(e, area, key, str);
  10. * });
  11. * If a handler returns true other handlers are not called and
  12. * the error is suppressed.
  13. *
  14. * Think quota errors will never happen to you? Think again:
  15. * http://spin.atomicobject.com/2013/01/23/ios-private-browsing-localstorage/
  16. * (this affects sessionStorage too)
  17. *
  18. * Status: ALPHA - API could use unbind feature
  19. */
  20. ;(function(store, _) {
  21. store.quota = function(fn) {
  22. store.quota.fns.push(fn);
  23. };
  24. store.quota.fns = [];
  25. var _set = _.set;
  26. _.set = function(area, key, str) {
  27. try {
  28. _set.apply(this, arguments);
  29. } catch (e) {
  30. if (e.name === 'QUOTA_EXCEEDED_ERR' ||
  31. e.name === 'NS_ERROR_DOM_QUOTA_REACHED') {
  32. var fns = store.quota.fns;
  33. for (var i=0,m=fns.length; i<m; i++) {
  34. if (true === fns[i].call(this, e, area, key, str)) {
  35. return;
  36. }
  37. }
  38. }
  39. throw e;
  40. }
  41. };
  42. })(window.store, window.store._);