mirror of
https://github.com/Xevion/paperless-mobile.git
synced 2025-12-15 16:12:20 -06:00
feat: Add response delay generator to mock_server
This commit is contained in:
46
packages/mock_server/lib/response_delay_generator.dart
Normal file
46
packages/mock_server/lib/response_delay_generator.dart
Normal file
@@ -0,0 +1,46 @@
|
||||
import 'dart:math';
|
||||
|
||||
abstract interface class DelayGenerator {
|
||||
Duration nextDelay();
|
||||
}
|
||||
|
||||
class RandomDelayGenerator implements DelayGenerator {
|
||||
/// Minimum allowed response delay
|
||||
final Duration minDelay;
|
||||
|
||||
/// Maximum allowed response delay
|
||||
final Duration maxDelay;
|
||||
|
||||
final Random _random = Random();
|
||||
RandomDelayGenerator(this.minDelay, this.maxDelay);
|
||||
|
||||
@override
|
||||
Duration nextDelay() {
|
||||
return Duration(
|
||||
milliseconds: minDelay.inMilliseconds +
|
||||
_random.nextInt(
|
||||
maxDelay.inMilliseconds - minDelay.inMilliseconds,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ConstantDelayGenerator implements DelayGenerator {
|
||||
final Duration delay;
|
||||
|
||||
const ConstantDelayGenerator(this.delay);
|
||||
|
||||
@override
|
||||
Duration nextDelay() {
|
||||
return delay;
|
||||
}
|
||||
}
|
||||
|
||||
class ZeroDelayGenerator implements DelayGenerator {
|
||||
const ZeroDelayGenerator();
|
||||
|
||||
@override
|
||||
Duration nextDelay() {
|
||||
return Duration.zero;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user