UNPKG

angular-localforage-setitems

Version:

Angular service & directive for https://github.com/mozilla/localForage (Offline storage, improved.) with added performant setItems function

16 lines 10.1 kB
/** * Jack in the Box inc. * We added a more performant setItems() function into angular-localForage * * angular-localforage-setitems - Angular service & directive for https://github.com/mozilla/localForage (Offline storage, improved.) with added performant setItems function * @version v1.0.0 * @link https://github.com/jackintheboxxd/angular-localForage-setItems * @license MIT * @author Christopher Soto <christopher.soto@jackinthebox.com> * * FORKED FROM: * @link https://github.com/ocombe/angular-localForage * @license MIT * @author Olivier Combe <olivier.combe@gmail.com> */ !function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("localforage")):"function"==typeof define&&define.amd?define(["exports","localforage"],r):r(e.localforageSetItems=e.localforageSetItems||{},e.localforage)}(this,function(e,r){"use strict";function t(e){return t.result?t.result:(e&&"function"==typeof e.getSerializer||Promise.reject(new Error("localforage.getSerializer() was not available! localforage v1.4+ is required!")),t.result=e.getSerializer(),t.result)}function n(e,r){r&&e.then(function(e){r(null,e)},function(e){r(e)})}function o(e,r,t,n){function o(e,r){var t=e||r;return e&&"function"==typeof e||"string"!=typeof t||(e=function(e){return e[t]}),e}var i=[];if("[object Array]"===Object.prototype.toString.call(e)){r=o(r,"key"),t=o(t,"value");for(var a=0,f=e.length;f>a;a++){var s=e[a];i.push(n(r(s),t(s)))}}else for(var u in e)e.hasOwnProperty(u)&&i.push(n(u,e[u]));return i}function i(e,r,t,i){var a=this,f=a.ready().then(function(){return new Promise(function(n,i){function f(e){var r=e.target||this;s=r.error||r.transaction.error,i(s)}var s,u=a._dbInfo,c=u.db.transaction(u.storeName,"readwrite"),l=c.objectStore(u.storeName);c.oncomplete=function(){n(e)},c.onabort=c.onerror=function(e){i(s||e.target)},o(e,r,t,function(e,r){null===r&&(r=void 0);var t=l.put(r,e);t.onerror=f})})});return n(f,i),f}function a(e,r,i,a){var f=this,s=new Promise(function(n,a){f.ready().then(function(){return t(f)}).then(function(t){var s=f._dbInfo;s.db.transaction(function(f){var u="INSERT OR REPLACE INTO "+s.storeName+" (key, value) VALUES (?, ?)",c=o(e,r,i,function(e,r){return new Promise(function(n,o){t.serialize(r,function(r,t){t?o(t):f.executeSql(u,[e,r],function(){n()},function(e,r){o(r)})})})});Promise.all(c).then(function(){n(e)},a)},function(e){a(e)})})["catch"](a)});return n(s,a),s}function f(e,r,t,i){var a=this,f=o(e,r,t,function(e,r){return a.setItem(e,r)}),s=Promise.all(f);return n(s,i),s}function s(e,r,t,n){var o=this,s=o.driver();return s===o.INDEXEDDB?i.call(o,e,r,t,n):s===o.WEBSQL?a.call(o,e,r,t,n):f.call(o,e,r,t,n)}function u(e){var r=Object.getPrototypeOf(e);r&&(r.setItems=s,r.setItems.indexedDB=function(){return i.apply(this,arguments)},r.setItems.websql=function(){return a.apply(this,arguments)},r.setItems.generic=function(){return f.apply(this,arguments)})}r="default"in r?r["default"]:r;var c=u(r);e.setItemsGeneric=f,e.localforageSetItems=s,e.extendPrototype=u,e.extendPrototypeResult=c}),function(e,r){"use strict";if("function"==typeof define&&define.amd)define(["localforage"],function(t){r(e.angular,t)});else{if("object"!=typeof exports&&"object"!=typeof global)return r(e.angular,e.localforage);var t=e.angular||window&&window.angular;module.exports=r(t,require("localforage"))}}(this,function(e,r,t){"use strict";var n=e.module("LocalForageModule",["ng"]);return n.provider("$localForage",function(){var t={},n={name:"lf"},o={setItem:!1,removeItem:!1},i={};this.setNotify=function(e,r){o={setItem:e,removeItem:r}},this.config=function(r){if(!e.isObject(r))throw new Error("The config parameter should be an object");e.extend(n,r)},this.$get=["$rootScope","$q","$parse",function(a,f,s){function u(r){for(var t in r)if(r.hasOwnProperty(t))return!1;return!0&&e.toJson(r)===e.toJson({})}var c=function(t){e.isDefined(t)?this._localforage=r.createInstance(t):(this._localforage=r,r.config(n))};c.prototype.createInstance=function(r){if(e.isObject(r)){if(r=e.extend({},n,r),e.isDefined(t[r.name]))throw new Error("A localForage instance with the name "+r.name+" is already defined.");return t[r.name]=new c(r),t[r.name]}throw new Error("The parameter should be a config object.")},c.prototype.instance=function(r){if(e.isUndefined(r))return t[n.name];if(e.isString(r)){if(e.isDefined(t[r]))return t[r];throw new Error("No localForage instance of that name exists.")}throw new Error("The parameter should be a string.")},c.prototype.setDriver=function(e){return this._localforage.setDriver(e)},c.prototype.driver=function(){return this._localforage.driver()},c.prototype.setItem=function(r,t){if(e.isUndefined(r))throw new Error("You must define a key to set");var n=this;if(e.isArray(r)){if(!e.isArray(t))throw new Error("If you set an array of keys, the values should be an array too");var i=[];return e.forEach(r,function(e,r){i.push(n.setItem(e,t[r]))}),f.all(i)}var s=f.defer(),u=arguments,c="undefined"!=typeof Blob&&"undefined"!=typeof ArrayBuffer&&(t instanceof Blob||t instanceof ArrayBuffer)?t:e.copy(t);return e.isObject(c)&&e.isDefined(c.$promise)&&delete c.$promise,n._localforage.setItem(n.prefix()+r,c).then(function(){o.setItem&&a.$broadcast("LocalForageModule.setItem",{key:r,newvalue:c,driver:n.driver()}),s.resolve(c)},function(e){n.onError(e,u,n.setItem,s)}),s.promise},c.prototype.setItems=function(r){if(e.isUndefined(r))throw new Error("You must define a key to set");var t=this,n=f.defer(),i=arguments,s=e.copy(r);return console.log(t),e.isArray(r)||e.isObject(r)?(u(r)?n.resolve("empty"):(e.isObject(s)&&e.isDefined(s.$promise)&&delete s.$promise,t._localforage.setItems(s).then(function(){o.setItems&&a.$broadcast("LocalForageModule.setItems",{newValue:s,driver:t.driver()}),n.resolve(s)},function(e){t.onError(e,i,t.setItems,n)})),n.promise):void 0},c.prototype.getItem=function(r){if(e.isUndefined(r))throw new Error("You must define a key to get");var t,n=f.defer(),o=arguments,i=this;if(e.isArray(r)){var a=[],s=0;t=i._localforage.iterate(function(e,t){var n=r.indexOf(i.prefix()+t);return n>-1&&(a[n]=e,s++),s===r.length?a:void 0}).then(function(){for(var t=0;t<r.length;t++)e.isUndefined(a[t])&&(a[t]=null);n.resolve(a)})}else t=i._localforage.getItem(i.prefix()+r).then(function(e){n.resolve(e)});return t.then(null,function(e){i.onError(e,o,i.getItem,n)}),n.promise},c.prototype.iterate=function(r){if(e.isUndefined(r))throw new Error("You must define a callback to iterate");var t=f.defer(),n=arguments,o=this;return o._localforage.iterate(r).then(function(e){t.resolve(e)},function(e){o.onError(e,n,o.iterate,t)}),t.promise},c.prototype.removeItem=function(r){if(e.isUndefined(r))throw new Error("You must define a key to remove");var t=this;if(e.isArray(r)){var n=[];return e.forEach(r,function(e,r){n.push(t.removeItem(e))}),f.all(n)}var i=f.defer(),s=arguments;return t._localforage.removeItem(t.prefix()+r).then(function(){o.removeItem&&a.$broadcast("LocalForageModule.removeItem",{key:r,driver:t.driver()}),i.resolve()},function(e){t.onError(e,s,t.removeItem,i)}),i.promise},c.prototype.pull=function(r){if(e.isUndefined(r))throw new Error("You must define a key to pull");var t=this,n=f.defer(),o=function(e){n.reject(e)};return t.getItem(r).then(function(e){t.removeItem(r).then(function(){n.resolve(e)},o)},o),n.promise},c.prototype.clear=function(){var e=f.defer(),r=arguments,t=this;return t._localforage.clear().then(function(r){e.resolve()},function(n){t.onError(n,r,t.clear,e)}),e.promise},c.prototype.key=function(r){if(e.isUndefined(r))throw new Error("You must define a position to get for the key function");var t=f.defer(),n=arguments,o=this;return o._localforage.key(r).then(function(e){t.resolve(e)},function(e){o.onError(e,n,o.key,t)}),t.promise};var l=function(){var e=f.defer(),r=arguments,t=this;return t._localforage.keys().then(function(r){if(n.oldPrefix&&"localStorageWrapper"===t.driver()){for(var o=[],i=0,a=r.length;a>i;i++)o.push(r[i].substr(t.prefix().length,r[i].length));r=o}e.resolve(r)},function(n){t.onError(n,r,t.keys,e)}),e.promise};return c.prototype.keys=l,c.prototype.getKeys=l,c.prototype.length=function(){var e=f.defer(),r=arguments,t=this;return t._localforage.length().then(function(r){e.resolve(r)},function(n){t.onError(n,r,length,e)}),e.promise},c.prototype.bind=function(r,o){if(e.isString(o))o={key:o};else if(!e.isObject(o)||e.isUndefined(o.key))throw new Error("You must define a key to bind");var a={defaultValue:"",name:n.name};o=e.extend({},a,o);var f=t[o.name];if(e.isUndefined(f))throw new Error("You must use the name of an existing instance");var u=o.scopeKey||o.key,c=s(u);return f.getItem(o.key).then(function(t){return null!==t?c.assign(r,t):e.isUndefined(o.defaultValue)||(c.assign(r,o.defaultValue),f.setItem(o.key,o.defaultValue)),e.isDefined(i[o.key])&&i[o.key](),i[o.key]=r.$watch(u,function(r){e.isDefined(r)&&f.setItem(o.key,r)},!0),t})},c.prototype.unbind=function(r,o){if(e.isString(o))o={key:o};else if(!e.isObject(o)||e.isUndefined(o.key))throw new Error("You must define a key to unbind");var a={scopeKey:o.key,name:n.name};o=e.extend({},a,o);var f=t[o.name];if(e.isUndefined(f))throw new Error("You must use the name of an existing instance");return s(o.scopeKey).assign(r,null),e.isDefined(i[o.key])&&(i[o.key](),delete i[o.key]),f.removeItem(o.key)},c.prototype.prefix=function(){return"localStorageWrapper"===this.driver()&&n.oldPrefix?this._localforage.config().name+".":""},c.prototype.onError=function(r,t,n,o){if((e.isObject(r)&&r.name?"InvalidStateError"===r.name:e.isString(r)&&"InvalidStateError"===r)&&"asyncStorage"===this.driver()||e.isObject(r)&&r.code&&5===r.code){var i=this;i.setDriver("localStorageWrapper").then(function(){n.apply(i,t).then(function(e){o.resolve(e)},function(e){o.reject(e)})},function(){o.reject(r)})}else o.reject(r)},t[n.name]=new c,t[n.name]}]}),n.directive("localForage",["$localForage",function(r){return{restrict:"A",link:function(t,n,o){var i=t.$eval(o.localForage);e.isObject(i)&&e.isDefined(i.key)?r.bind(t,i):r.bind(t,o.localForage)}}}]),n.name});