diff --git a/lib/src/bones_api_utils_timedmap.dart b/lib/src/bones_api_utils_timedmap.dart index b4c87a0..cdae827 100644 --- a/lib/src/bones_api_utils_timedmap.dart +++ b/lib/src/bones_api_utils_timedmap.dart @@ -5,8 +5,8 @@ class TimedMap implements Map { /// The key timeout. When a key is put, it expires after the timeout [Duration]. final Duration keyTimeout; - final bool? Function(Object? key, Duration elapsedTime, Duration keyTimeout)? - keyTimeoutChecker; + final bool? Function(TimedMap timedMap, Object? key, Duration elapsedTime, + Duration keyTimeout)? keyTimeoutChecker; TimedMap(this.keyTimeout, [Map? map, this.keyTimeoutChecker]) { if (map != null) { @@ -140,7 +140,7 @@ class TimedMap implements Map { keyTimeout ??= this.keyTimeout; if (keyTimeoutChecker != null) { - var expired = keyTimeoutChecker(key, elapsedTime, keyTimeout); + var expired = keyTimeoutChecker(this, key, elapsedTime, keyTimeout); if (expired != null) { return expired; } diff --git a/test/bones_api_utils_test.dart b/test/bones_api_utils_test.dart index b5772ab..6230c30 100644 --- a/test/bones_api_utils_test.dart +++ b/test/bones_api_utils_test.dart @@ -433,6 +433,34 @@ void main() { expect(m.length, equals(0)); }); + + test('keyTimeoutChecker', () async { + final checks = []; + + var m = TimedMap(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', () {