-
Notifications
You must be signed in to change notification settings - Fork 0
createMethodDecorator
awekrx edited this page May 29, 2026
·
1 revision
import { createMethodDecorator } from '@dev-suite/decorators/createMethodDecorator'
core
Build reusable method decorators with shared before/after/wrap hooks.
- Descriptor wrapping boilerplate in each decorator
- Different hook semantics across decorators
function timed() {
return function (_target: object, key: string | symbol, descriptor: PropertyDescriptor) {
const original = descriptor.value;
descriptor.value = async function (...args: unknown[]) {
const start = Date.now();
try {
return await original.apply(this, args);
} finally {
console.log(String(key), Date.now() - start);
}
};
};
}import { createMethodDecorator } from '@dev-suite/decorators/createMethodDecorator';
const timed = () =>
createMethodDecorator({
wrap: ({ invoke, args, propertyKey }) => {
const start = Date.now();
try {
return invoke(args);
} finally {
console.log(String(propertyKey), Date.now() - start);
}
},
});- Centralizes cross-cutting behavior.
- Method/class/property code stays focused on domain logic.
function callCounter() {
let count = 0;
return function (_target: object, key: string | symbol, descriptor: PropertyDescriptor) {
const original = descriptor.value;
descriptor.value = function (...args: unknown[]) {
count += 1;
console.log(String(key), count);
return original.apply(this, args);
};
};
}import { createMethodDecorator } from '@dev-suite/decorators/createMethodDecorator';
const callCounter = () => {
let count = 0;
return createMethodDecorator({
before: ({ propertyKey }) => {
count += 1;
console.log(String(propertyKey), count);
},
});
};- Second scenario reuses same policy without duplication.
- Behavior is more consistent and easier to audit.