public static async Task <IComputed <T> > CaptureAsync <T>(Func <CancellationToken, Task <T> > producer, CancellationToken cancellationToken = default) { using var ccs = ComputeContext.New(CallOptions.Capture).Activate(); IComputed <T>?result; try { await producer.Invoke(cancellationToken).ConfigureAwait(false); } catch (OperationCanceledException) { throw; } catch (Exception) { result = ccs.Context.GetCapturedComputed <T>(); if (result?.Error != null) { return(result); } throw; } result = ccs.Context.GetCapturedComputed <T>(); if (result == null) { throw Errors.NoComputedCaptured(); } return(result); }
public static IComputed <T>?TryGetExisting <T>(Func <ValueTask <T> > producer) { using var ccs = ComputeContext.New(CallOptions.TryGetExisting | CallOptions.Capture).Activate(); var task = producer.Invoke(); task.AssertCompleted(); // The must be always synchronous in this case return(ccs.Context.GetCapturedComputed <T>()); }
public static void Invalidate <T>(Func <ValueTask <T> > producer) { using var ccs = ComputeContext.New(CallOptions.Invalidate).Activate(); var task = producer.Invoke(); // The flow is essentially synchronous in this case, so... task.AssertCompleted(); }
// TryGetExisting public static IComputed <T>?TryGetExisting <T>(Func <Task <T> > producer) { using var ccs = ComputeContext.New(CallOptions.TryGetExisting | CallOptions.Capture).Activate(); var task = producer.Invoke(); // The flow is essentially synchronous in this case, so... task.AssertCompleted(); return(ccs.Context.GetCapturedComputed <T>()); }
// Capture & invalidate public static async Task <IComputed <T>?> TryCaptureAsync <T>(Func <CancellationToken, Task <T> > producer, CancellationToken cancellationToken = default) { using var ccs = ComputeContext.New(CallOptions.Capture).Activate(); await producer.Invoke(cancellationToken).ConfigureAwait(false); var result = ccs.Context.GetCapturedComputed <T>(); return(result); }