UNPKG

8.76 kBPlain TextView Raw
1{"code":"function defineName$__(cls,name){\n\tObject.defineProperty(cls,\"name\",{value:name,configurable:true});\n};\nconst $__initor__$ = Symbol.for('#__initor__'), $__inited__$ = Symbol.for('#__inited__'), $__hooks__$ = Symbol.for('#__hooks__');\n\n/*body*/\nimport {commit} from './scheduler';\n\nconst fn = function() { return true; };\n\nclass Storage {\n\t\n\tconstructor(store,ns = ''){\n\t\t\n\t\tthis.store = store || {};\n\t\tthis.cache = {raw: {},rich: {}};\n\t\tthis.ns = ns;\n\t\tthis.local = store == globalThis.sessionStorage;\n\t\tthis.children = new Map;\n\t}\n\t\n\tserialize(val,key){\n\t\t\n\t\treturn JSON.stringify(val);\n\t}\n\t\n\tdeserialize(val,key){\n\t\t\n\t\treturn JSON.parse(val);\n\t}\n\t\n\tgetItem(name){\n\t\t\n\t\tlet key = String(this.ns) + ':' + name;\n\t\t\n\t\tif (this.store instanceof Storage) {\n\t\t\t\n\t\t\treturn this.store.getItem(key);\n\t\t};\n\t\t\n\t\tif (this.local && this.cache.rich[key] != undefined) {\n\t\t\t\n\t\t\treturn this.cache.rich[key];\n\t\t};\n\t\t\n\t\tlet raw = this.store.getItem(key);\n\t\t// if we are\n\t\tif (raw != undefined) {\n\t\t\t\n\t\t\tif (this.cache.raw[key] == raw) {\n\t\t\t\t\n\t\t\t\treturn this.cache.rich[key];\n\t\t\t};\n\t\t\t\n\t\t\tthis.cache.raw[key] = raw;\n\t\t\treturn this.cache.rich[key] = this.deserialize(raw,key);\n\t\t};\n\t}\n\t\n\tsetItem(name,value){\n\t\tvar $1, $2;\n\t\t\n\t\tlet key = String(this.ns) + ':' + name;\n\t\t\n\t\tif (this.store instanceof Storage) {\n\t\t\t\n\t\t\treturn this.store.setItem(key,value);\n\t\t};\n\t\t\n\t\tlet cached = this.cache.rich[key];\n\t\tlet typ = typeof value;\n\t\t\n\t\tif (this.local) {\n\t\t\t\n\t\t\tthis.cache.rich[key] = value;\n\t\t};\n\t\t\n\t\tif (cached !== value || typ == 'object') {\n\t\t\t\n\t\t\tlet prev = this.store.getItem(key);\n\t\t\t\n\t\t\tif (value == undefined) {\n\t\t\t\t\n\t\t\t\tthis.store.removeItem(key);\n\t\t\t\t((($1 = this.cache.rich[key]),delete this.cache.rich[key], $1));\n\t\t\t\t((($2 = this.cache.raw[key]),delete this.cache.raw[key], $2));\n\t\t\t\treturn commit();\n\t\t\t} else {\n\t\t\t\t\n\t\t\t\tlet raw = this.serialize(value,key);\n\t\t\t\tthis.cache.rich[key] = value;\n\t\t\t\tthis.cache.raw[key] = raw;\n\t\t\t\t\n\t\t\t\tif (prev != raw) {\n\t\t\t\t\t\n\t\t\t\t\tthis.store.setItem(key,raw);\n\t\t\t\t\treturn commit();\n\t\t\t\t};\n\t\t\t};\n\t\t};\n\t}\n\t\n\tget(target,key,receiver){\n\t\t\n\t\treturn this.getItem(key);\n\t}\n\t\n\tset(target,key,value,receiver){\n\t\t\n\t\tthis.setItem(key,value);\n\t\treturn true;\n\t}\n\t\n\tapply(target,that,[name]){\n\t\t\n\t\tlet item = this.children.get(name);\n\t\titem || this.children.set(name,item = new Proxy(fn,new Storage(this,name)));\n\t\treturn item;\n\t}\n\t\n\tdeleteProperty(target,name){\n\t\tvar $3;\n\t\t\n\t\tlet key = this.ns + name;\n\t\t((($3 = this.cache[key]),delete this.cache[key], $3));\n\t\treturn this.store.removeItem(key);\n\t}\n\tstatic {\n\t\tdefineName$__(this,'Storage');}\n};\n\nexport const locals = new Proxy(fn,new Storage(globalThis.localStorage));\n\nexport const session = new Proxy(fn,new Storage(globalThis.sessionStorage));\n","map":{"version":3,"file":"storage.js","sourceRoot":"","sources":["/Users/abdellah/workspace/scrimba/imba/packages/imba/src/imba/storage.imba"],"sourcesContent":["import {commit} from './scheduler'\n\nconst fn = do yes\n\nclass Storage\n\tdef constructor store, ns = ''\n\t\tself.store = store or {}\n\t\tself.cache = {raw:{},rich:{}}\n\t\tself.ns = ns\n\t\tself.local = store == global.sessionStorage\n\t\tself.children = new Map\n\n\tdef serialize val, key\n\t\tJSON.stringify(val)\n\n\tdef deserialize val, key\n\t\tJSON.parse(val)\n\n\tdef getItem name\n\t\tlet key = String(ns) + ':' + name\n\n\t\tif store isa Storage\n\t\t\treturn store.getItem(key)\n\n\t\tif local and cache.rich[key] != undefined\n\t\t\treturn cache.rich[key]\n\n\t\tlet raw = store.getItem(key)\n\t\t# if we are\n\t\tif raw != undefined\n\t\t\tif cache.raw[key] == raw\n\t\t\t\treturn cache.rich[key]\n\n\t\t\tcache.raw[key] = raw\n\t\t\treturn cache.rich[key] = deserialize(raw,key)\n\n\tdef setItem name, value\n\t\tlet key = String(ns) + ':' + name\n\n\t\tif store isa Storage\n\t\t\treturn store.setItem(key,value)\n\n\t\tlet cached = cache.rich[key]\n\t\tlet typ = typeof value\n\n\t\tif local\n\t\t\tcache.rich[key] = value\n\n\t\tif cached !== value or typ == 'object'\n\t\t\tlet prev = store.getItem(key)\n\n\t\t\tif value == undefined\n\t\t\t\tstore.removeItem(key)\n\t\t\t\tdelete cache.rich[key]\n\t\t\t\tdelete cache.raw[key]\n\t\t\t\tcommit!\n\t\t\telse\n\t\t\t\tlet raw = serialize(value,key)\n\t\t\t\tcache.rich[key] = value\n\t\t\t\tcache.raw[key] = raw\n\n\t\t\t\tif prev != raw\n\t\t\t\t\tstore.setItem(key,raw)\n\t\t\t\t\tcommit!\n\n\tdef get target,key,receiver\n\t\tgetItem(key)\n\n\tdef set target,key,value,receiver\n\t\tsetItem(key,value)\n\t\treturn yes\n\n\tdef apply target, that, [name]\n\t\tlet item = children.get(name)\n\t\titem || children.set(name,item = new Proxy(fn,new Storage(self,name)))\n\t\treturn item\n\n\tdef deleteProperty target, name\n\t\tlet key = ns + name\n\t\tdelete cache[key]\n\t\treturn store.removeItem(key)\n\nexport const locals = new Proxy(fn,new Storage(global.localStorage))\n\nexport const session = new Proxy(fn,new Storage(global.sessionStorage))"],"names":[],"mappings":";;;;;;AAAA,MAAM,EAAE,MAAM,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