ts-raii-scope
Version:
TypeScript RAII proof of concept
41 lines (40 loc) • 1.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const scopeStack_1 = require("./scopeStack");
// noinspection JSUnusedGlobalSymbols
function SyncRaiiMethodScope(target, propertyName, propertyDescriptor) {
const method = propertyDescriptor.value;
if (method) {
propertyDescriptor.value = function (...args) {
const raiiScope = scopeStack_1.scopeStack.enterScope(false);
try {
return method.call(target, ...args);
}
finally {
scopeStack_1.scopeStack.exitScope(raiiScope);
}
};
}
return propertyDescriptor;
}
exports.SyncRaiiMethodScope = SyncRaiiMethodScope;
// noinspection JSUnusedGlobalSymbols
function AsyncRaiiMethodScope(target, propertyName, propertyDescriptor) {
const method = propertyDescriptor.value;
if (method) {
propertyDescriptor.value = function (...args) {
const raiiScope = scopeStack_1.scopeStack.enterScope(true);
const methodPromise = method.call(target, ...args);
scopeStack_1.scopeStack.exitScope(raiiScope);
return methodPromise.then(async (result) => {
await scopeStack_1.scopeStack.asyncScopeDone(raiiScope);
return result;
}, async (reason) => {
await scopeStack_1.scopeStack.asyncScopeDone(raiiScope);
throw reason;
});
};
}
return propertyDescriptor;
}
exports.AsyncRaiiMethodScope = AsyncRaiiMethodScope;