UNPKG

8.31 kBPlain TextView Raw
1{"code":"\n/*body*/\n\nexport function αlazy(target,key,desc){\n\t\n\tif (desc.get) {\n\t\t\n\t\tlet get = desc.get;\n\t\tdesc.get = function() {\n\t\t\t\n\t\t\tlet val = get.call(this);\n\t\t\tObject.defineProperty(this,key,{enumerable: false,value: val});\n\t\t\treturn val;\n\t\t};\n\t};\n\treturn desc;\n};\n\nexport function αbound(target,key,desc){\n\tvar $1, $2;\n\t\n\tif (desc.value instanceof Function) {\n\t\t\n\t\tlet fn = desc.value;\n\t\tdesc.get = function() {\n\t\t\t\n\t\t\tif (this == target) {\n\t\t\t\t\n\t\t\t\treturn fn;\n\t\t\t};\n\t\t\tlet val = fn.bind(this);\n\t\t\tObject.defineProperty(this,key,{enumerable: false,value: val});\n\t\t\treturn val;\n\t\t};\n\t\t((($1 = desc.value),delete desc.value, $1));\n\t\t((($2 = desc.writable),delete desc.writable, $2));\n\t};\n\treturn desc;\n};\n\n\nconst thenables = new WeakMap;\n\nexport function αthenable(target,key,desc){\n\t\n\t\n\tlet val = desc.value;\n\tlet sym = Symbol();\n\tlet thensym = Symbol();\n\t\n\tlet meta = thenables.get(target);\n\tmeta || thenables.set(target,meta = []);\n\t\n\tif (meta.length) {\n\t\t\n\t\tthrow new Error((`@thenable ` + (target.constructor?.name) + `.` + key + ` not allowed - @thenable ` + (meta[0][0]) + ` already defined`));\n\t};\n\t\n\tconst warn = function() {\n\t\t\n\t\treturn console.trace((`@thenable ` + (target.constructor?.name) + `.` + key + ` takes long - make sure method does not return self`));\n\t};\n\t\n\tmeta.push([key,sym]);\n\t\n\tif (val instanceof Function) {\n\t\t\n\t\tif (val.length > 0) {\n\t\t\t\n\t\t\tthrow new Error((`@thenable methods cannot be called with arguments (` + key + `)`));\n\t\t};\n\t\t\n\t\tObject.defineProperty(target,'then',{\n\t\t\tenumerable: false,\n\t\t\tvalue: function(ok,err) {\n\t\t\t\t\n\t\t\t\tlet that = this;\n\t\t\t\t\n\t\t\t\tlet promise = this[thensym] ||= new Promise(function(resolve,reject) {\n\t\t\t\t\t\n\t\t\t\t\t// TODO should only happen in debug\n\t\t\t\t\tlet timeout = setTimeout(warn,(2 * 1000));\n\t\t\t\t\t\n\t\t\t\t\tlet err = function(error) {\n\t\t\t\t\t\t\n\t\t\t\t\t\tclearTimeout(timeout);\n\t\t\t\t\t\treturn reject(error);\n\t\t\t\t\t};\n\t\t\t\t\t\n\t\t\t\t\treturn that[key]().then(function() {\n\t\t\t\t\t\t\n\t\t\t\t\t\tclearTimeout(timeout);\n\t\t\t\t\t\tObject.defineProperty(that,'then',{value: null,writable: true,configurable: true});\n\t\t\t\t\t\treturn resolve(that);\n\t\t\t\t\t},err);\n\t\t\t\t});\n\t\t\t\t\n\t\t\t\treturn promise.then(ok,err);\n\t\t\t}\n\t\t});\n\t\t\n\t\tdesc.value = function(value) {\n\t\t\t\n\t\t\treturn this[sym] ||= Promise.resolve(val.call(this));\n\t\t};\n\t\t\n\t\t// You can reset thenables explicitly by calling instance.myfunction.reset(instance)\n\t\tdesc.value.reset = function(target) {\n\t\t\tvar $3;\n\t\t\t\n\t\t\tif (target && target[sym]) {\n\t\t\t\t\n\t\t\t\ttarget[sym] = null;\n\t\t\t\ttarget[thensym] = null;\n\t\t\t\treturn ((($3 = target.then),delete target.then, $3));\n\t\t\t};\n\t\t};\n\t};\n\t\n\treturn desc;\n};\n","map":{"version":3,"file":"decorators.js","sourceRoot":"","sources":["/Users/abdellah/workspace/scrimba/imba/packages/imba/src/imba/decorators.imba"],"sourcesContent":["\nexport def @lazy target, key, desc\n\tif desc.get\n\t\tlet get = desc.get\n\t\tdesc.get = do\n\t\t\tlet val = get.call(this)\n\t\t\tObject.defineProperty(this,key,{enumerable: no, value: val})\n\t\t\treturn val\n\treturn desc\n\nexport def @bound target, key, desc\n\tif desc.value isa Function\n\t\tlet fn = desc.value\n\t\tdesc.get = do\n\t\t\tif this == target\n\t\t\t\treturn fn\n\t\t\tlet val = fn.bind(this)\n\t\t\tObject.defineProperty(this,key,{enumerable: no, value: val})\n\t\t\treturn val\n\t\tdelete desc.value\n\t\tdelete desc.writable\n\treturn desc\n\n\nconst thenables = new WeakMap\n\nexport def @thenable target, key, desc\n\n\tlet val = desc.value\n\tlet sym = Symbol!\n\tlet thensym = Symbol!\n\n\tlet meta = thenables.get(target)\n\tmeta || thenables.set(target,meta = [])\n\n\tif meta.length\n\t\tthrow new Error(`@thenable {target.constructor..name}.{key} not allowed - @thenable {meta[0][0]} already defined`)\n\n\tconst warn = do\n\t\tconsole.trace `@thenable {target.constructor..name}.{key} takes long - make sure method does not return self`\n\n\tmeta.push([key,sym])\n\n\tif val isa Function\n\t\tif val.length > 0\n\t\t\tthrow new Error(`@thenable methods cannot be called with arguments ({key})`)\n\n\t\tObject.defineProperty(target,'then',{\n\t\t\tenumerable: no,\n\t\t\tvalue: do(ok,err)\n\t\t\t\tlet that = this\n\n\t\t\t\tlet promise = this[thensym] ||= new Promise do(resolve,reject)\n\t\t\t\t\t# TODO should only happen in debug\n\t\t\t\t\tlet timeout = setTimeout(warn,2s)\n\n\t\t\t\t\tlet err = do(error)\n\t\t\t\t\t\tclearTimeout(timeout)\n\t\t\t\t\t\treject(error)\n\n\t\t\t\t\tthat[key]().then(&,err) do\n\t\t\t\t\t\tclearTimeout(timeout)\n\t\t\t\t\t\tObject.defineProperty(that,'then',{value: null, writable: yes, configurable: yes})\n\t\t\t\t\t\tresolve(that)\n\t\t\t\t\n\t\t\t\tpromise.then(ok,err)\n\t\t})\n\n\t\tdesc.value = do(value)\n\t\t\tthis[sym] ||= Promise.resolve(val.call(this))\n\n\t\t# You can reset thenables explicitly by calling instance.myfunction.reset(instance)\n\t\tdesc.value.reset = do(target)\n\t\t\tif target and target[sym]\n\t\t\t\ttarget[sym] = null\n\t\t\t\ttarget[thensym] = null\n\t\t\t\tdelete target.then\n\n\treturn desc"],"names":[],"mappings":";;;AACA,MAAM,CAAC,QAAG,CAAC,KAAK,CAAC,MAAM,CAAE,GAAG,CAA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