using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MW.WorkFlow
{
    public class ActivityControl
    {
        public CancellationToken CancellationToken { get; }
        private readonly Func<Task> _pauseCheck;

        public ActivityControl(CancellationToken cancellationToken, Func<Task> pauseCheck)
        {
            CancellationToken = cancellationToken;
            _pauseCheck = pauseCheck ?? throw new ArgumentNullException(nameof(pauseCheck));
        }

        /// <summary>
        /// 检查是否有取消请求，如果有则抛出 OperationCanceledException。
        /// </summary>
        public void ThrowIfCancellationRequested()
        {
            CancellationToken.ThrowIfCancellationRequested();
        }

        /// <summary>
        /// 检查流程是否处于暂停状态，如果是则等待直到恢复。
        /// 同时响应取消请求。
        /// </summary>
        public Task CheckPauseAsync() => _pauseCheck();


    }
}
