-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5321999
commit 56e8fc6
Showing
5 changed files
with
278 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
"use strict"; | ||
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) { | ||
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; } | ||
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value"; | ||
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null; | ||
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {}); | ||
var _, done = false; | ||
for (var i = decorators.length - 1; i >= 0; i--) { | ||
var context = {}; | ||
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p]; | ||
for (var p in contextIn.access) context.access[p] = contextIn.access[p]; | ||
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); }; | ||
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context); | ||
if (kind === "accessor") { | ||
if (result === void 0) continue; | ||
if (result === null || typeof result !== "object") throw new TypeError("Object expected"); | ||
if (_ = accept(result.get)) descriptor.get = _; | ||
if (_ = accept(result.set)) descriptor.set = _; | ||
if (_ = accept(result.init)) initializers.push(_); | ||
} | ||
else if (_ = accept(result)) { | ||
if (kind === "field") initializers.push(_); | ||
else descriptor[key] = _; | ||
} | ||
} | ||
if (target) Object.defineProperty(target, contextIn.name, descriptor); | ||
done = true; | ||
}; | ||
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) { | ||
var useValue = arguments.length > 2; | ||
for (var i = 0; i < initializers.length; i++) { | ||
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg); | ||
} | ||
return useValue ? value : void 0; | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (g && (g = 0, op[0] && (_ = 0)), _) try { | ||
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; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
var _this = this; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var promises_1 = require("timers/promises"); | ||
function loggedMethod(originalMethod, _context) { | ||
console.error(_context); | ||
console.error(String(_context.name)); | ||
function replacementMethod() { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
console.log("LOG: Entering method."); | ||
var result = originalMethod.call.apply(originalMethod, __spreadArray([this], args, false)); | ||
console.log("LOG: Exiting method."); | ||
return result; | ||
} | ||
return replacementMethod; | ||
} | ||
function lock(originalMethod, _context) { | ||
var cachedPromise = null; | ||
function replacementMethod() { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
return __awaiter(this, void 0, void 0, function () { | ||
var returnValue; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
console.error({ cachedPromise: cachedPromise }); | ||
cachedPromise !== null && cachedPromise !== void 0 ? cachedPromise : (cachedPromise = originalMethod.call.apply(originalMethod, __spreadArray([this], args, false))); | ||
return [4 /*yield*/, cachedPromise]; | ||
case 1: | ||
returnValue = _a.sent(); | ||
cachedPromise = null; | ||
return [2 /*return*/, returnValue]; | ||
} | ||
}); | ||
}); | ||
} | ||
return replacementMethod; | ||
} | ||
function lazyInit(_a, _b) { | ||
var get = _a.get, set = _a.set; | ||
var name = _b.name, kind = _b.kind; | ||
if (kind === 'accessor') { | ||
var initializedValue_1 = null; | ||
return { | ||
get: function () { | ||
!initializedValue_1 && (initializedValue_1 = get.call(this)); | ||
return initializedValue_1; | ||
} | ||
}; | ||
} | ||
} | ||
function number(value, _context) { | ||
return function replacement() { | ||
var returnValue = value.call(this); | ||
return Number(returnValue); | ||
}; | ||
} | ||
var ExampleClass = function () { | ||
var _a; | ||
var _instanceExtraInitializers = []; | ||
var _fooBar_decorators; | ||
var _promiseReturns_decorators; | ||
var _get_myField_decorators; | ||
return _a = /** @class */ (function () { | ||
function ExampleClass() { | ||
__runInitializers(this, _instanceExtraInitializers); | ||
} | ||
ExampleClass.prototype.fooBar = function () { | ||
console.log('evaluating method'); | ||
}; | ||
ExampleClass.prototype.promiseReturns = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
console.log('starting execution'); | ||
return [4 /*yield*/, (0, promises_1.setTimeout)(1000)]; | ||
case 1: | ||
_a.sent(); | ||
console.log('finishing execution'); | ||
return [2 /*return*/, 3]; | ||
} | ||
}); | ||
}); | ||
}; | ||
Object.defineProperty(ExampleClass.prototype, "myField", { | ||
get: function () { | ||
// @ts-expect-error expected error | ||
return '2'; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
return ExampleClass; | ||
}()), | ||
(function () { | ||
_fooBar_decorators = [loggedMethod]; | ||
_promiseReturns_decorators = [lock]; | ||
_get_myField_decorators = [number]; | ||
__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); | ||
__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); | ||
__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); | ||
})(), | ||
_a; | ||
}(); | ||
var x = new ExampleClass(); | ||
// x.promiseReturns(); | ||
console.log('instantiated class', typeof x.myField); | ||
x.myField; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { setTimeout } from "timers/promises"; | ||
|
||
function loggedMethod(originalMethod: any, _context: ClassMethodDecoratorContext) { | ||
console.error(_context); | ||
console.error(String(_context.name)); | ||
function replacementMethod(this: any, ...args: any[]) { | ||
console.log("LOG: Entering method.") | ||
const result = originalMethod.call(this, ...args); | ||
console.log("LOG: Exiting method.") | ||
return result; | ||
} | ||
|
||
return replacementMethod; | ||
} | ||
|
||
function lock<This, Args extends any[], Return>( | ||
originalMethod: (this: This, args: Args) => Promise<Return>, | ||
_context: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Promise<Return>>) { | ||
let cachedPromise: Promise<Return> | null = null; | ||
async function replacementMethod(this: This, ...args: Args): Promise<Return> { | ||
console.error({ cachedPromise }) | ||
cachedPromise ??= originalMethod.call(this, ...args); | ||
const returnValue = await cachedPromise; | ||
cachedPromise = null; | ||
return returnValue; | ||
} | ||
|
||
return replacementMethod; | ||
} | ||
|
||
function lazyInit({ get, set }, { name, kind }) { | ||
if (kind === 'accessor') { | ||
let initializedValue = null; | ||
return { | ||
get() { | ||
!initializedValue && (initializedValue = get.call(this)); | ||
return initializedValue; | ||
} | ||
}; | ||
} | ||
} | ||
|
||
type ValidTypes = 'int' | 'long' | 'string'; | ||
|
||
function number(value, _context: ClassGetterDecoratorContext) { | ||
return function replacement(this) { | ||
const returnValue = value.call(this); | ||
|
||
return Number(returnValue); | ||
} | ||
} | ||
|
||
class ExampleClass { | ||
@loggedMethod | ||
fooBar() { | ||
console.log('evaluating method'); | ||
} | ||
|
||
@lock | ||
async promiseReturns(): Promise<number> { | ||
console.log('starting execution') | ||
await setTimeout(1000); | ||
console.log('finishing execution') | ||
return 3; | ||
} | ||
|
||
@number | ||
get myField(): number { | ||
// @ts-expect-error expected error | ||
return '2'; | ||
} | ||
} | ||
|
||
const x = new ExampleClass(); | ||
// x.promiseReturns(); | ||
|
||
console.log('instantiated class', typeof x.myField); | ||
x.myField; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters