/// <summary> /// Create a new cancellation context and run the provided Aff in that context /// </summary> /// <param name="ma">Operation to run in the next context</param> /// <typeparam name="RT">Runtime environment</typeparam> /// <typeparam name="A">Bound value type</typeparam> /// <returns>An asynchronous effect that captures the operation running in context</returns> public static Eff <RT, A> localCancel <RT, A>(Eff <RT, A> ma) where RT : struct, HasCancel <RT> => EffMaybe <RT, A>(rt => { var rt1 = rt.LocalCancel; using (rt1.CancellationTokenSource) { return(ma.ReRun(rt1)); } });
/// <summary> /// Safely use a disposable resource /// </summary> /// <param name="Acq">Acquire resource</param> /// <param name="Use">Use resource</param> public static Eff <R> use <H, R>(Eff <H> Acq, Func <H, Eff <R> > Use) where H : IDisposable => EffMaybe(() => { var h = Acq.ReRun(); if (h.IsFail) { return(h.Cast <R>()); } try { return(Use(h.Value).Run()); } finally { h.Value?.Dispose(); } });
/// <summary> /// Safely use a disposable resource /// </summary> /// <param name="Acq">Acquire resource</param> /// <param name="Use">Use resource</param> public static Aff <R> use <H, R>(Eff <H> Acq, Func <H, Aff <R> > Use) where H : IDisposable => AffMaybe(async() => { var h = Acq.ReRun(); if (h.IsFail) { return(h.Cast <R>()); } try { return(await Use(h.Value).Run().ConfigureAwait(false)); } finally { h.Value?.Dispose(); } });
/// <summary> /// Safely use a disposable resource /// </summary> /// <param name="Acq">Acquire resource</param> /// <param name="Use">Use resource</param> public static Eff <RT, R> use <RT, H, R>(Eff <H> Acq, Func <H, Eff <RT, R> > Use) where RT : struct, HasCancel <RT> where H : IDisposable => EffMaybe <RT, R>(env => { var h = Acq.ReRun(); if (h.IsFail) { return(h.Cast <R>()); } try { return(Use(h.Value).Run(env)); } finally { h.Value?.Dispose(); } });
/// <summary> /// Safely use a disposable resource /// </summary> /// <param name="Acq">Acquire resource</param> /// <param name="Use">Use resource</param> public static Aff <RT, R> use <RT, H, R>(Eff <H> Acq, Func <H, Aff <RT, R> > Use) where RT : struct, HasCancel <RT> where H : IDisposable => AffMaybe <RT, R>(async env => { var h = Acq.ReRun(); if (h.IsFail) { return(h.Cast <R>()); } try { return(await Use(h.Value).Run(env).ConfigureAwait(false)); } finally { h.Value?.Dispose(); } });