1
0
mirror of synced 2024-11-15 06:43:49 +00:00
Tapeti/Tapeti.Flow/Default/FlowContext.cs

60 lines
1.8 KiB
C#
Raw Normal View History

using System;
using System.Diagnostics;
using System.Threading.Tasks;
namespace Tapeti.Flow.Default
{
internal class FlowContext : IDisposable
{
2019-08-15 10:04:03 +00:00
public IFlowHandlerContext HandlerContext { get; set; }
public IFlowStateLock FlowStateLock { get; set; }
public FlowState FlowState { get; set; }
public Guid ContinuationID { get; set; }
public ContinuationMetadata ContinuationMetadata { get; set; }
private int storeCalled;
private int deleteCalled;
public async Task Store(bool persistent)
{
storeCalled++;
2019-08-15 10:04:03 +00:00
if (HandlerContext == null) throw new ArgumentNullException(nameof(HandlerContext));
if (FlowState == null) throw new ArgumentNullException(nameof(FlowState));
if (FlowStateLock == null) throw new ArgumentNullException(nameof(FlowStateLock));
2019-08-15 10:04:03 +00:00
FlowState.Data = Newtonsoft.Json.JsonConvert.SerializeObject(HandlerContext.Controller);
await FlowStateLock.StoreFlowState(FlowState, persistent);
}
public async Task Delete()
{
deleteCalled++;
if (FlowStateLock != null)
await FlowStateLock.DeleteFlowState();
}
public bool IsStoredOrDeleted()
{
return storeCalled > 0 || deleteCalled > 0;
}
public void EnsureStoreOrDeleteIsCalled()
{
if (!IsStoredOrDeleted())
throw new InvalidProgramException("Neither Store nor Delete are called for the state of the current flow. FlowID = " + FlowStateLock?.FlowID);
Debug.Assert(storeCalled <= 1, "Store called more than once!");
Debug.Assert(deleteCalled <= 1, "Delete called more than once!");
}
public void Dispose()
{
FlowStateLock?.Dispose();
}
}
}