surrogate
Version:
Object method hooks made easy
142 lines (129 loc) • 5.39 kB
JavaScript
import {TimeTrackable as $2LSyv$TimeTrackable} from "./surrogate.0b67290d.js";
import {Next as $2LSyv$Next} from "./surrogate.cbe2cc46.js";
import {MethodWrapper as $66c0dea2f60cbf63$export$9255060c057a2446} from "./surrogate.a243fd60.js";
import {HookType as $96586206eab52612$export$b3a402ce9f993bc7} from "./surrogate.5d082a84.js";
import {isAsync as $8c13fb1fbdc14b86$export$aefee5ebe1dcfd9e, isFunction as $8c13fb1fbdc14b86$export$f6e2535fb5126e54} from "./surrogate.68fc8564.js";
import {containerSorter as $f864a6250a41563a$export$ab25e1288b803381} from "./surrogate.d0216b33.js";
import {Tail as $8c78a71587639d7a$export$cadaa6181bfd6ce1} from "./surrogate.b769cedc.js";
import {asArray as $2LSyv$asArray} from "@jfrazx/asarray";
import {v4 as $2LSyv$v4} from "uuid";
class $9121e432d9cc792d$export$f7c638676c8af91d {
constructor(context){
this.context = context;
this.utilizeLatest = false;
this.hasThrown = false;
this.timeTracker = (0, $2LSyv$TimeTrackable).fetch();
this.correlationId = (0, $2LSyv$v4)();
}
static for(context, typeContainers) {
const hasAsync = this.hasAsync(typeContainers);
const TargetContext = hasAsync || (0, $8c13fb1fbdc14b86$export$aefee5ebe1dcfd9e)(context.original) ? $9121e432d9cc792d$export$354c9ee217516037 : $9121e432d9cc792d$export$a9787f5a4efadfdd;
return new TargetContext(context);
}
setupPipeline(proxy, typeContainers) {
const which = [
(0, $96586206eab52612$export$b3a402ce9f993bc7).PRE,
(0, $96586206eab52612$export$b3a402ce9f993bc7).POST
];
which.forEach((type)=>{
const containers = [
...typeContainers[type]
];
containers.sort((0, $f864a6250a41563a$export$ab25e1288b803381)).forEach((container)=>new (0, $2LSyv$Next)(this, proxy, container, type));
(0, $8c78a71587639d7a$export$cadaa6181bfd6ce1).for(this, proxy, type);
});
return this;
}
static hasAsync(typeContainers) {
return Object.values(typeContainers).flat().some(({ handler: handler, options: options })=>(0, $8c13fb1fbdc14b86$export$aefee5ebe1dcfd9e)(handler) || options.wrapper === (0, $66c0dea2f60cbf63$export$9255060c057a2446).Async);
}
setReturnValue(value) {
this.returnValue ??= value;
}
setNext(next) {
this.nextNode = next;
}
addNext(next) {
this.nextNode ? this.nextNode.addNext(next) : this.setNext(next);
return this;
}
updateLatestArgs(updatedArgs) {
this.utilizeLatest = true;
this.latestArgs = (0, $2LSyv$asArray)(updatedArgs);
}
get currentArgs() {
return this.utilizeLatest ? this.latestArgs : this.context.originalArguments;
}
runNext(next) {
const node = next ?? this.nextNode;
this.setNext(node.nextNode);
return node.handleNext();
}
logError(node, error) {
const { silenceErrors: silenceErrors } = node.container.options;
const silence = (0, $8c13fb1fbdc14b86$export$f6e2535fb5126e54)(silenceErrors) ? silenceErrors(error) : silenceErrors;
if (!silence && !this.hasThrown) {
this.hasThrown = true;
console.error(`SurrogateError: ${error?.message ? error.message : JSON.stringify(error)}`);
}
}
}
class $9121e432d9cc792d$export$354c9ee217516037 extends $9121e432d9cc792d$export$f7c638676c8af91d {
async start() {
return new Promise((resolve, reject)=>{
this.resolver = resolve;
this.rejecter = reject;
try {
this.runNext(this.nextNode);
} catch (error) {
this.handleError(this.nextNode, error);
}
});
}
complete() {
this.resolver(this.returnValue);
}
async runOriginal(node) {
const { container: container, context: context } = node;
const handler = container.getHandler(context);
try {
const result = await handler.apply(context.receiver, this.currentArgs);
this.setReturnValue(result);
this.runNext(node.nextNode);
} catch (error) {
this.handleError(this.nextNode, error);
}
}
handleError(node, error) {
this.logError(node, error);
this.rejecter(error);
}
bail(bailWith) {
this.resolver(bailWith ?? this.returnValue);
}
}
class $9121e432d9cc792d$export$a9787f5a4efadfdd extends $9121e432d9cc792d$export$f7c638676c8af91d {
start() {
this.runNext();
return this.complete();
}
complete() {
return this.returnValue;
}
runOriginal(node) {
const { container: container, context: context } = node;
const handler = container.getHandler(context);
const result = handler.apply(context.receiver, this.currentArgs);
this.setReturnValue(result);
this.runNext(node.nextNode);
}
bail(bailWith) {
this.setReturnValue(bailWith);
}
handleError(node, error) {
this.logError(node, error);
throw error;
}
}
export {$9121e432d9cc792d$export$f7c638676c8af91d as ExecutionContext, $9121e432d9cc792d$export$354c9ee217516037 as NextAsyncContext, $9121e432d9cc792d$export$a9787f5a4efadfdd as NextContext};
//# sourceMappingURL=surrogate.757a0eed.js.map