TimeConstraint.cs 719 B

123456789101112131415161718192021222324252627282930
  1. using Jint.Runtime;
  2. namespace Jint.Constraints
  3. {
  4. internal sealed class TimeConstraint : IConstraint
  5. {
  6. private readonly long _maxTicks;
  7. private long _timeoutTicks;
  8. public TimeConstraint(TimeSpan timeout)
  9. {
  10. _maxTicks = timeout.Ticks;
  11. }
  12. public void Check()
  13. {
  14. if (_timeoutTicks > 0 && _timeoutTicks < DateTime.UtcNow.Ticks)
  15. {
  16. ExceptionHelper.ThrowTimeoutException();
  17. }
  18. }
  19. public void Reset()
  20. {
  21. var timeoutIntervalTicks = _maxTicks;
  22. _timeoutTicks = timeoutIntervalTicks > 0 ? DateTime.UtcNow.Ticks + timeoutIntervalTicks : 0;
  23. }
  24. }
  25. }