public static HandlerFilter Aspect( this IHandler handler, BeforeCallback before, AfterCallback after, bool reentrant) { return(handler?.Filter((callback, composer, proceed) => { object state = null; var cb = callback as ICallback; if (before != null) { state = before(callback, composer); var promise = state as Promise; if (promise != null) { // TODO: Use Promise.End if cb.ResultType is not a Promise // TODO: or you will get an InvalidCastException var accept = promise.Then((accepted, s) => { if (!Equals(accepted, false)) { AspectProceed(callback, composer, proceed, after, state); return Promise.Resolved(cb?.Result); } return Promise.Rejected(new RejectedException(callback), s); }); if (cb != null) { cb.Result = accept; } return true; } if (Equals(state, false)) { var resultType = cb?.ResultType; if (resultType != null && typeof(Promise).IsAssignableFrom(resultType)) { cb.Result = Promise.Rejected(new RejectedException(callback)) .Coerce(resultType); return true; } throw new RejectedException(callback); } } return AspectProceed(callback, composer, proceed, after, state); }, reentrant)); }
public void BeforeInvoke(BeforeInvokeArgs args) { BeforeCallback?.Invoke(args); BeforeInvokeArgs = args; }
public static HandlerFilter Aspect( this IHandler handler, BeforeCallback before, AfterCallback after) { return(Aspect(handler, before, after, false)); }
public static HandlerFilter Aspect( this IHandler handler, BeforeCallback before, bool reentrant) { return(Aspect(handler, before, null, reentrant)); }