UNPKG

type-r2

Version:

Serializable, validated, and observable data layer for modern JS applications

3 lines (2 loc) 1.74 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("type-r")):"function"==typeof define&&define.amd?define(["exports","type-r"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).localStorageIO={},t.Nested)}(this,function(t,e){"use strict";function o(t){return new i(t)}n.prototype.resolve=function(o){return e.createIOPromise(function(t,e){setTimeout(function(){t(o)},0)})},n.prototype.reject=function(o){return e.createIOPromise(function(t,e){setTimeout(function(){return e(o)},0)})},n.prototype.create=function(t,e){var o=this.index;return o.push(t.id=String(o[0]++)),this.index=o,this.set(t),this.resolve({id:t.id})},n.prototype.set=function(t){localStorage.setItem(this.key+"#"+t.id,JSON.stringify(t))},n.prototype.get=function(t){return JSON.parse(localStorage.getItem(this.key+"#"+t))},n.prototype.update=function(t,e,o){return e.id=t,this.set(e),this.resolve({})},n.prototype.read=function(t,e){t=this.get(t);return t?this.resolve(t):this.reject("Not found")},n.prototype.destroy=function(e,t){return this.get(e)?(localStorage.removeItem(this.key+"#"+e),this.index=this.index.filter(function(t){return t!==e}),this.resolve({})):this.reject("Not found")},Object.defineProperty(n.prototype,"index",{get:function(){return JSON.parse(localStorage.getItem(this.key))||[0]},set:function(t){localStorage.setItem(this.key,JSON.stringify(t))},enumerable:!1,configurable:!0}),n.prototype.list=function(t){var e=this;return this.index,this.resolve(this.index.slice(1).map(function(t){return e.get(t)}))},n.prototype.subscribe=function(t){},n.prototype.unsubscribe=function(t){};var i=n;function n(t){this.key=t}t.LocalStorageEndpoint=i,t.create=o,t.localStorageIO=o}); //# sourceMappingURL=index.js.map