Skip to content

Commit

Permalink
Merge v1.7.4
Browse files Browse the repository at this point in the history
 - `TimedMap`:
   - Added keyTimeoutChecker
  • Loading branch information
gmpassos authored May 21, 2024
2 parents b6b5822 + 8383d73 commit d11eec1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.7.4

- `TimedMap`:
- Added keyTimeoutChecker

## 1.7.3

- `MapAsCacheExtension`:
Expand Down
2 changes: 1 addition & 1 deletion lib/src/bones_api_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ typedef APILogger = void Function(APIRoot apiRoot, String type, String? message,
/// Bones API Library class.
class BonesAPI {
// ignore: constant_identifier_names
static const String VERSION = '1.7.3';
static const String VERSION = '1.7.4';

static bool _boot = false;

Expand Down
15 changes: 14 additions & 1 deletion lib/src/bones_api_utils_timedmap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ class TimedMap<K, V> implements Map<K, V> {
/// The key timeout. When a key is put, it expires after the timeout [Duration].
final Duration keyTimeout;

TimedMap(this.keyTimeout, [Map<K, V>? map]) {
final bool? Function(TimedMap timedMap, Object? key, Duration elapsedTime,
Duration keyTimeout)? keyTimeoutChecker;

TimedMap(this.keyTimeout, [Map<K, V>? map, this.keyTimeoutChecker]) {
if (map != null) {
addAll(map);
}
Expand Down Expand Up @@ -132,7 +135,17 @@ class TimedMap<K, V> implements Map<K, V> {
var elapsedTime = getElapsedTime(key, now: now);
if (elapsedTime == null) return false;

final keyTimeoutChecker = this.keyTimeoutChecker;

keyTimeout ??= this.keyTimeout;

if (keyTimeoutChecker != null) {
var expired = keyTimeoutChecker(this, key, elapsedTime, keyTimeout);
if (expired != null) {
return expired;
}
}

return elapsedTime.inMilliseconds > keyTimeout.inMilliseconds;
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: bones_api
description: Bones_API - A powerful API backend framework for Dart. It comes with a built-in HTTP Server, route handler, entity handler, SQL translator, and DB adapters.
version: 1.7.3
version: 1.7.4
homepage: https://github.com/Colossus-Services/bones_api

environment:
Expand Down
28 changes: 28 additions & 0 deletions test/bones_api_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,34 @@ void main() {

expect(m.length, equals(0));
});

test('keyTimeoutChecker', () async {
final checks = <String>[];

var m = TimedMap<String, int>(Duration(seconds: 1), null, (tm, k, t, to) {
checks.add('$k,${to.inMilliseconds}');
if (k == 'b') return false;
return null;
});

m.put('a', 1);
m.put('b', 2);

expect(m.length, equals(2));
expect(checks, isEmpty);

m.checkAllEntries();

expect(m.length, equals(2));
expect(checks, equals(['a,1000', 'b,1000']));

await Future.delayed(Duration(milliseconds: 1100));

m.checkAllEntries();

expect(m, equals({'b': 2}));
expect(checks, equals(['a,1000', 'b,1000', 'a,1000', 'b,1000']));
});
});

group('PositionalFields', () {
Expand Down

0 comments on commit d11eec1

Please sign in to comment.