// // IContract.cs // // Copyright (c) František Boháček. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; using System.Threading; using System.Threading.Tasks; using NosSmooth.Core.Contracts.Responders; using Remora.Results; namespace NosSmooth.Core.Contracts; /// /// A contract, used for executing an operation with feedback. /// /// /// Do not use this type directly, use the generic one instead. /// public interface IContract { /// /// Gets whether this contract /// is registered to the contractor. /// public bool IsRegistered { get; } /// /// Register this contract into contractor. /// /// /// The contract will receive data from the contractor, /// using CheckDataAsync method. This way there may be a /// feedback coming back to the contract. /// public void Register(); /// /// Unregister this contract from contractor. /// public void Unregister(); /// /// Update the contract with the given received data. /// /// /// Called from /// or similar. Used for updating the state. /// The contract looks for actions that trigger updates /// and in case it matches the , /// the state is switched. /// /// The data that were received. /// The type of the data. /// The cancellation token used for cancelling the operation. /// The result that may or may not have succeeded. public Task> Update(TAny data, CancellationToken ct = default) where TAny : notnull; /// /// Executes the contract without registering it, /// running only the initial operation. /// /// /// For example, to use skill, create a contract for /// using a skill and call this method. /// If you want to wait for response from the server, /// use instead. /// That will register the contract and wait for response. /// /// The cancellation token used for cancelling the operation. /// The result that may or may not have succeeded. public Task OnlyExecuteAsync(CancellationToken ct = default); } /// /// A contract, used for executing an operation with feedback. /// /// /// Could be used for operations that may end successfully or fail /// after some time, with response from the server. /// /// Look at for example usage. /// /// The data returned by the contract in case of success. /// Type containing the states of the contract. public interface IContract : IContract where TData : notnull where TState : IComparable { /// /// Gets the current state of the contract. /// /// /// To wait for any state, see . /// public TState CurrentState { get; } /// /// Gets the data of the contract obtained from packets/. /// /// /// This won't be filled in case the contract /// is not registered. /// public TData? Data { get; } /// /// Register to contractor and wait for the given state. /// Execute the initial action. /// /// The state to wait for. /// Whether to unregister the contract from the contractor after the state is reached. The contract won't be updated anymore. /// The cancellation token used for cancelling the operation. /// The data of the contract or an error. /// Thrown in case the given state cannot fill the data. public Task> WaitForAsync(TState state, bool unregisterAfter = true, CancellationToken ct = default); /// /// Gets whether the given state has been reached already. /// /// The state to check has been reached. /// True in case the given state has been reached or there was an error. public bool HasReachedState(TState state); }