surrogate
Version:
Object method hooks made easy
138 lines • 4.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SurrogateAsyncPre = exports.SurrogateAsyncPost = exports.SurrogateAsyncPreAndPost = exports.SurrogatePost = exports.SurrogatePre = exports.SurrogatePreAndPost = exports.SurrogateFor = exports.SurrogateDelegate = void 0;
const manageDecorator_1 = require("./manageDecorator");
const surrogateClassWrapper_1 = require("./surrogateClassWrapper");
const which_1 = require("../which");
const asarray_1 = require("@jfrazx/asarray");
/**
* @description Register class for automatic surrogate wrapping
*
* @export
* @decorator
* @template T
* @param {SurrogateDecorateOptions<T>} [delegateOptions={}]
*/
const SurrogateDelegate = (delegateOptions = {}) => (klass) => surrogateClassWrapper_1.SurrogateClassWrapper.wrap(klass, delegateOptions);
exports.SurrogateDelegate = SurrogateDelegate;
/**
* @description Registers hooks for decorated methods. Handler type must be assigned.
*
* @export
* @decorator
* @template T
* @param {(SurrogateForOptions<T> | SurrogateForOptions<T>[])} forOptions
* @returns {PropertyDecorator<T>}
*/
const SurrogateFor = (forOptions) => {
return (target, event) => {
(0, asarray_1.asArray)(forOptions).forEach((forOption) => {
const { type, options } = forOption;
const which = (0, manageDecorator_1.determineWhich)(type);
which.forEach((type) => (0, manageDecorator_1.manageDecorator)(type, options)(target, event));
});
};
};
exports.SurrogateFor = SurrogateFor;
/**
* @description Registers pre and post hooks for decorated methods
*
* @export
* @decorator
* @template T
* @param {(SurrogateDelegateOptions<T> | SurrogateDelegateOptions<T>[])} decoratorOptions
* @returns {PropertyDecorator<T>}
*/
const SurrogatePreAndPost = (decoratorOptions) => {
return (target, event) => {
(0, asarray_1.asArray)(decoratorOptions).forEach((options) => (0, exports.SurrogateFor)({
options,
type: which_1.HookType.BOTH,
})(target, event));
};
};
exports.SurrogatePreAndPost = SurrogatePreAndPost;
/**
* @description Registers pre hooks for decorated methods
*
* @export
* @decorator
* @template T
* @param {(SurrogateDelegateOptions<T> | SurrogateDelegateOptions<T>[])} decoratorOptions
* @returns {PropertyDecorator<T>}
*/
const SurrogatePre = (decoratorOptions) => {
const forOptions = (0, asarray_1.asArray)(decoratorOptions).map((options) => ({
options,
type: which_1.HookType.PRE,
}));
return (0, exports.SurrogateFor)(forOptions);
};
exports.SurrogatePre = SurrogatePre;
/**
* @description Registers post hooks for decorated methods
*
* @export
* @decorator
* @template T
* @param {(SurrogateDelegateOptions<T> | SurrogateDelegateOptions<T>[])} decoratorOptions
* @returns {PropertyDecorator<T>}
*/
const SurrogatePost = (decoratorOptions) => {
const forOptions = (0, asarray_1.asArray)(decoratorOptions).map((options) => ({
options,
type: which_1.HookType.POST,
}));
return (0, exports.SurrogateFor)(forOptions);
};
exports.SurrogatePost = SurrogatePost;
/**
* @description Registers async pre and post hooks for decorated methods
*
* @export
* @decorator
* @template T
* @param {(SurrogateDelegateOptions<T> | SurrogateDelegateOptions<T>[])} decoratorOptions
* @returns {PropertyDecorator<T>}
*/
const SurrogateAsyncPreAndPost = (decoratorOptions) => {
const which = [which_1.HookType.PRE, which_1.HookType.POST];
return (target, event) => {
which.forEach((type) => surrogateAsyncHelper(type, decoratorOptions)(target, event));
};
};
exports.SurrogateAsyncPreAndPost = SurrogateAsyncPreAndPost;
/**
* @description Registers async post hooks for decorated methods
*
* @export
* @decorator
* @template T
* @param {(SurrogateDelegateOptions<T> | SurrogateDelegateOptions<T>[])} decoratorOptions
* @returns {PropertyDecorator<T>}
*/
const SurrogateAsyncPost = (decoratorOptions) => {
return surrogateAsyncHelper(which_1.HookType.POST, decoratorOptions);
};
exports.SurrogateAsyncPost = SurrogateAsyncPost;
/**
* @description Registers async pre hooks for decorated methods
*
* @export
* @decorator
* @template T
* @param {(SurrogateDelegateOptions<T> | SurrogateDelegateOptions<T>[])} decoratorOptions
* @returns {PropertyDecorator<T>}
*/
const SurrogateAsyncPre = (decoratorOptions) => {
return surrogateAsyncHelper(which_1.HookType.PRE, decoratorOptions);
};
exports.SurrogateAsyncPre = SurrogateAsyncPre;
const surrogateAsyncHelper = (type, asyncOptions) => {
return (target, event) => {
(0, asarray_1.asArray)(asyncOptions).forEach((options) => {
(0, manageDecorator_1.manageAsyncDecorator)(type, options)(target, event);
});
};
};
//# sourceMappingURL=surrogateDecorators.js.map