|
| 1 | +"use strict"; |
| 2 | +var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) { |
| 3 | + function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; } |
| 4 | + var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value"; |
| 5 | + var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null; |
| 6 | + var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {}); |
| 7 | + var _, done = false; |
| 8 | + for (var i = decorators.length - 1; i >= 0; i--) { |
| 9 | + var context = {}; |
| 10 | + for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p]; |
| 11 | + for (var p in contextIn.access) context.access[p] = contextIn.access[p]; |
| 12 | + context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); }; |
| 13 | + var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context); |
| 14 | + if (kind === "accessor") { |
| 15 | + if (result === void 0) continue; |
| 16 | + if (result === null || typeof result !== "object") throw new TypeError("Object expected"); |
| 17 | + if (_ = accept(result.get)) descriptor.get = _; |
| 18 | + if (_ = accept(result.set)) descriptor.set = _; |
| 19 | + if (_ = accept(result.init)) initializers.push(_); |
| 20 | + } |
| 21 | + else if (_ = accept(result)) { |
| 22 | + if (kind === "field") initializers.push(_); |
| 23 | + else descriptor[key] = _; |
| 24 | + } |
| 25 | + } |
| 26 | + if (target) Object.defineProperty(target, contextIn.name, descriptor); |
| 27 | + done = true; |
| 28 | +}; |
| 29 | +var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) { |
| 30 | + var useValue = arguments.length > 2; |
| 31 | + for (var i = 0; i < initializers.length; i++) { |
| 32 | + value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg); |
| 33 | + } |
| 34 | + return useValue ? value : void 0; |
| 35 | +}; |
| 36 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 37 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 38 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 39 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 40 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 41 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 42 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 43 | + }); |
| 44 | +}; |
| 45 | +var __generator = (this && this.__generator) || function (thisArg, body) { |
| 46 | + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; |
| 47 | + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; |
| 48 | + function verb(n) { return function (v) { return step([n, v]); }; } |
| 49 | + function step(op) { |
| 50 | + if (f) throw new TypeError("Generator is already executing."); |
| 51 | + while (g && (g = 0, op[0] && (_ = 0)), _) try { |
| 52 | + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; |
| 53 | + if (y = 0, t) op = [op[0] & 2, t.value]; |
| 54 | + switch (op[0]) { |
| 55 | + case 0: case 1: t = op; break; |
| 56 | + case 4: _.label++; return { value: op[1], done: false }; |
| 57 | + case 5: _.label++; y = op[1]; op = [0]; continue; |
| 58 | + case 7: op = _.ops.pop(); _.trys.pop(); continue; |
| 59 | + default: |
| 60 | + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } |
| 61 | + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } |
| 62 | + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } |
| 63 | + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } |
| 64 | + if (t[2]) _.ops.pop(); |
| 65 | + _.trys.pop(); continue; |
| 66 | + } |
| 67 | + op = body.call(thisArg, _); |
| 68 | + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } |
| 69 | + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; |
| 70 | + } |
| 71 | +}; |
| 72 | +var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { |
| 73 | + if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { |
| 74 | + if (ar || !(i in from)) { |
| 75 | + if (!ar) ar = Array.prototype.slice.call(from, 0, i); |
| 76 | + ar[i] = from[i]; |
| 77 | + } |
| 78 | + } |
| 79 | + return to.concat(ar || Array.prototype.slice.call(from)); |
| 80 | +}; |
| 81 | +var _this = this; |
| 82 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 83 | +var promises_1 = require("timers/promises"); |
| 84 | +function loggedMethod(originalMethod, _context) { |
| 85 | + console.error(_context); |
| 86 | + console.error(String(_context.name)); |
| 87 | + function replacementMethod() { |
| 88 | + var args = []; |
| 89 | + for (var _i = 0; _i < arguments.length; _i++) { |
| 90 | + args[_i] = arguments[_i]; |
| 91 | + } |
| 92 | + console.log("LOG: Entering method."); |
| 93 | + var result = originalMethod.call.apply(originalMethod, __spreadArray([this], args, false)); |
| 94 | + console.log("LOG: Exiting method."); |
| 95 | + return result; |
| 96 | + } |
| 97 | + return replacementMethod; |
| 98 | +} |
| 99 | +function lock(originalMethod, _context) { |
| 100 | + var cachedPromise = null; |
| 101 | + function replacementMethod() { |
| 102 | + var args = []; |
| 103 | + for (var _i = 0; _i < arguments.length; _i++) { |
| 104 | + args[_i] = arguments[_i]; |
| 105 | + } |
| 106 | + return __awaiter(this, void 0, void 0, function () { |
| 107 | + var returnValue; |
| 108 | + return __generator(this, function (_a) { |
| 109 | + switch (_a.label) { |
| 110 | + case 0: |
| 111 | + console.error({ cachedPromise: cachedPromise }); |
| 112 | + cachedPromise !== null && cachedPromise !== void 0 ? cachedPromise : (cachedPromise = originalMethod.call.apply(originalMethod, __spreadArray([this], args, false))); |
| 113 | + return [4 /*yield*/, cachedPromise]; |
| 114 | + case 1: |
| 115 | + returnValue = _a.sent(); |
| 116 | + cachedPromise = null; |
| 117 | + return [2 /*return*/, returnValue]; |
| 118 | + } |
| 119 | + }); |
| 120 | + }); |
| 121 | + } |
| 122 | + return replacementMethod; |
| 123 | +} |
| 124 | +function lazyInit(_a, _b) { |
| 125 | + var get = _a.get, set = _a.set; |
| 126 | + var name = _b.name, kind = _b.kind; |
| 127 | + if (kind === 'accessor') { |
| 128 | + var initializedValue_1 = null; |
| 129 | + return { |
| 130 | + get: function () { |
| 131 | + !initializedValue_1 && (initializedValue_1 = get.call(this)); |
| 132 | + return initializedValue_1; |
| 133 | + } |
| 134 | + }; |
| 135 | + } |
| 136 | +} |
| 137 | +function number(value, _context) { |
| 138 | + return function replacement() { |
| 139 | + var returnValue = value.call(this); |
| 140 | + return Number(returnValue); |
| 141 | + }; |
| 142 | +} |
| 143 | +var ExampleClass = function () { |
| 144 | + var _a; |
| 145 | + var _instanceExtraInitializers = []; |
| 146 | + var _fooBar_decorators; |
| 147 | + var _promiseReturns_decorators; |
| 148 | + var _get_myField_decorators; |
| 149 | + return _a = /** @class */ (function () { |
| 150 | + function ExampleClass() { |
| 151 | + __runInitializers(this, _instanceExtraInitializers); |
| 152 | + } |
| 153 | + ExampleClass.prototype.fooBar = function () { |
| 154 | + console.log('evaluating method'); |
| 155 | + }; |
| 156 | + ExampleClass.prototype.promiseReturns = function () { |
| 157 | + return __awaiter(this, void 0, void 0, function () { |
| 158 | + return __generator(this, function (_a) { |
| 159 | + switch (_a.label) { |
| 160 | + case 0: |
| 161 | + console.log('starting execution'); |
| 162 | + return [4 /*yield*/, (0, promises_1.setTimeout)(1000)]; |
| 163 | + case 1: |
| 164 | + _a.sent(); |
| 165 | + console.log('finishing execution'); |
| 166 | + return [2 /*return*/, 3]; |
| 167 | + } |
| 168 | + }); |
| 169 | + }); |
| 170 | + }; |
| 171 | + Object.defineProperty(ExampleClass.prototype, "myField", { |
| 172 | + get: function () { |
| 173 | + // @ts-expect-error expected error |
| 174 | + return '2'; |
| 175 | + }, |
| 176 | + enumerable: false, |
| 177 | + configurable: true |
| 178 | + }); |
| 179 | + return ExampleClass; |
| 180 | + }()), |
| 181 | + (function () { |
| 182 | + _fooBar_decorators = [loggedMethod]; |
| 183 | + _promiseReturns_decorators = [lock]; |
| 184 | + _get_myField_decorators = [number]; |
| 185 | + __esDecorate(_a, null, _fooBar_decorators, { kind: "method", name: "fooBar", static: false, private: false, access: { has: function (obj) { return "fooBar" in obj; }, get: function (obj) { return obj.fooBar; } } }, null, _instanceExtraInitializers); |
| 186 | + __esDecorate(_a, null, _promiseReturns_decorators, { kind: "method", name: "promiseReturns", static: false, private: false, access: { has: function (obj) { return "promiseReturns" in obj; }, get: function (obj) { return obj.promiseReturns; } } }, null, _instanceExtraInitializers); |
| 187 | + __esDecorate(_a, null, _get_myField_decorators, { kind: "getter", name: "myField", static: false, private: false, access: { has: function (obj) { return "myField" in obj; }, get: function (obj) { return obj.myField; } } }, null, _instanceExtraInitializers); |
| 188 | + })(), |
| 189 | + _a; |
| 190 | +}(); |
| 191 | +var x = new ExampleClass(); |
| 192 | +// x.promiseReturns(); |
| 193 | +console.log('instantiated class', typeof x.myField); |
| 194 | +x.myField; |
0 commit comments