@deepkit/core
Version:
Deepkit core library
98 lines • 3.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.log = log;
exports.stack = stack;
exports.singleStack = singleStack;
const __ΩPropertyDescriptor = ['configurable', 'enumerable', 'value', 'writable', 'get', 'v', 'set', 'PropertyDescriptor', 'P)4!8)4"8"4#8)4$8P"1%8P"2&$1\'8Mw(y'];
const __ΩTypedPropertyDescriptor = ['T', 'enumerable', 'configurable', 'writable', 'value', '', 'get', 'set', 'TypedPropertyDescriptor', 'b!P)4"8)4#8)4$8e"!4%8Pe#!/&4\'8Pe#!2%$/&4(8Mw)y'];
function __assignType(fn, args) {
fn.__type = args;
return fn;
}
/*
* Deepkit Framework
* Copyright (C) 2021 Deepkit UG, Marc J. Schmidt
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the MIT License.
*
* You should have received a copy of the MIT License along with this program.
*/
const core_js_1 = require("./core.js");
const perf_js_1 = require("./perf.js");
/**
* Logs every call to this method on stdout.
*
* @group Decorators
*/
function log() {
return __assignType(function (target, propertyKey, descriptor) {
const orig = descriptor.value;
descriptor.value = __assignType(function (...args) {
const a = args.map(__assignType(v => typeof v, ['v', '', 'P"2!"/"'])).join(',');
console.info((0, core_js_1.getClassName)(target) + '::' + String(propertyKey) + '(' + a + ')');
return orig.apply(this, args);
}, ['args', '', 'P"@2!"/"']);
return descriptor;
}, ['target', 'propertyKey', () => __ΩPropertyDescriptor, 'descriptor', '', 'P%2!P&+J2"n#2$"/%']);
}
log.__type = ['log', 'P"/!'];
/**
* Makes sure that calls to this async method are stacked up and are called one after another and not parallel.
*
* @group Decorators
*/
function stack() {
return __assignType(function (target, propertyKey, descriptor) {
const orig = descriptor.value;
// console.log('sync patch', propertyKey, constructor.prototype[propertyKey]);
descriptor.value = __assignType(async function (...args) {
const name = '__c_' + propertyKey;
if (this[name] === undefined) {
this[name] = null;
(0, perf_js_1.toFastProperties)(this);
}
while (this[name]) {
await this[name];
}
this[name] = orig.apply(this, args);
try {
return await this[name];
}
finally {
this[name] = null;
}
}, ['args', '', 'P"@2!"/"']);
}, ['target', 'propertyKey', () => __ΩTypedPropertyDescriptor, 'args', '', 'descriptor', 'P%2!&2"P"@2$"`/%o#"2&"/%']);
}
stack.__type = ['stack', 'P"/!'];
/**
* Makes sure that this async method is only running once at a time. When this method is running and it is tried
* to call it another times, that call is "dropped" and it returns simply the result of the previous running call (waiting for it to complete first).
*
* @group Decorators
*/
function singleStack() {
return __assignType(function (target, propertyKey, descriptor) {
const orig = descriptor.value;
descriptor.value = __assignType(async function (...args) {
const name = '__sc_' + propertyKey;
if (this[name] === undefined) {
this[name] = null;
(0, perf_js_1.toFastProperties)(this);
}
if (this[name]) {
return await this[name];
}
this[name] = orig.apply(this, args);
try {
return await this[name];
}
finally {
this[name] = null;
}
}, ['args', '', 'P"@2!"/"']);
}, ['target', 'propertyKey', () => __ΩTypedPropertyDescriptor, 'args', '', 'descriptor', 'P%2!&2"P"@2$"`/%o#"2&"/%']);
}
singleStack.__type = ['singleStack', 'P"/!'];
//# sourceMappingURL=decorators.js.map