XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEEDToolBox

【WinUI】XFE精英危险工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using XFEEDToolBox.Core.Execution;

// A real runner may remain Running while its first operation is held here.
// None of the four mutation paths is ever forwarded to the production driver.
internal sealed class TradePauseMutationBarrier(ITradeCommandDriver production,ITradeRunMenuRecovery menu):ITradeCommandDriver,ITradeRunMenuRecovery
{
    private readonly TaskCompletionSource reached=new(TaskCreationOptions.RunContinuationsAsynchronously);
    private int buys,sells,travels,services;
    internal Task FirstMutationHeld=>reached.Task;
    internal int BlockedBuys=>Volatile.Read(ref buys);
    internal int BlockedSells=>Volatile.Read(ref sells);
    internal int BlockedTravels=>Volatile.Read(ref travels);
    internal int BlockedServices=>Volatile.Read(ref services);
    internal const int ForwardedMutations=0;
    public Task PreflightAsync(CancellationToken token)=>production.PreflightAsync(token);
    public IDisposable EnterRunningMenuRecovery(Func<bool> isRunning,CancellationToken token)=>menu.EnterRunningMenuRecovery(isRunning,token);
    public async Task ServiceDockedAsync(DockedServiceRequest request,Func<CancellationToken,Task> beforeInput,CancellationToken token)
    {await beforeInput(token);Interlocked.Increment(ref services);reached.TrySetResult();await Task.Delay(Timeout.Infinite,token);}
    public async Task ExecuteAsync(TradeAction action,Func<CancellationToken,Task> beforeInput,CancellationToken token)
    {
        await beforeInput(token);
        switch(action.Kind)
        {
            case TradeActionKind.Buy:Interlocked.Increment(ref buys);break;
            case TradeActionKind.Sell:Interlocked.Increment(ref sells);break;
            case TradeActionKind.Travel:Interlocked.Increment(ref travels);break;
            default:throw new InvalidOperationException("Unknown trade mutation was refused.");
        }
        reached.TrySetResult();await Task.Delay(Timeout.Infinite,token);
    }
    public Task ReleaseAllAsync()=>production.ReleaseAllAsync();
}