type-r
Version:
Serializable, validated, and observable data layer for modern JS applications
3 lines (2 loc) • 1.66 kB
JavaScript
!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=t||self).memoryIO={},t.Nested)}(this,function(t,e){"use strict";function i(t,e){return void 0===t&&(t=[]),void 0===e&&(e=50),new r(t,e)}var r=function(){function t(t,e){this.delay=e,this.index=[0],this.items={};for(var i=0,r=t;i<r.length;i++){var n=r[i];this.create(n,{})}}return t.prototype.resolve=function(i){var r=this;return e.createIOPromise(function(t,e){setTimeout(function(){return t(i)},r.delay)})},t.prototype.reject=function(i){var r=this;return e.createIOPromise(function(t,e){setTimeout(function(){return e(i)},r.delay)})},t.prototype.generateId=function(t){var e=Number(t);return isNaN(e)||(this.index[0]=Math.max(this.index[0],e)),t||String(this.index[0]++)},t.prototype.create=function(t,e){var i=t.id=this.generateId(t.id);return this.index.push(i),this.items[i]=t,this.resolve({id:i})},t.prototype.update=function(t,e,i){return this.items[t]=e,this.resolve({})},t.prototype.read=function(t,e){var i=this.items[t];return i?this.resolve(i):this.reject("Not found")},t.prototype.destroy=function(e,t){return this.items[e]?(delete this.items[e],this.index=this.index.filter(function(t){return t!==e}),this.resolve({})):this.reject("Not found")},t.prototype.list=function(t){var e=this;return this.resolve(this.index.slice(1).map(function(t){return e.items[t]}))},t.prototype.subscribe=function(t){},t.prototype.unsubscribe=function(t){},t}();t.create=i,t.memoryIO=i,t.MemoryEndpoint=r,Object.defineProperty(t,"__esModule",{value:!0})});
//# sourceMappingURL=index.js.map