UNPKG

type-r2

Version:

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

3 lines (2 loc) 1.62 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).memoryIO={},t.Nested)}(this,function(t,e){"use strict";function i(t,e){return new r(t=void 0===t?[]:t,e=void 0===e?50:e)}n.prototype.resolve=function(i){var r=this;return e.createIOPromise(function(t,e){setTimeout(function(){return t(i)},r.delay)})},n.prototype.reject=function(i){var r=this;return e.createIOPromise(function(t,e){setTimeout(function(){return e(i)},r.delay)})},n.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]++)},n.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})},n.prototype.update=function(t,e,i){return this.items[t]=e,this.resolve({})},n.prototype.read=function(t,e){t=this.items[t];return t?this.resolve(t):this.reject("Not found")},n.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")},n.prototype.list=function(t){var e=this;return this.resolve(this.index.slice(1).map(function(t){return e.items[t]}))},n.prototype.subscribe=function(t){},n.prototype.unsubscribe=function(t){};var r=n;function n(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,{})}}t.MemoryEndpoint=r,t.create=i,t.memoryIO=i}); //# sourceMappingURL=index.js.map